Skip to main content

playwright_rs/
lib.rs

1//! playwright: High-level Rust bindings for Microsoft Playwright
2//!
3//! This crate provides the public API for browser automation using Playwright.
4//!
5//! # Examples
6//!
7//! ## Basic Navigation and Interaction
8//!
9//! ```ignore
10//! use playwright_rs::{Playwright, SelectOption};
11//!
12//! #[tokio::main]
13//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
14//!     let playwright = Playwright::launch().await?;
15//!     let browser = playwright.chromium().launch().await?;
16//!     let page = browser.new_page().await?;
17//!
18//!     // Navigate using data URL for self-contained test
19//!     let _ = page.goto(
20//!         "data:text/html,<html><body>\
21//!             <h1 id='title'>Welcome</h1>\
22//!             <button id='btn' onclick='this.textContent=\"Clicked\"'>Click me</button>\
23//!         </body></html>",
24//!         None
25//!     ).await;
26//!
27//!     // Query elements with locators
28//!     let heading = page.locator("#title").await;
29//!     let text = heading.text_content().await?;
30//!     assert_eq!(text, Some("Welcome".to_string()));
31//!
32//!     // Click button and verify result
33//!     let button = page.locator("#btn").await;
34//!     button.click(None).await?;
35//!     let button_text = button.text_content().await?;
36//!     assert_eq!(button_text, Some("Clicked".to_string()));
37//!
38//!     browser.close().await?;
39//!     Ok(())
40//! }
41//! ```
42//!
43//! ## Form Interaction
44//!
45//! ```ignore
46//! use playwright_rs::{Playwright, SelectOption};
47//!
48//! #[tokio::main]
49//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
50//!     let playwright = Playwright::launch().await?;
51//!     let browser = playwright.chromium().launch().await?;
52//!     let page = browser.new_page().await?;
53//!
54//!     // Create form with data URL
55//!     let _ = page.goto(
56//!         "data:text/html,<html><body>\
57//!             <input type='text' id='name' />\
58//!             <input type='checkbox' id='agree' />\
59//!             <select id='country'>\
60//!                 <option value='us'>USA</option>\
61//!                 <option value='uk'>UK</option>\
62//!                 <option value='ca'>Canada</option>\
63//!             </select>\
64//!         </body></html>",
65//!         None
66//!     ).await;
67//!
68//!     // Fill text input
69//!     let name = page.locator("#name").await;
70//!     name.fill("John Doe", None).await?;
71//!     assert_eq!(name.input_value(None).await?, "John Doe");
72//!
73//!     // Check checkbox
74//!     let checkbox = page.locator("#agree").await;
75//!     checkbox.set_checked(true, None).await?;
76//!     assert!(checkbox.is_checked().await?);
77//!
78//!     // Select option
79//!     let select = page.locator("#country").await;
80//!     select.select_option("uk", None).await?;
81//!     assert_eq!(select.input_value(None).await?, "uk");
82//!
83//!     browser.close().await?;
84//!     Ok(())
85//! }
86//! ```
87//!
88//! ## Element Screenshots
89//!
90//! ```ignore
91//! use playwright_rs::Playwright;
92//!
93//! #[tokio::main]
94//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
95//!     let playwright = Playwright::launch().await?;
96//!     let browser = playwright.chromium().launch().await?;
97//!     let page = browser.new_page().await?;
98//!
99//!     // Create element to screenshot
100//!     let _ = page.goto(
101//!         "data:text/html,<html><body>\
102//!             <div id='box' style='width:100px;height:100px;background:blue'></div>\
103//!         </body></html>",
104//!         None
105//!     ).await;
106//!
107//!     // Take screenshot of specific element
108//!     let element = page.locator("#box").await;
109//!     let screenshot = element.screenshot(None).await?;
110//!     assert!(!screenshot.is_empty());
111//!
112//!     browser.close().await?;
113//!     Ok(())
114//! }
115//! ```
116//!
117//! ## Assertions (expect API)
118//!
119//! ```ignore
120//! use playwright_rs::{expect, Playwright};
121//!
122//! #[tokio::main]
123//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
124//!     let playwright = Playwright::launch().await?;
125//!     let browser = playwright.chromium().launch().await?;
126//!     let page = browser.new_page().await?;
127//!
128//!     let _ = page.goto(
129//!         "data:text/html,<html><body>\
130//!             <button id='enabled'>Enabled</button>\
131//!             <button id='disabled' disabled>Disabled</button>\
132//!             <input type='checkbox' id='checked' checked />\
133//!         </body></html>",
134//!         None
135//!     ).await;
136//!
137//!     // Assert button states with auto-retry
138//!     let enabled_btn = page.locator("#enabled").await;
139//!     expect(enabled_btn.clone()).to_be_enabled().await?;
140//!
141//!     let disabled_btn = page.locator("#disabled").await;
142//!     expect(disabled_btn).to_be_disabled().await?;
143//!
144//!     // Assert checkbox state
145//!     let checkbox = page.locator("#checked").await;
146//!     expect(checkbox).to_be_checked().await?;
147//!
148//!     browser.close().await?;
149//!     Ok(())
150//! }
151//! ```
152
153// Internal modules (exposed for integration tests)
154#[doc(hidden)]
155pub mod server;
156
157pub mod api;
158mod assertions;
159mod error;
160pub mod protocol;
161mod tty_guard;
162
163/// Playwright server version bundled with this crate.
164///
165/// This version determines which browser builds are compatible.
166/// When installing browsers, use this version to ensure compatibility:
167///
168/// ```bash
169/// npx playwright@1.59.1 install
170/// ```
171///
172/// See: <https://playwright.dev/docs/browsers>
173pub const PLAYWRIGHT_VERSION: &str = env!("PLAYWRIGHT_DRIVER_VERSION");
174
175/// Default timeout in milliseconds for Playwright operations.
176///
177/// This matches Playwright's standard default across all language implementations (Python, Java, .NET, JS).
178/// Required in Playwright 1.56.1+ when timeout parameter is not explicitly provided.
179///
180/// See: <https://playwright.dev/docs/test-timeouts>
181pub const DEFAULT_TIMEOUT_MS: f64 = 30000.0;
182
183// Re-export error types
184pub use error::{Error, Result};
185
186// Re-export assertions API
187pub use assertions::{
188    Animations, PageExpectation, ScreenshotAssertionOptions, ScreenshotAssertionOptionsBuilder,
189    expect, expect_page,
190};
191
192// Re-export Playwright main entry point and browser API
193pub use protocol::{
194    Browser, BrowserContext, BrowserType, FrameLocator, HeaderEntry, Page, Playwright, Response,
195    Selectors,
196};
197
198// Re-export input device types
199pub use protocol::{Keyboard, Mouse, Touchscreen};
200
201// Re-export Request and related types
202pub use protocol::{Request, ResourceTiming};
203
204// Re-export Locator and element APIs
205pub use protocol::{
206    AriaRole, BoundingBox, ElementHandle, FilterOptions, GetByRoleOptions, JSHandle, Locator,
207};
208
209// Re-export navigation and page options
210pub use protocol::{GotoOptions, WaitUntil};
211
212// Re-export action options
213pub use protocol::{
214    CheckOptions, ClickOptions, DragToOptions, FillOptions, HoverOptions, PressOptions,
215    PressSequentiallyOptions, SelectOptions, TapOptions, WaitForOptions, WaitForState,
216};
217
218// Re-export Position (needed for DragToOptions and other options)
219pub use protocol::Position;
220
221// Re-export form and input types
222pub use protocol::{FilePayload, SelectOption};
223
224// Re-export screenshot types
225pub use protocol::{ScreenshotClip, ScreenshotOptions, ScreenshotType};
226
227// Re-export new page method types
228pub use protocol::{
229    AddScriptTagOptions, ColorScheme, EmulateMediaOptions, ForcedColors, Media, PdfMargin,
230    PdfOptions, ReducedMotion,
231};
232
233// Re-export browser context options and storage state types
234pub use protocol::{
235    BrowserContextOptions, Cookie, Geolocation, LocalStorageItem, Origin, RecordHar, RecordVideo,
236    StorageState, Viewport,
237};
238
239// Re-export EventWaiter for use with expect_page() / expect_close()
240pub use protocol::EventWaiter;
241
242// Re-export EventValue for use with expect_event()
243pub use protocol::EventValue;
244
245// Re-export ConsoleMessage types
246pub use protocol::{ConsoleMessage, ConsoleMessageLocation};
247
248// Re-export device descriptor types
249pub use protocol::{DeviceDescriptor, DeviceViewport};
250
251// Re-export WebError
252pub use protocol::WebError;
253
254// Re-export WebSocketRoute
255pub use protocol::{WebSocketRoute, WebSocketRouteCloseOptions};
256
257// Re-export FileChooser
258pub use protocol::FileChooser;
259
260// Re-export Accessibility and Coverage types
261pub use protocol::{
262    Accessibility, AccessibilitySnapshotOptions, CSSCoverageEntry, Coverage, CoverageRange,
263    JSCoverageEntry, JSCoverageRange, JSFunctionCoverage, StartCSSCoverageOptions,
264    StartJSCoverageOptions,
265};
266
267// Re-export Clock types
268pub use protocol::{Clock, ClockInstallOptions};
269
270// Re-export Video
271pub use protocol::Video;
272
273// Re-export routing types
274pub use protocol::{FetchOptions, FetchResponse, FulfillOptions, Route, UnrouteBehavior};
275
276// Re-export APIRequest public API
277pub use protocol::{APIRequest, APIRequestContext, APIRequestContextOptions, APIResponse};
278
279// Re-export launch and connection options
280pub use api::{ConnectOverCdpOptions, LaunchOptions};
281
282// Re-export browser installation helpers
283pub use server::driver::{install_browsers, install_browsers_with_deps};