Playwright.BrowserType (playwright v1.18.0-alpha.1) View Source

Playwright.BrowserType provides functions to launch a specific browser instance or connect to an existing one.

The following is a typical example of using Playwright to drive automation:

alias Playwright.{Browser, BrowserType, Page}

{session, browser} = BrowserType.launch(:chromium)
page = Browser.new_page(browser)

Page.goto(page, "https://example.com")
# other actions...

Browser.close(browser)

Example

Open a new chromium via the CLI driver:

{session, browser} = Playwright.BrowserType.launch()

Connect to a running playwright instances:

{session, browser} =
  Playwright.BrowserType.connect("ws://localhost:3000/playwright")

Link to this section Summary

Types

The web client type used for launch/1 and connect/2 functions.

A map/struct providing call options

t()

%Playwright.BrowserType{}

A string URL

A websocket endpoint (URL)

Functions

Attaches Playwright to an existing browser instance over a websocket.

Optional callback implementation for Playwright.ChannelOwner.init/2.

Launches a new browser instance via the Playwright driver CLI.

Link to this section Types

Specs

client() :: :chromium | :firefox | :webkit

The web client type used for launch/1 and connect/2 functions.

Specs

connect_options() :: map()

Options for connect/2

Specs

options() :: map()

A map/struct providing call options

Specs

t() :: %Playwright.BrowserType{
  guid: term(),
  initializer: term(),
  listeners: term(),
  parent: term(),
  session: term(),
  type: term()
}

%Playwright.BrowserType{}

Specs

url() :: String.t()

A string URL

Specs

ws_endpoint() :: url()

A websocket endpoint (URL)

Link to this section Functions

Link to this function

connect(ws_endpoint, options \\ %{})

View Source

Specs

Attaches Playwright to an existing browser instance over a websocket.

Returns

  • {session, %Playwright.Browser{}}

Arguments

key/nametypedescription
ws_endpointparamBrowserType.ws_endpoint()A browser websocket endpoint to connect to.
:headersoptionmap()Additional HTTP headers to be sent with websocket connect request
:slow_mowoptioninteger()Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. (default: 0)
:loggeroptionLogger sink for Playwright logging
:timeoutoptioninteger()Maximum time in milliseconds to wait for the connection to be established. Pass 0 to disable timeout. (default: 30_000 (30 seconds))
Link to this function

init(owner, initializer)

View Source

Specs

init(
  struct(),
  map()
) :: {atom(), struct()}

Optional callback implementation for Playwright.ChannelOwner.init/2.

If implemented, the callback will receive:

  1. The newly created "channel owner" struct.
  2. The :initializer received from the Playwright browser server.

The implementation has the option of "patching" the struct as stored in the catalog, and/or binding event handlers.

Example

def init(%{session: session} = owner, _initializer) do
  Channel.bind(session, {:guid, owner.guid}, :close, fn event ->
    Logger.warn("Closing #{inspect(event.target)}")
  end)

  {:ok, %{owner | version: "1.2.3"}}
end

Returns

  • {:ok, struct()}

Arguments

key/nametypedescription
ownerparamstruct()The newly created channel owner (resource).
initializerparamstruct()The initializer received from with the channel owner instance was derived.
Link to this function

launch(client \\ nil, options \\ %{})

View Source

Specs

launch(client() | nil, any()) :: {pid(), Playwright.Browser.t()}

Launches a new browser instance via the Playwright driver CLI.

Example

# Use `:ignore_default_args` option to filter out `--mute-audio` from
# default arguments:
browser =
  Playwright.launch(:chromium, %{ignore_default_args = ["--mute-audio"]})

Returns

  • {session, %Playwright.Browser{}}

Arguments

... (many)

NOTE

Chromium-only Playwright can also be used to control the Google Chrome or Microsoft Edge browsers, but it works best with the version of Chromium it is bundled with. There is no guarantee it will work with any other version. Use :executable_path option with extreme caution.

If Google Chrome (rather than Chromium) is preferred, a Chrome Canary or Dev Channel build is suggested.

Stock browsers like Google Chrome and Microsoft Edge are suitable for tests that require proprietary media codecs for video playback. See this article for other differences between Chromium and Chrome. This article describes some differences for Linux users.