playwright/lib.rs
1//! # Playwright for Rust
2//!
3//! Playwright is a modern browser automation library that provides a high-level API to control
4//! Chromium, Firefox, and WebKit browsers. This crate is a Rust port of the Playwright
5//! JavaScript library.
6//!
7//! ## Features
8//!
9//! - **Cross-browser support**: Automate Chromium, Firefox, and WebKit browsers
10//! - **Headless and headed modes**: Run browsers with or without a UI
11//! - **Device emulation**: Simulate real devices like iPhone, iPad, and Android phones
12//! - **Network control**: Monitor, intercept, and mock network requests
13//! - **Screenshot and video recording**: Capture screenshots and record videos of your automation
14//! - **Multiple runtimes**: Support for `tokio`, `actix-rt`, and `async-std`
15//!
16//! ## Getting Started
17//!
18//! Add this to your `Cargo.toml`:
19//!
20//! ```toml
21//! [dependencies]
22//! playwright = { url = "https://github.com/sctg-development/playwright-rust", branch = "master" }
23//! tokio = { version = "1", features = ["full"] }
24//! ```
25//!
26//! ## Basic Example
27//!
28//! This example initializes Playwright, launches a browser, navigates to a website,
29//! and retrieves the version number:
30//!
31//! ```
32//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
33//! use playwright::Playwright;
34//!
35//! // Initialize Playwright and install browsers if needed
36//! let playwright = Playwright::initialize().await?;
37//! playwright.prepare()?;
38//!
39//! // Launch a headless Chromium browser
40//! let chromium = playwright.chromium();
41//! let browser = chromium.launcher().headless(true).launch().await?; // Use .headless(false) to see the browser
42//!
43//! // Create a new browser context and page
44//! let context = browser.context_builder().build().await?;
45//! let page = context.new_page().await?;
46//!
47//! // Navigate to the GitHub Pages documentation
48//! page.goto_builder("https://sctg-development.github.io/playwright-rust/playwright/")
49//! .goto()
50//! .await?;
51//!
52//! // Execute JavaScript to get the current URL
53//! let url: String = page.eval("() => location.href").await?;
54//! println!("Current URL: {}", url);
55//!
56//! // Click on the API documentation link
57//! page.click_builder(r#"a[title="mod playwright::api"]"#)
58//! .click()
59//! .await?;
60//!
61//! // Extract the version number from the documentation page
62//! let version: String = page
63//! .eval(r#"() => document.querySelector("span.version").innerText.trim()"#)
64//! .await?;
65//! println!("Package version: {}", version);
66//!
67//! // Verify we're on the correct page
68//! assert_eq!(
69//! page.url().unwrap(),
70//! "https://sctg-development.github.io/playwright-rust/playwright/api/index.html"
71//! );
72//!
73//! // Clean up - browser context and page are automatically closed when dropped
74//! browser.close().await?;
75//! # Ok(())
76//! # }
77//! ```
78//!
79//! ## Async Runtimes
80//!
81//! This crate supports multiple async runtimes via feature flags:
82//!
83//! - `rt-tokio` (default): Uses the `tokio` runtime
84//! - `rt-actix`: Uses the `actix-rt` runtime
85//! - `rt-async-std`: Uses the `async-std` runtime
86//!
87//! ## Main Types
88//!
89//! - [`Playwright`]: The main entry point for browser automation
90//! - [`api::browser_type::BrowserType`]: Launcher for a specific browser engine
91//! - [`api::browser::Browser`]: A browser instance
92//! - [`api::browser_context::BrowserContext`]: A context in which pages are isolated
93//! - [`api::page::Page`]: A single tab or window in a browser context
94//!
95//! ## Resources
96//!
97//! - [GitHub Repository](https://github.com/sctg-development/playwright-rust)
98//! - [Official Playwright Documentation](https://playwright.dev)
99
100#[macro_use]
101extern crate serde;
102#[macro_use]
103extern crate serde_with;
104
105pub mod api;
106mod imp;
107
108pub use crate::imp::core::{Driver, Error};
109pub use api::playwright::Playwright;
110
111#[doc(hidden)]
112#[macro_export]
113macro_rules! runtime_test {
114 ($name:tt, $main:stmt) => {
115 #[cfg(feature = "rt-tokio")]
116 #[test]
117 fn $name() {
118 env_logger::builder().is_test(true).try_init().ok();
119 tokio::runtime::Builder::new_current_thread()
120 .enable_all()
121 .build()
122 .unwrap()
123 .block_on(async { $main });
124 }
125
126 #[cfg(feature = "rt-actix")]
127 #[test]
128 fn $name() {
129 env_logger::builder().is_test(true).try_init().ok();
130 actix_rt::System::new().block_on(async { $main });
131 }
132
133 #[cfg(feature = "rt-async-std")]
134 #[test]
135 fn $name() {
136 env_logger::builder().is_test(true).try_init().ok();
137 async_std::task::block_on(async { $main });
138 }
139 };
140}