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

A Playwright.Browser instance is createed via:

An example of using a Playwright.Browser to create a Playwright.Page:

alias Playwright.{Browser, Page}

browser = Playwright.launch(:chromium)
page = Browser.new_page(browser)

Page.goto(page, "https://example.com")
Browser.close(browser)

Properties

  • :name
  • :version

Link to this section Summary

Types

Supported events

A map/struct providing call options

t()

%Playwright.Browser{}

Functions

Closes the browser.

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.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{
  guid: term(),
  initializer: term(),
  listeners: term(),
  name: term(),
  parent: term(),
  session: term(),
  type: term(),
  version: term()
}

%Playwright.Browser{}

Link to this section 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

Specs

contexts(t()) :: [Playwright.BrowserContext.t()]

Returns an array 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
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

new_context(browser, options \\ %{})

View Source

Specs

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/nametypedescription
accept_downloadsoptionboolean()Whether to automatically download all the attachments. If false, all the downloads are canceled. (default: false)
...option......
Link to this function

new_page(browser, options \\ %{})

View Source

Specs

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.

Specs

version(t()) :: term()

Returns the browser version