Expand description
§Page Management and Interaction
The Page type represents a browser tab and provides methods for navigation,
content interaction, and capturing screenshots or PDFs.
§Features
- Navigation: Navigate to URLs, go back/forward, reload
- Element Interaction: Locate and interact with elements via
Locator - JavaScript Evaluation: Execute JavaScript in the page context
- Screenshots: Capture viewport or full page screenshots
- PDF Generation: Generate PDFs from page content
- Input Devices: Control keyboard, mouse, and touchscreen
- Event Handling: Handle dialogs, downloads, console messages
- Network Interception: Route, modify, and mock network requests
- Clock Mocking: Control time in the page with
Clock - Frames: Access and interact with iframes via
FrameandFrameLocator - Video Recording: Record page interactions
§Quick Start
use viewpoint_core::{Browser, DocumentLoadState};
use std::time::Duration;
let browser = Browser::launch().headless(true).launch().await?;
let context = browser.new_context().await?;
let page = context.new_page().await?;
// Navigate to a URL
page.goto("https://example.com")
.wait_until(DocumentLoadState::DomContentLoaded)
.goto()
.await?;
// Get page title
let title = page.title().await?;
println!("Page title: {}", title);
// Get current URL
let url = page.url().await?;
println!("Current URL: {}", url);§Element Interaction with Locators
use viewpoint_core::{Browser, AriaRole};
// Click a button
page.locator("button#submit").click().await?;
// Fill an input
page.locator("input[name='email']").fill("user@example.com").await?;
// Get text content
let text = page.locator("h1").text_content().await?;
// Use semantic locators
page.get_by_role(AriaRole::Button)
.with_name("Submit")
.build()
.click()
.await?;
page.get_by_label("Username").fill("john").await?;
page.get_by_placeholder("Search...").fill("query").await?;
page.get_by_test_id("submit-btn").click().await?;§Screenshots and PDF
use viewpoint_core::Browser;
use viewpoint_core::page::PaperFormat;
// Viewport screenshot
page.screenshot()
.path("screenshot.png")
.capture()
.await?;
// Full page screenshot
page.screenshot()
.full_page(true)
.path("full-page.png")
.capture()
.await?;
// Generate PDF
page.pdf()
.format(PaperFormat::A4)
.path("document.pdf")
.generate()
.await?;§Input Devices
ⓘ
use viewpoint_core::Browser;
// Keyboard
page.keyboard().press("Tab").await?;
page.keyboard().type_text("Hello World").await?;
page.keyboard().press("Control+a").await?;
// Mouse
page.mouse().click(100.0, 200.0).await?;
page.mouse().move_to(300.0, 400.0).await?;
// Touchscreen
page.touchscreen().tap(100.0, 200.0).await?;§Event Handling
ⓘ
use viewpoint_core::Browser;
// Handle dialogs
page.on_dialog(|dialog| async move {
println!("Dialog: {}", dialog.message());
dialog.accept(None).await
}).await;
// Handle downloads
page.on_download(|download| async move {
download.save_as("file.zip").await
}).await;
// Handle console messages
page.on_console(|msg| async move {
println!("[{}] {}", msg.message_type(), msg.text());
Ok(())
}).await;§Frames
ⓘ
use viewpoint_core::Browser;
// Access iframe by selector
let frame = page.frame_locator("iframe#content");
frame.locator("button").click().await?;
// Access iframe by name
let frame = page.frame("content-frame").await;
if let Some(f) = frame {
f.locator("input").fill("text").await?;
}§Clock Mocking
ⓘ
use viewpoint_core::Browser;
// Install clock mocking
page.clock().install().await?;
// Set to specific time
page.clock().set_fixed_time("2024-01-01T12:00:00Z").await?;
// Advance time
page.clock().run_for(60000).await?; // 60 secondsRe-exports§
pub use clock::Clock;pub use clock::TimeValue;pub use console::ConsoleMessage;pub use console::ConsoleMessageLocation;pub use console::ConsoleMessageType;pub use console::JsArg;pub use dialog::Dialog;pub use download::Download;pub use download::DownloadState;pub use emulation::EmulateMediaBuilder;pub use emulation::MediaType;pub use emulation::VisionDeficiency;pub use events::PageEventManager;pub use file_chooser::FileChooser;pub use file_chooser::FilePayload;pub use frame::Frame;pub use frame_locator::FrameElementLocator;pub use frame_locator::FrameLocator;pub use frame_locator::FrameRoleLocatorBuilder;pub use keyboard::Keyboard;pub use locator::AriaCheckedState;pub use locator::AriaRole;pub use locator::AriaSnapshot;pub use locator::BoundingBox;pub use locator::BoxModel;pub use locator::ElementHandle;pub use locator::FilterBuilder;pub use locator::Locator;pub use locator::LocatorOptions;pub use locator::RoleLocatorBuilder;pub use locator::Selector;pub use locator::TapBuilder;pub use locator::TextOptions;pub use locator_handler::LocatorHandlerHandle;pub use locator_handler::LocatorHandlerManager;pub use locator_handler::LocatorHandlerOptions;pub use page_error::PageError as PageErrorInfo;pub use page_error::WebError;pub use video::Video;pub use video::VideoOptions;
Modules§
- binding
- Exposed function bindings.
- clock
- Clock mocking for controlling time in browser pages.
- console
- Console message types and event handling.
- dialog
- Dialog handling for browser dialogs.
- download
- Download handling for browser downloads.
- emulation
- Page emulation features for media and vision deficiency emulation.
- events
- Page event handling for dialogs, downloads, file choosers, console, and errors.
- file_
chooser - File chooser handling for file upload dialogs.
- frame
- Frame management and navigation.
- frame_
locator - Frame locator for interacting with iframe content.
- keyboard
- Keyboard input handling.
- locator
- Locator system for element selection.
- locator_
handler - Locator handlers for handling overlay elements that block actions.
- page_
error - Page error types and event handling.
- popup
- Popup window handling.
- video
- Video recording for pages.
Structs§
- Clip
Region - Clip region for screenshots.
- Drag
AndDrop Builder - Builder for drag and drop operations.
- Goto
Builder - Builder for configuring page navigation.
- JsHandle
- A handle to a JavaScript object in the page context.
- Margins
- Margins for PDF generation in inches.
- Mouse
- Mouse controller for direct mouse input.
- Navigation
Response - Response from a navigation.
- Page
- A browser page (tab).
- PdfBuilder
- Builder for generating PDFs.
- Screenshot
Builder - Builder for capturing screenshots.
- Script
TagBuilder - Builder for injecting script tags.
- SetContent
Builder - Builder for setting page content.
- Style
TagBuilder - Builder for injecting style tags.
- Touchscreen
- Touchscreen controller for touch input simulation.
- Viewport
Size - Viewport size.
- Wait
ForFunction Builder - Builder for
wait_for_function.
Enums§
- Animations
- Animation handling mode for screenshots.
- Dialog
Type - Type of JavaScript dialog.
- Mouse
Button - Mouse button type.
- Paper
Format - Paper format for PDF generation.
- Polling
- Polling mode for
wait_for_function. - Screenshot
Format - Image format for screenshots.
- Script
Type - Script type for injection.
Constants§
- DEFAULT_
TEST_ ID_ ATTRIBUTE - Default test ID attribute name.