Playwright.CDPSession (Playwright v1.65.0-rc.3)

Copy Markdown View Source

The CDPSession instances are used to talk raw Chrome Devtools Protocol:

Useful links:

client = Page.context(page) |> BrowserContext.new_cdp_session!(page)
CDPSession.send!(client, "Animation.enable")
CDPSession.on(client, "Animation.animationCreated", fn _ -> IO.puts("Animation created!") end)
response = CDPSession.send!(client, "Animation.getPlaybackRate")
IO.puts("playback rate is #{response["playbackRate"]}")
CDPSession.send!(client, "Animation.setPlaybackRate", %{playbackRate: response["playbackRate"] / 2})

Since Playwright 1.8.

The examples alias the modules they use (alias Playwright.{Locator, Page}).

Events

:close

Since Playwright 1.59. Emitted when the session is closed, either because the target was closed or Playwright.CDPSession.detach/1 was called.

:event

Since Playwright 1.59. Emitted for every CDP event received from the session. Allows subscribing to all CDP events at once without knowing their names ahead of time.

Usage

CDPSession.on(session, :event, fn event ->
  IO.puts("CDP event: #{event.method} #{inspect(event[:params])}")
end)

Summary

Types

An event name, see the module docs.

Options of send/4, see its docs.

t()

A CDPSession handle. Compare handles with ==, their fields are internal.

Functions

Detaches the CDPSession from the target. Once detached, the CDPSession object won't emit any events and can't be used to send messages.

Like detach/1, but returns the value and raises on error.

Like send/4, but returns the value and raises on error.

Events

Adds an event listener for event, see the module docs for the events.

Removes an event listener added by on/3 or add_listener/3.

Adds an event listener for event, see the module docs for the events.

Adds an event listener that is removed after it is triggered once. See on/3.

Adds an event listener for event, see the module docs for the events.

Removes an event listener added by on/3 or add_listener/3.

Types

event()

@type event() :: :close | :event

An event name, see the module docs.

send_options()

@type send_options() ::
  [signal: Playwright.AbortSignal.t(), timeout: number() | :infinity]
  | %{
      optional(:signal) => Playwright.AbortSignal.t(),
      optional(:timeout) => number() | :infinity
    }

Options of send/4, see its docs.

t()

@type t() :: %Playwright.CDPSession{
  conn: term(),
  guid: term(),
  initializer: term(),
  table: term(),
  type: term()
}

A CDPSession handle. Compare handles with ==, their fields are internal.

Functions

detach(cdp_session)

@spec detach(t()) :: :ok | {:error, Playwright.error()}

Detaches the CDPSession from the target. Once detached, the CDPSession object won't emit any events and can't be used to send messages.

Since Playwright 1.8.

detach!(cdp_session)

@spec detach!(t()) :: :ok

Like detach/1, but returns the value and raises on error.

send(cdp_session, method, params \\ nil, options \\ [])

@spec send(t(), String.t(), map() | nil, send_options()) ::
  {:ok, map()} | {:error, Playwright.error()}

Since Playwright 1.8.

Arguments

  • method - Protocol method name.
  • params - Optional method parameters.

Options

send!(cdp_session, method, params \\ nil, options \\ [])

@spec send!(t(), String.t(), map() | nil, send_options()) :: map()

Like send/4, but returns the value and raises on error.

Events

add_listener(cdp_session, event, listener)

@spec add_listener(t(), :close, (t() -> term())) :: t()
@spec add_listener(t(), :event, (%{:method => String.t(), optional(:params) => map()} ->
                             term())) :: t()
@spec add_listener(t(), String.t(), (term() -> term())) :: t()

Adds an event listener for event, see the module docs for the events.

off(cdp_session, event, listener)

@spec off(t(), :close, (t() -> term())) :: t()
@spec off(t(), :event, (%{:method => String.t(), optional(:params) => map()} ->
                    term())) :: t()
@spec off(t(), String.t(), (term() -> term())) :: t()

Removes an event listener added by on/3 or add_listener/3.

on(cdp_session, event, listener)

@spec on(t(), :close, (t() -> term())) :: t()
@spec on(t(), :event, (%{:method => String.t(), optional(:params) => map()} -> term())) ::
  t()
@spec on(t(), String.t(), (term() -> term())) :: t()

Adds an event listener for event, see the module docs for the events.

once(cdp_session, event, listener)

@spec once(t(), :close, (t() -> term())) :: t()
@spec once(t(), :event, (%{:method => String.t(), optional(:params) => map()} ->
                     term())) :: t()
@spec once(t(), String.t(), (term() -> term())) :: t()

Adds an event listener that is removed after it is triggered once. See on/3.

prepend_listener(cdp_session, event, listener)

@spec prepend_listener(t(), :close, (t() -> term())) :: t()
@spec prepend_listener(t(), :event, (%{
                                 :method => String.t(),
                                 optional(:params) => map()
                               } ->
                                 term())) ::
  t()
@spec prepend_listener(t(), String.t(), (term() -> term())) :: t()

Adds an event listener for event, see the module docs for the events.

remove_listener(cdp_session, event, listener)

@spec remove_listener(t(), :close, (t() -> term())) :: t()
@spec remove_listener(t(), :event, (%{
                                :method => String.t(),
                                optional(:params) => map()
                              } ->
                                term())) ::
  t()
@spec remove_listener(t(), String.t(), (term() -> term())) :: t()

Removes an event listener added by on/3 or add_listener/3.