View Source Playwright.Browser (playwright v1.49.1-alpha.1)
A Playwright.Browser
instance is created 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)
page = Browser.new_page(browser)
Page.goto(page, "https://example.com")
Browser.close(browser)
Properties
:name
:version
Summary
Functions
Closes the browser.
Returns a list of all open browser contexts. In a newly created browser, this will return zero browser contexts.
Optional callback implementation for Playwright.SDK.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
Types
Functions
Closes the browser.
Given a Playwright.Browser
obtained from Playwright.BrowserType.launch/2
,
closes the Browser
and all of its Pages
(if any were opened).
Given a Playwright.Browser
obtained via Playwright.BrowserType.connect/2
,
clears all created Contexts
belonging to this Browser
and disconnects
from the browser server.
The Browser object itself is considered to be disposed and cannot be used anymore.
Returns
:ok
@spec contexts(t()) :: [Playwright.BrowserContext.t()]
Returns a list of all open browser contexts. In a newly created browser, this will return zero browser contexts.
Example
contexts = Browser.contexts(browser)
asset Enum.empty?(contexts)
Browser.new_context(browser)
contexts = Browser.contexts(browser)
assert length(contexts) == 1
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 new_context(t(), options()) :: 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.
context = Browser.new_context(browser)
# create a new page in a pristine context.
page = BrowserContext.new_page(context)
Page.goto(page, "https://example.com")
Returns
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 | ... | ... |
@spec new_page(t(), options()) :: 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.
Returns the browser version