The CDPSession instances are used to talk raw Chrome Devtools Protocol:
- protocol methods can be called with
Playwright.CDPSession.send/4method. - protocol events can be subscribed to with
Playwright.CDPSession.on/3method.
Useful links:
- Documentation on DevTools Protocol can be found here: DevTools Protocol Viewer.
- Getting Started with DevTools Protocol: https://github.com/aslushnikov/getting-started-with-cdp/blob/master/README.md
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.
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.
Since Playwright 1.8.
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
@type event() :: :close | :event
An event name, see the module docs.
@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.
@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
@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.
@spec detach!(t()) :: :ok
Like detach/1, but returns the value and raises on error.
@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
:signal(Playwright.AbortSignal.t/0) - Since Playwright 1.65. Allows to cancel the operation using anAbortSignal. If the signal is aborted, the operation will be aborted and fail.In Elixir, a
Playwright.AbortSignal(Playwright.AbortController.signal/1).:timeout(number/0|:infinity) - Since Playwright 1.65. Maximum time in milliseconds to wait for the response. Defaults to0- no timeout.In Elixir,
:infinityis the same as0: no timeout.
@spec send!(t(), String.t(), map() | nil, send_options()) :: map()
Like send/4, but returns the value and raises on error.
Events
@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.
@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.
@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.
@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.
@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.
@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.