pub struct Playwright { /* private fields */ }Expand description
Playwright is the root object that provides access to browser types.
This is the main entry point for the Playwright API. It provides access to the three browser types (Chromium, Firefox, WebKit) and other top-level services.
§Example
use playwright_rs::protocol::Playwright;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Launch Playwright server and initialize
let playwright = Playwright::launch().await?;
// Verify all three browser types are available
let chromium = playwright.chromium();
let firefox = playwright.firefox();
let webkit = playwright.webkit();
assert_eq!(chromium.name(), "chromium");
assert_eq!(firefox.name(), "firefox");
assert_eq!(webkit.name(), "webkit");
// Verify we can launch a browser
let browser = chromium.launch().await?;
assert!(!browser.version().is_empty());
browser.close().await?;
// Shutdown when done
playwright.shutdown().await?;
Ok(())
}Implementations§
Source§impl Playwright
impl Playwright
Sourcepub async fn launch() -> Result<Self>
pub async fn launch() -> Result<Self>
Launches Playwright and returns a handle to interact with browser types.
This is the main entry point for the Playwright API. It will:
- Launch the Playwright server process
- Establish a connection via stdio
- Initialize the protocol
- Return a Playwright instance with access to browser types
§Errors
Returns error if:
- Playwright server is not found or fails to launch
- Connection to server fails
- Protocol initialization fails
- Server doesn’t respond within timeout (30s)
Sourcepub async fn new(
connection: Arc<dyn ConnectionLike>,
type_name: String,
guid: Arc<str>,
initializer: Value,
) -> Result<Self>
pub async fn new( connection: Arc<dyn ConnectionLike>, type_name: String, guid: Arc<str>, initializer: Value, ) -> Result<Self>
Creates a new Playwright object from protocol initialization.
Called by the object factory when server sends create message for root object.
§Arguments
connection- The connection (Playwright is root, so no parent)type_name- Protocol type name (“Playwright”)guid- Unique GUID from server (typically “playwright@1”)initializer- Initial state with references to browser types
§Initializer Format
The initializer contains GUID references to BrowserType objects:
{
"chromium": { "guid": "browserType@chromium" },
"firefox": { "guid": "browserType@firefox" },
"webkit": { "guid": "browserType@webkit" }
}Note: Selectors is a pure client-side coordinator, not a protocol object.
It is created fresh here rather than looked up from the registry.
Sourcepub fn chromium(&self) -> &BrowserType
pub fn chromium(&self) -> &BrowserType
Returns the Chromium browser type.
Sourcepub fn firefox(&self) -> &BrowserType
pub fn firefox(&self) -> &BrowserType
Returns the Firefox browser type.
Sourcepub fn webkit(&self) -> &BrowserType
pub fn webkit(&self) -> &BrowserType
Returns the WebKit browser type.
Sourcepub fn request(&self) -> APIRequest
pub fn request(&self) -> APIRequest
Returns an APIRequest factory for creating standalone HTTP request contexts.
Use this to perform HTTP requests outside of a browser page, suitable for headless API testing.
§Example
let playwright = Playwright::launch().await?;
let ctx = playwright.request().new_context(None).await?;
let response = ctx.get("https://httpbin.org/get", None).await?;
assert!(response.ok());
ctx.dispose().await?;See: https://playwright.dev/docs/api/class-playwright#playwright-request
Sourcepub fn selectors(&self) -> Arc<Selectors>
pub fn selectors(&self) -> Arc<Selectors>
Returns the Selectors object for registering custom selector engines.
The Selectors instance is shared across all browser contexts created on this connection. Register custom selector engines here before creating any pages that will use them.
§Example
let playwright = Playwright::launch().await?;
let selectors = playwright.selectors();
selectors.set_test_id_attribute("data-custom-id").await?;See: https://playwright.dev/docs/api/class-playwright#playwright-selectors
Sourcepub fn devices(&self) -> &HashMap<String, DeviceDescriptor>
pub fn devices(&self) -> &HashMap<String, DeviceDescriptor>
Returns the device descriptors map for browser emulation.
Each entry maps a device name (e.g., "iPhone 13") to a DeviceDescriptor
containing user agent, viewport, and other emulation settings.
§Example
let playwright = Playwright::launch().await?;
let iphone = &playwright.devices()["iPhone 13"];
// Use iphone fields to configure BrowserContext...See: https://playwright.dev/docs/api/class-playwright#playwright-devices
Sourcepub async fn shutdown(&self) -> Result<()>
pub async fn shutdown(&self) -> Result<()>
Shuts down the Playwright server gracefully.
This method should be called when you’re done using Playwright to ensure the server process is terminated cleanly, especially on Windows.
§Platform-Specific Behavior
Windows: Closes stdio pipes before shutting down to prevent hangs.
Unix: Standard graceful shutdown.
§Errors
Returns an error if the server shutdown fails.
Trait Implementations§
Source§impl Clone for Playwright
impl Clone for Playwright
Source§fn clone(&self) -> Playwright
fn clone(&self) -> Playwright
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Playwright
impl Debug for Playwright
Source§impl Drop for Playwright
impl Drop for Playwright
Source§fn drop(&mut self)
fn drop(&mut self)
Ensures Playwright server is shut down when Playwright is dropped.
This is critical on Windows to prevent process hangs when tests complete. The Drop implementation will attempt to kill the server process synchronously.
Note: For graceful shutdown, prefer calling playwright.shutdown().await
explicitly before dropping.