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" }
}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 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 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.