Playwriter.Transport.Behaviour behaviour (Playwriter v0.2.0)

Copy Markdown View Source

Behaviour defining the transport interface for Playwright communication.

Transports abstract how Playwriter communicates with Playwright:

Summary

Callbacks

Add cookies to a context. Each cookie is a map using Playwright's field names (name, value, and either url or domain+path; optionally expires, httpOnly, secure, sameSite). Used to seed a signed session cookie so tests can start past an auth gate without driving the login UI.

Add an init script to a context, evaluated before any page scripts on every page/navigation in that context. Targets a context guid, so it must be installed before the page is created.

Send a CDP command over a session opened with new_cdp_session/2 (e.g. Network.emulateNetworkConditions). Windows transport only.

Get page content

Evaluate a JavaScript expression in a frame and return the result.

Expose an Elixir callback to the page as a binding: the page can call window.<name>(...args) and the transport invokes the callback with the argument list, returning its result to the page.

Check if transport is healthy

Open a Chrome DevTools Protocol session for a page. Returns an opaque CDP session id to be used with cdp_send/4.

Create a new browser context

Create a new page in a context

Send a message to Playwright and wait for response

Start the transport connection

Stop the transport

Return the context's storage state (cookies + localStorage snapshot), e.g. to capture it after a UI login and re-seed it (via add_cookies/3) in later runs.

Wait until a JavaScript predicate becomes truthy in a frame.

Types

browser_type()

@type browser_type() :: :chromium | :firefox | :webkit

guid()

@type guid() :: String.t()

message()

@type message() :: map()

result()

@type result() :: {:ok, map()} | {:error, term()}

transport()

@type transport() :: pid() | GenServer.server()

Callbacks

add_cookies(transport, guid, list)

@callback add_cookies(transport(), guid(), [map()]) :: :ok | {:error, term()}

Add cookies to a context. Each cookie is a map using Playwright's field names (name, value, and either url or domain+path; optionally expires, httpOnly, secure, sameSite). Used to seed a signed session cookie so tests can start past an auth gate without driving the login UI.

add_init_script(transport, guid, t, keyword)

@callback add_init_script(transport(), guid(), String.t(), keyword()) ::
  :ok | {:error, term()}

Add an init script to a context, evaluated before any page scripts on every page/navigation in that context. Targets a context guid, so it must be installed before the page is created.

cdp_send(transport, guid, t, map)

@callback cdp_send(transport(), guid(), String.t(), map()) ::
  {:ok, term()} | {:error, term()}

Send a CDP command over a session opened with new_cdp_session/2 (e.g. Network.emulateNetworkConditions). Windows transport only.

click(transport, guid, t, keyword)

@callback click(transport(), guid(), String.t(), keyword()) :: :ok | {:error, term()}

Click an element

close_browser(transport, guid)

@callback close_browser(transport(), guid()) :: :ok | {:error, term()}

Close a browser

close_context(transport, guid)

@callback close_context(transport(), guid()) :: :ok | {:error, term()}

Close a context

close_page(transport, guid)

@callback close_page(transport(), guid()) :: :ok | {:error, term()}

Close a page

content(transport, guid)

@callback content(transport(), guid()) :: {:ok, String.t()} | {:error, term()}

Get page content

evaluate(transport, guid, t, keyword)

@callback evaluate(transport(), guid(), String.t(), keyword()) ::
  {:ok, term()} | {:error, term()}

Evaluate a JavaScript expression in a frame and return the result.

opts may include :is_function (treat the expression as a function body), :arg (an argument passed to the function), and :timeout.

expose_binding(transport, guid, t, function)

@callback expose_binding(transport(), guid(), String.t(), (list() -> term())) ::
  :ok | {:error, term()}

Expose an Elixir callback to the page as a binding: the page can call window.<name>(...args) and the transport invokes the callback with the argument list, returning its result to the page.

Experimental and :windows-only - it is the one verb that needs the bidirectional event channel. :local/:remote return {:error, :not_supported}. Most harness needs are met by polling with evaluate/4 + wait_for_function/4; prefer those.

fill(transport, guid, t, t, keyword)

@callback fill(transport(), guid(), String.t(), String.t(), keyword()) ::
  :ok | {:error, term()}

Fill an input

goto(transport, guid, t, keyword)

@callback goto(transport(), guid(), String.t(), keyword()) :: result()

Navigate to a URL

healthy?(transport)

@callback healthy?(transport()) :: boolean()

Check if transport is healthy

launch_browser(transport, browser_type, keyword)

@callback launch_browser(transport(), browser_type(), keyword()) ::
  {:ok, guid()} | {:error, term()}

Launch a browser instance

new_cdp_session(transport, guid)

@callback new_cdp_session(transport(), guid()) :: {:ok, guid()} | {:error, term()}

Open a Chrome DevTools Protocol session for a page. Returns an opaque CDP session id to be used with cdp_send/4.

Only the :windows transport supports CDP; :local returns {:error, :not_supported} (playwright_ex exposes no CDP surface).

new_context(transport, guid, keyword)

@callback new_context(transport(), guid(), keyword()) :: {:ok, guid()} | {:error, term()}

Create a new browser context

new_page(transport, guid)

@callback new_page(transport(), guid()) :: {:ok, map()} | {:error, term()}

Create a new page in a context

screenshot(transport, guid, keyword)

@callback screenshot(transport(), guid(), keyword()) :: {:ok, binary()} | {:error, term()}

Take a screenshot

send_message(transport, message, timeout)

@callback send_message(transport(), message(), timeout()) :: result()

Send a message to Playwright and wait for response

start_link(keyword)

@callback start_link(keyword()) :: {:ok, pid()} | {:error, term()}

Start the transport connection

stop(transport)

@callback stop(transport()) :: :ok

Stop the transport

storage_state(transport, guid)

@callback storage_state(transport(), guid()) :: {:ok, map()} | {:error, term()}

Return the context's storage state (cookies + localStorage snapshot), e.g. to capture it after a UI login and re-seed it (via add_cookies/3) in later runs.

wait_for_function(transport, guid, t, keyword)

@callback wait_for_function(transport(), guid(), String.t(), keyword()) ::
  {:ok, term()} | {:error, term()}

Wait until a JavaScript predicate becomes truthy in a frame.

opts may include :is_function, :arg, :polling (a number of ms or "raf"), and :timeout.