module

Playwright::BrowserContext

BrowserContexts provide a way to operate multiple independent browser sessions. If a page opens another page, e.g. with a window.open call, the popup will belong to the parent page's browser context. Playwright allows creation of "incognito" browser contexts with browser.newContext() method. "Incognito" browser contexts don't write any browsing data to disk.

Instance methods

add_cookies(cookies : Array(AddCookie))

Adds cookies into this browser context. All pages within this context will have these cookies installed. Cookies can be obtained via browserContext.cookies([urls]).

Source
add_init_script(script : String, arg : Any) : Nil

Adds a script which would be evaluated in one of the following scenarios:

Whenever a page is created in the browser context or is navigated. Whenever a child frame is attached or navigated in any page in the browser context. In this case, the script is evaluated in the context of the newly attached frame.

The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend the JavaScript environment, e.g. to seed Math.random. An example of overriding Math.random before the page loads:

NOTE The order of evaluation of multiple scripts installed via browserContext.addInitScript(script[, arg]) and page.addInitScript(script[, arg]) is not defined.

Source
add_init_script(script : String) : Nil
Source
add_listener(type : EventType, listener : Listener(EventType))
Source
browser

Returns the browser instance of the context. If it was launched as a persistent context null gets returned.

Source
clear_cookies

Clears context cookies.

Source
clear_permissions

Clears all permission overrides for the browser context.

Source
close

Closes the browser context. All the pages that belong to the browser context will be closed.

NOTE the default browser context cannot be closed.

Source
cookies(url : String)
Source
cookies
Source
expose_binding(name : String, playwright_binding : Page::Binding, options : ExposeBindingOptions | Nil) : Nil

The method adds a function called name on the window object of every frame in every page in the context. When called, the function executes playwrightBinding and returns a Promise which resolves to the return value of playwrightBinding. If the playwrightBinding returns a Promise, it will be awaited. The first argument of the playwrightBinding function contains information about the caller: { browserContext: BrowserContext, page: Page, frame: Frame }. See page.exposeBinding(name, playwrightBinding[, options]) for page-only version. An example of exposing page URL to all frames in all pages in the context:

An example of passing an element handle:

Source
expose_binding(name : String, playwright_binding : Page::Binding) : Nil
Source
expose_function(name : String, playwright_function : Page::Function) : Nil

The method adds a function called name on the window object of every frame in every page in the context. When called, the function executes playwrightFunction and returns a Promise which resolves to the return value of playwrightFunction. If the playwrightFunction returns a Promise, it will be awaited. See page.exposeFunction(name, playwrightFunction) for page-only version. An example of adding an md5 function to all pages in the context:

Source
grant_permissions(permissions : Array(String), options : GrantPermissionsOptions | Nil) : Nil

Grants specified permissions to the browser context. Only grants corresponding permissions to the given origin if specified.

Source
grant_permissions(permissions : Array(String)) : Nil
Source
new_page

Creates a new page in the browser context.

Source
pages

Returns all open pages in the context. Non visible pages, such as "background_page", will not be listed here. You can find them using chromiumBrowserContext.backgroundPages().

Source
remove_listener(type : EventType, listener : Listener(EventType))
Source
route(url : String, handler : Consumer(Route))
Source
route(url : Regex, handler : Consumer(Route))
Source
route(url : String -> Bool, handler : Consumer(Route))

Routing provides the capability to modify network requests that are made by any page in the browser context. Once route is enabled, every request matching the url pattern will stall unless it's continued, fulfilled or aborted. An example of a naïve handler that aborts all image requests:

or the same snippet using a regex pattern instead:

Page routes (set up with page.route(url, handler)) take precedence over browser context routes when request matches both handlers.

NOTE Enabling routing disables http cache.

Source
set_default_navigation_timeout(timeout : Int32) : Nil

This setting will change the default maximum navigation time for the following methods and related shortcuts:

page.goBack([options]) page.goForward([options]) page.goto(url[, options]) page.reload([options]) page.setContent(html[, options]) page.waitForNavigation([options])

NOTE page.setDefaultNavigationTimeout(timeout) and page.setDefaultTimeout(timeout) take priority over browserContext.setDefaultNavigationTimeout(timeout).

Source
set_default_timeout(timeout : Int32) : Nil

This setting will change the default maximum time for all the methods accepting timeout option.

NOTE page.setDefaultNavigationTimeout(timeout), page.setDefaultTimeout(timeout) and browserContext.setDefaultNavigationTimeout(timeout) take priority over browserContext.setDefaultTimeout(timeout).

Source
set_extra_http_headers(headers : Hash(String, String)) : Nil

The extra HTTP headers will be sent with every request initiated by any page in the context. These headers are merged with page-specific extra HTTP headers set with page.setExtraHTTPHeaders(headers). If page overrides a particular header, page-specific header value will be used instead of the browser context header value.

NOTE browserContext.setExtraHTTPHeaders does not guarantee the order of headers in the outgoing requests.

Source
set_geolocation(geolocation : Geolocation | Nil) : Nil

Sets the context's geolocation. Passing null or undefined emulates position unavailable.

NOTE Consider using browserContext.grantPermissions(permissions[, options]) to grant permissions for the browser context pages to read its geolocation.

Source
set_offline(offline : Bool) : Nil
Source
storage_state(options : StorageStateOptions | Nil) : StorageState

Returns storage state for this browser context, contains current cookies and local storage snapshot.

Source
storage_state
Source
unroute(url : String, handler : Consumer(Route) | Nil)
Source
unroute(url : Regex, handler : Consumer(Route) | Nil)
Source
unroute(url : String -> Bool, handler : Consumer(Route) | Nil)

Removes a route created with browserContext.route(url, handler). When handler is not specified, removes all routes for the url.

Source
unroute(url : String)
Source
unroute(url : Regex)
Source
unroute(url : String -> Bool)
Source
wait_for_event(event : EventType, predicate : Event(EventType) -> Bool) : Deferred(Event(EventType))
Source
wait_for_event(event : EventType, options : WaitForEventOptions | Nil) : Deferred(Event(EventType))

Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value. Will throw an error if the context closes before the event is fired. Returns the event data value.

Source
wait_for_event(event : EventType) : Deferred(Event(EventType))
Source

Nested types