Playwright.Browser (playwright v0.1.17-preview-1) View Source
A Playwright.Browser
instance is createed via:
Playwright.BrowserType.launch/0
, when using the "driver" transport.Playwright.BrowserType.connect/1
, when using the "websocket" transport.
An example of using a Playwright.Browser
to create a Playwright.Page
:
alias Playwright.{Browser, Page}
{:ok, browser} = Playwright.launch(:chromium)
{:ok, page} = Browser.new_page(browser)
Page.goto(page, "https://example.com")
Browser.close(browser)
Properties
:name
:version
Link to this section Summary
Functions
Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.
Optional callback implementation for Playwright.ChannelOwner.init/2
.
Create a new Playwright.BrowserContext
for this Playwright.Browser
.
Create a new Playwright.Page
for this Browser, within a new "owned"
Playwright.BrowserContext
.
Returns the browser version
Link to this section Types
Specs
event() :: :disconnected
Supported events
Specs
options() :: map()
A map/struct providing call options
Specs
t() :: %Playwright.Browser{ connection: term(), guid: term(), initializer: term(), listeners: term(), name: term(), parent: term(), type: term(), version: term() }
%Playwright.Browser{}
Link to this section Functions
Specs
contexts(t()) :: {:ok, [Playwright.BrowserContext.t()]}
Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.
Example
{:ok, contexts} = Browser.contexts(browser)
asset Enum.empty?(contexts)
Browser.new_context(browser)
{:ok, contexts} = Browser.contexts(browser)
assert length(contexts) == 1
Specs
Optional callback implementation for Playwright.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(owner, _initializer) do
Channel.bind(owner, :close, fn event ->
Logger.warn("Closing #{inspect(event.target)}")
end)
{:ok, %{owner | version: "1.2.3"}}
end
Returns
{:ok, %{}}
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. |
Specs
new_context(t(), options()) :: {:ok, Playwright.BrowserContext.t()}
Create a new Playwright.BrowserContext
for this Playwright.Browser
.
A BrowserContext
does not share cookies/cache with other BrowserContexts
and is somewhat equivalent to an "incognito" browser "window".
Example
# create a new "incognito" browser context.
{:ok, context} = Browser.new_context(browser)
# create a new page in a pristine context.
{:ok, page} = BrowserContext.new_page(context)
Page.goto(page, "https://example.com")
Returns
{:ok, Playwright.BrowserContext.t()}
Arguments
key / name | type | description | |
---|---|---|---|
accept_downloads | option | boolean() | Whether to automatically download all the attachments. If false, all the downloads are canceled. (default: false) |
... | option | ... | ... |
Specs
new_page(t() | {:ok, t()}, options()) :: {:ok, Playwright.Page.t()}
Create a new Playwright.Page
for this Browser, within a new "owned"
Playwright.BrowserContext
.
That is, Playwright.Browser.new_page/2
will also create a new
Playwright.BrowserContext
. That BrowserContext
becomes, both, the
parent of the Page
, and owned by the Page
. When the Page
closes,
the context goes with it.
This is a convenience API function that should only be used for single-page
scenarios and short snippets. Production code and testing frameworks should
explicitly create via Playwright.Browser.new_context/2
followed by
Playwright.BrowserContext.new_page/2
, given the new context, to manage
resource lifecycles.
Specs
Returns the browser version