View Source Playwright.BrowserType (playwright v1.44.0-alpha.4)
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 instance:
{session, browser} =
Playwright.BrowserType.connect("ws://localhost:3000/")
Summary
Types
Options for connect/2
A map/struct providing call options
%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.SDK.ChannelOwner.init/2
.
Launches a new browser instance via the Playwright driver CLI.
Types
@type client() :: :chromium | :firefox | :webkit
The web client type used for launch/1
and connect/2
functions.
@type connect_options() :: map()
Options for connect/2
@type options() :: map()
A map/struct providing call options
@type t() :: %Playwright.BrowserType{ guid: term(), initializer: term(), listeners: term(), parent: term(), session: term(), type: term() }
%Playwright.BrowserType{}
@type url() :: String.t()
A string URL
@type ws_endpoint() :: url()
A websocket endpoint (URL)
Functions
@spec connect(ws_endpoint(), connect_options()) :: {pid(), Playwright.Browser.t()}
Attaches Playwright to an existing browser instance over a websocket.
Returns
{session, %Playwright.Browser{}}
Arguments
key/name | type | description | |
---|---|---|---|
ws_endpoint | param | BrowserType.ws_endpoint() | A browser websocket endpoint to connect to. |
:headers | option | map() | Additional HTTP headers to be sent with websocket connect request |
:slow_mow | option | integer() | Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. (default: 0) |
:logger | option | Logger sink for Playwright logging | |
:timeout | option | integer() | Maximum time in milliseconds to wait for the connection to be established. Pass 0 to disable timeout. (default: 30_000 (30 seconds)) |
Optional callback implementation for Playwright.SDK.ChannelOwner.init/2
.
If implemented, the callback will receive:
- The newly created "channel owner" struct.
- 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.warning("Closing #{inspect(event.target)}")
end)
{:ok, %{owner | version: "1.2.3"}}
end
Returns
{:ok, struct()}
Arguments
key/name | type | description | |
---|---|---|---|
owner | param | struct() | The newly created channel owner (resource). |
initializer | param | struct() | The initializer received from with the channel owner instance was derived. |
@spec 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:
{:ok, 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.