Playwright

Struct Playwright 

Source
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_core::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(())
}

See: https://playwright.dev/docs/api/class-playwright

Implementations§

Source§

impl Playwright

Source

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:

  1. Launch the Playwright server process
  2. Establish a connection via stdio
  3. Initialize the protocol
  4. 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)
Source

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

pub fn chromium(&self) -> &BrowserType

Returns the Chromium browser type.

Source

pub fn firefox(&self) -> &BrowserType

Returns the Firefox browser type.

Source

pub fn webkit(&self) -> &BrowserType

Returns the WebKit browser type.

Source

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 ChannelOwner for Playwright

Source§

fn guid(&self) -> &str

Returns the unique GUID for this object. Read more
Source§

fn type_name(&self) -> &str

Returns the protocol type name (e.g., “Browser”, “Page”).
Source§

fn parent(&self) -> Option<Arc<dyn ChannelOwner>>

Returns the parent object, if any. Read more
Source§

fn connection(&self) -> Arc<dyn ConnectionLike>

Returns the connection this object belongs to.
Source§

fn initializer(&self) -> &Value

Returns the raw initializer JSON from the server. Read more
Source§

fn channel(&self) -> &Channel

Returns the channel for RPC communication.
Source§

fn dispose(&self, reason: DisposeReason)

Disposes this object and all its children. Read more
Source§

fn adopt(&self, child: Arc<dyn ChannelOwner>)

Adopts a child object (moves from old parent to this parent). Read more
Source§

fn add_child(&self, guid: Arc<str>, child: Arc<dyn ChannelOwner>)

Adds a child object to this parent’s registry. Read more
Source§

fn remove_child(&self, guid: &str)

Removes a child object from this parent’s registry. Read more
Source§

fn on_event(&self, method: &str, params: Value)

Handles a protocol event sent to this object. Read more
Source§

fn was_collected(&self) -> bool

Returns true if this object was garbage collected.
Source§

fn as_any(&self) -> &dyn Any

Enables downcasting to concrete types. Read more
Source§

impl Debug for Playwright

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Playwright

Source§

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more