Crate playwright

Crate playwright 

Source
Expand description

§Playwright for Rust

Playwright is a modern browser automation library that provides a high-level API to control Chromium, Firefox, and WebKit browsers. This crate is a Rust port of the Playwright JavaScript library.

§Features

  • Cross-browser support: Automate Chromium, Firefox, and WebKit browsers
  • Headless and headed modes: Run browsers with or without a UI
  • Device emulation: Simulate real devices like iPhone, iPad, and Android phones
  • Network control: Monitor, intercept, and mock network requests
  • Screenshot and video recording: Capture screenshots and record videos of your automation
  • Multiple runtimes: Support for tokio, actix-rt, and async-std

§Getting Started

Add this to your Cargo.toml:

[dependencies]
playwright = { url = "https://github.com/sctg-development/playwright-rust", branch = "master" }
tokio = { version = "1", features = ["full"] }

§Basic Example

This example initializes Playwright, launches a browser, navigates to a website, and retrieves the version number:

use playwright::Playwright;

// Initialize Playwright and install browsers if needed
let playwright = Playwright::initialize().await?;
playwright.prepare()?;

// Launch a headless Chromium browser
let chromium = playwright.chromium();
let browser = chromium.launcher().headless(true).launch().await?; // Use .headless(false) to see the browser

// Create a new browser context and page
let context = browser.context_builder().build().await?;
let page = context.new_page().await?;

// Navigate to the GitHub Pages documentation
page.goto_builder("https://sctg-development.github.io/playwright-rust/playwright/")
    .goto()
    .await?;

// Execute JavaScript to get the current URL
let url: String = page.eval("() => location.href").await?;
println!("Current URL: {}", url);

// Click on the API documentation link
page.click_builder(r#"a[title="mod playwright::api"]"#)
    .click()
    .await?;

// Extract the version number from the documentation page
let version: String = page
    .eval(r#"() => document.querySelector("span.version").innerText.trim()"#)
    .await?;
println!("Package version: {}", version);

// Verify we're on the correct page
assert_eq!(
    page.url().unwrap(),
    "https://sctg-development.github.io/playwright-rust/playwright/api/index.html"
);

// Clean up - browser context and page are automatically closed when dropped
browser.close().await?;

§Async Runtimes

This crate supports multiple async runtimes via feature flags:

  • rt-tokio (default): Uses the tokio runtime
  • rt-actix: Uses the actix-rt runtime
  • rt-async-std: Uses the async-std runtime

§Main Types

§Resources

Re-exports§

pub use api::playwright::Playwright;

Modules§

api
imp 🔒

Structs§

Driver

Enums§

Error