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

A Playwright.Browser instance is created via:

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

Types

Supported events

A map/struct providing call options

t()

%Playwright.Browser{}

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.Page for this Browser, within a new "owned" Playwright.BrowserContext.

Returns the browser version

Types

@type event() :: :disconnected

Supported events

@type options() :: map()

A map/struct providing call options

@type t() :: %Playwright.Browser{
  guid: term(),
  initializer: term(),
  listeners: term(),
  name: term(),
  parent: term(),
  session: term(),
  type: term(),
  version: term()
}

%Playwright.Browser{}

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

init(owner, initializer)

View Source
@spec init(
  struct(),
  map()
) :: {atom(), struct()}

Optional callback implementation for Playwright.SDK.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.warning("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
@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/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
@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.

@spec version(t()) :: term()

Returns the browser version