viewpoint_core/page/
mod.rs

1//! # Page Management and Interaction
2//!
3//! The `Page` type represents a browser tab and provides methods for navigation,
4//! content interaction, and capturing screenshots or PDFs.
5//!
6//! ## Features
7//!
8//! - **Navigation**: Navigate to URLs, go back/forward, reload
9//! - **Element Interaction**: Locate and interact with elements via [`Locator`]
10//! - **JavaScript Evaluation**: Execute JavaScript in the page context
11//! - **Screenshots**: Capture viewport or full page screenshots
12//! - **PDF Generation**: Generate PDFs from page content
13//! - **Input Devices**: Control keyboard, mouse, and touchscreen
14//! - **Event Handling**: Handle dialogs, downloads, console messages
15//! - **Network Interception**: Route, modify, and mock network requests
16//! - **Clock Mocking**: Control time in the page with [`Clock`]
17//! - **Frames**: Access and interact with iframes via [`Frame`] and [`FrameLocator`]
18//! - **Video Recording**: Record page interactions
19//!
20//! ## Quick Start
21//!
22//! ```no_run
23//! use viewpoint_core::{Browser, DocumentLoadState};
24//! use std::time::Duration;
25//!
26//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
27//! let browser = Browser::launch().headless(true).launch().await?;
28//! let context = browser.new_context().await?;
29//! let page = context.new_page().await?;
30//!
31//! // Navigate to a URL
32//! page.goto("https://example.com")
33//!     .wait_until(DocumentLoadState::DomContentLoaded)
34//!     .goto()
35//!     .await?;
36//!
37//! // Get page title
38//! let title = page.title().await?;
39//! println!("Page title: {}", title);
40//!
41//! // Get current URL
42//! let url = page.url().await?;
43//! println!("Current URL: {}", url);
44//! # Ok(())
45//! # }
46//! ```
47//!
48//! ## Element Interaction with Locators
49//!
50//! ```no_run
51//! use viewpoint_core::{Browser, AriaRole};
52//!
53//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
54//! # let browser = Browser::launch().headless(true).launch().await?;
55//! # let context = browser.new_context().await?;
56//! # let page = context.new_page().await?;
57//! // Click a button
58//! page.locator("button#submit").click().await?;
59//!
60//! // Fill an input
61//! page.locator("input[name='email']").fill("user@example.com").await?;
62//!
63//! // Get text content
64//! let text = page.locator("h1").text_content().await?;
65//!
66//! // Use semantic locators
67//! page.get_by_role(AriaRole::Button)
68//!     .with_name("Submit")
69//!     .build()
70//!     .click()
71//!     .await?;
72//!
73//! page.get_by_label("Username").fill("john").await?;
74//! page.get_by_placeholder("Search...").fill("query").await?;
75//! page.get_by_test_id("submit-btn").click().await?;
76//! # Ok(())
77//! # }
78//! ```
79//!
80//! ## Screenshots and PDF
81//!
82//! ```no_run
83//! use viewpoint_core::Browser;
84//! use viewpoint_core::page::PaperFormat;
85//!
86//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
87//! # let browser = Browser::launch().headless(true).launch().await?;
88//! # let context = browser.new_context().await?;
89//! # let page = context.new_page().await?;
90//! // Viewport screenshot
91//! page.screenshot()
92//!     .path("screenshot.png")
93//!     .capture()
94//!     .await?;
95//!
96//! // Full page screenshot
97//! page.screenshot()
98//!     .full_page(true)
99//!     .path("full-page.png")
100//!     .capture()
101//!     .await?;
102//!
103//! // Generate PDF
104//! page.pdf()
105//!     .format(PaperFormat::A4)
106//!     .path("document.pdf")
107//!     .generate()
108//!     .await?;
109//! # Ok(())
110//! # }
111//! ```
112//!
113//! ## Input Devices
114//!
115//! ```ignore
116//! use viewpoint_core::Browser;
117//!
118//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
119//! # let browser = Browser::launch().headless(true).launch().await?;
120//! # let context = browser.new_context().await?;
121//! # let page = context.new_page().await?;
122//! // Keyboard
123//! page.keyboard().press("Tab").await?;
124//! page.keyboard().type_text("Hello World").await?;
125//! page.keyboard().press("Control+a").await?;
126//!
127//! // Mouse
128//! page.mouse().click(100.0, 200.0).await?;
129//! page.mouse().move_to(300.0, 400.0).await?;
130//!
131//! // Touchscreen
132//! page.touchscreen().tap(100.0, 200.0).await?;
133//! # Ok(())
134//! # }
135//! ```
136//!
137//! ## Event Handling
138//!
139//! ```ignore
140//! use viewpoint_core::Browser;
141//!
142//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
143//! # let browser = Browser::launch().headless(true).launch().await?;
144//! # let context = browser.new_context().await?;
145//! # let page = context.new_page().await?;
146//! // Handle dialogs
147//! page.on_dialog(|dialog| async move {
148//!     println!("Dialog: {}", dialog.message());
149//!     dialog.accept(None).await
150//! }).await;
151//!
152//! // Handle downloads
153//! page.on_download(|download| async move {
154//!     download.save_as("file.zip").await
155//! }).await;
156//!
157//! // Handle console messages
158//! page.on_console(|msg| async move {
159//!     println!("[{}] {}", msg.message_type(), msg.text());
160//!     Ok(())
161//! }).await;
162//! # Ok(())
163//! # }
164//! ```
165//!
166//! ## Frames
167//!
168//! ```ignore
169//! use viewpoint_core::Browser;
170//!
171//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
172//! # let browser = Browser::launch().headless(true).launch().await?;
173//! # let context = browser.new_context().await?;
174//! # let page = context.new_page().await?;
175//! // Access iframe by selector
176//! let frame = page.frame_locator("iframe#content");
177//! frame.locator("button").click().await?;
178//!
179//! // Access iframe by name
180//! let frame = page.frame("content-frame").await;
181//! if let Some(f) = frame {
182//!     f.locator("input").fill("text").await?;
183//! }
184//! # Ok(())
185//! # }
186//! ```
187//!
188//! ## Clock Mocking
189//!
190//! ```ignore
191//! use viewpoint_core::Browser;
192//!
193//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
194//! # let browser = Browser::launch().headless(true).launch().await?;
195//! # let context = browser.new_context().await?;
196//! # let page = context.new_page().await?;
197//! // Install clock mocking
198//! page.clock().install().await?;
199//!
200//! // Set to specific time
201//! page.clock().set_fixed_time("2024-01-01T12:00:00Z").await?;
202//!
203//! // Advance time
204//! page.clock().run_for(60000).await?; // 60 seconds
205//! # Ok(())
206//! # }
207//! ```
208
209mod accessors;
210mod aria_snapshot;
211pub use aria_snapshot::SnapshotOptions;
212pub mod binding;
213pub mod clock;
214mod clock_script;
215pub mod console;
216mod constructors;
217mod content;
218pub mod dialog;
219pub mod download;
220pub mod emulation;
221mod evaluate;
222pub mod events;
223pub mod file_chooser;
224pub mod frame;
225pub mod frame_locator;
226mod frame_locator_actions;
227mod frame_page_methods;
228mod input_devices;
229pub mod keyboard;
230mod lifecycle;
231pub mod locator;
232mod locator_factory;
233pub mod locator_handler;
234mod mouse;
235mod mouse_drag;
236mod navigation;
237pub mod page_error;
238mod page_info;
239mod pdf;
240pub mod popup;
241mod ref_resolution;
242mod routing_impl;
243mod screenshot;
244mod screenshot_element;
245mod scripts;
246mod touchscreen;
247pub mod video;
248mod video_io;
249
250use std::sync::Arc;
251use std::time::Duration;
252
253use tokio::sync::RwLock;
254use viewpoint_cdp::CdpConnection;
255
256use crate::error::NavigationError;
257use crate::network::{RouteHandlerRegistry, WebSocketManager};
258
259pub use clock::{Clock, TimeValue};
260pub use console::{ConsoleMessage, ConsoleMessageLocation, ConsoleMessageType, JsArg};
261pub use content::{ScriptTagBuilder, ScriptType, SetContentBuilder, StyleTagBuilder};
262pub use dialog::Dialog;
263pub use download::{Download, DownloadState};
264pub use emulation::{EmulateMediaBuilder, MediaType, VisionDeficiency};
265pub use evaluate::{JsHandle, Polling, WaitForFunctionBuilder};
266pub use events::PageEventManager;
267pub use file_chooser::{FileChooser, FilePayload};
268pub(crate) use frame::ExecutionContextRegistry;
269pub use frame::Frame;
270pub use frame_locator::{FrameElementLocator, FrameLocator, FrameRoleLocatorBuilder};
271pub use keyboard::Keyboard;
272pub use locator::{
273    AriaCheckedState, AriaRole, AriaSnapshot, BoundingBox, BoxModel, ElementHandle, FilterBuilder,
274    Locator, LocatorOptions, RoleLocatorBuilder, Selector, TapBuilder, TextOptions,
275};
276pub use locator_handler::{LocatorHandlerHandle, LocatorHandlerManager, LocatorHandlerOptions};
277pub use mouse::Mouse;
278pub use mouse_drag::DragAndDropBuilder;
279pub use navigation::{GotoBuilder, NavigationResponse};
280pub use page_error::{PageError as PageErrorInfo, WebError};
281pub use pdf::{Margins, PaperFormat, PdfBuilder};
282pub use screenshot::{Animations, ClipRegion, ScreenshotBuilder, ScreenshotFormat};
283pub use touchscreen::Touchscreen;
284pub use video::{Video, VideoOptions};
285pub use viewpoint_cdp::protocol::DialogType;
286pub use viewpoint_cdp::protocol::emulation::ViewportSize;
287pub use viewpoint_cdp::protocol::input::MouseButton;
288
289/// Default navigation timeout.
290const DEFAULT_NAVIGATION_TIMEOUT: Duration = Duration::from_secs(30);
291
292/// Default test ID attribute name.
293pub const DEFAULT_TEST_ID_ATTRIBUTE: &str = "data-testid";
294
295/// A browser page (tab).
296pub struct Page {
297    /// CDP connection.
298    connection: Arc<CdpConnection>,
299    /// Target ID.
300    target_id: String,
301    /// Session ID for this page.
302    session_id: String,
303    /// Main frame ID.
304    frame_id: String,
305    /// Context index for element ref generation.
306    /// Used to generate scoped element refs in the format `c{contextIndex}p{pageIndex}e{counter}`.
307    context_index: usize,
308    /// Page index within the context for element ref generation.
309    /// Used to generate scoped element refs in the format `c{contextIndex}p{pageIndex}e{counter}`.
310    page_index: usize,
311    /// Whether the page has been closed.
312    closed: bool,
313    /// Route handler registry.
314    route_registry: Arc<RouteHandlerRegistry>,
315    /// Keyboard controller.
316    keyboard: Keyboard,
317    /// Mouse controller.
318    mouse: Mouse,
319    /// Touchscreen controller.
320    touchscreen: Touchscreen,
321    /// Event manager for dialogs, downloads, and file choosers.
322    event_manager: Arc<PageEventManager>,
323    /// Locator handler manager.
324    locator_handler_manager: Arc<LocatorHandlerManager>,
325    /// Video recording controller (if recording is enabled).
326    video_controller: Option<Arc<Video>>,
327    /// Opener target ID (for popup pages).
328    opener_target_id: Option<String>,
329    /// Popup event manager.
330    popup_manager: Arc<popup::PopupManager>,
331    /// WebSocket event manager.
332    websocket_manager: Arc<WebSocketManager>,
333    /// Exposed function binding manager.
334    binding_manager: Arc<binding::BindingManager>,
335    /// Custom test ID attribute (defaults to "data-testid").
336    test_id_attribute: String,
337    /// Execution context registry for tracking frame contexts.
338    context_registry: Arc<ExecutionContextRegistry>,
339    /// Ref map for element ref resolution.
340    /// Maps ref strings (e.g., `c0p0e1`) to their backendNodeIds.
341    /// Updated on each `aria_snapshot()` call.
342    ref_map: std::sync::Arc<
343        parking_lot::RwLock<
344            std::collections::HashMap<String, viewpoint_cdp::protocol::dom::BackendNodeId>,
345        >,
346    >,
347    /// Reference to context's pages list for removal on close.
348    /// This is used to remove the page from the context's tracking list when closed,
349    /// preventing stale sessions from accumulating.
350    /// Stores a `Vec<Page>` to enable returning functional Page objects from context.pages().
351    context_pages: Option<Arc<RwLock<Vec<Page>>>>,
352}
353
354// Manual Debug implementation since some fields don't implement Debug
355impl std::fmt::Debug for Page {
356    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
357        f.debug_struct("Page")
358            .field("target_id", &self.target_id)
359            .field("session_id", &self.session_id)
360            .field("frame_id", &self.frame_id)
361            .field("context_index", &self.context_index)
362            .field("page_index", &self.page_index)
363            .field("closed", &self.closed)
364            .finish_non_exhaustive()
365    }
366}
367
368impl Page {
369    /// Navigate to a URL.
370    ///
371    /// Returns a builder for configuring navigation options.
372    ///
373    /// # Example
374    ///
375    /// ```no_run
376    /// use viewpoint_core::Page;
377    /// use viewpoint_core::DocumentLoadState;
378    /// use std::time::Duration;
379    ///
380    /// # async fn example(page: Page) -> Result<(), viewpoint_core::CoreError> {
381    /// // Simple navigation
382    /// page.goto("https://example.com").goto().await?;
383    ///
384    /// // Navigation with options
385    /// page.goto("https://example.com")
386    ///     .wait_until(DocumentLoadState::DomContentLoaded)
387    ///     .timeout(Duration::from_secs(10))
388    ///     .goto()
389    ///     .await?;
390    /// # Ok(())
391    /// # }
392    /// ```
393    pub fn goto(&self, url: impl Into<String>) -> GotoBuilder<'_> {
394        GotoBuilder::new(self, url.into())
395    }
396
397    /// Navigate to a URL and wait for the specified load state.
398    ///
399    /// This is a convenience method that calls `goto(url).goto().await`.
400    ///
401    /// # Errors
402    ///
403    /// Returns an error if:
404    /// - The page is closed
405    /// - Navigation fails
406    /// - The wait times out
407    pub async fn goto_url(&self, url: &str) -> Result<NavigationResponse, NavigationError> {
408        self.goto(url).goto().await
409    }
410
411    // =========================================================================
412    // Screenshot & PDF Methods
413    // =========================================================================
414
415    /// Create a screenshot builder for capturing page screenshots.
416    ///
417    /// # Example
418    ///
419    /// ```no_run
420    /// # async fn example(page: viewpoint_core::Page) -> Result<(), viewpoint_core::CoreError> {
421    /// // Capture viewport screenshot
422    /// let bytes = page.screenshot().capture().await?;
423    ///
424    /// // Capture full page screenshot
425    /// page.screenshot()
426    ///     .full_page(true)
427    ///     .path("screenshot.png")
428    ///     .capture()
429    ///     .await?;
430    ///
431    /// // Capture JPEG with quality
432    /// page.screenshot()
433    ///     .jpeg(Some(80))
434    ///     .path("screenshot.jpg")
435    ///     .capture()
436    ///     .await?;
437    /// # Ok(())
438    /// # }
439    /// ```
440    pub fn screenshot(&self) -> screenshot::ScreenshotBuilder<'_> {
441        screenshot::ScreenshotBuilder::new(self)
442    }
443
444    /// Create a PDF builder for generating PDFs from the page.
445    ///
446    /// # Example
447    ///
448    /// ```no_run
449    /// use viewpoint_core::page::PaperFormat;
450    ///
451    /// # async fn example(page: viewpoint_core::Page) -> Result<(), viewpoint_core::CoreError> {
452    /// // Generate PDF with default settings
453    /// let bytes = page.pdf().generate().await?;
454    ///
455    /// // Generate A4 landscape PDF
456    /// page.pdf()
457    ///     .format(PaperFormat::A4)
458    ///     .landscape(true)
459    ///     .path("document.pdf")
460    ///     .generate()
461    ///     .await?;
462    ///
463    /// // Generate PDF with custom margins
464    /// page.pdf()
465    ///     .margin(1.0) // 1 inch margins
466    ///     .print_background(true)
467    ///     .generate()
468    ///     .await?;
469    /// # Ok(())
470    /// # }
471    /// ```
472    pub fn pdf(&self) -> pdf::PdfBuilder<'_> {
473        pdf::PdfBuilder::new(self)
474    }
475}