module Playwright::BrowserContext

Overview

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.

Defined in:

playwright/browsercontext.cr

Instance Method Summary

Instance Method Detail

abstract def 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]).


[View source]
abstract def 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.


[View source]
def add_init_script(script : String) : Nil #

[View source]
abstract def add_listener(type : EventType, listener : Listener(EventType)) #

[View source]
abstract def browser : Browser | Nil #

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


[View source]
abstract def clear_cookies : Nil #

Clears context cookies.


[View source]
abstract def clear_permissions : Nil #

Clears all permission overrides for the browser context.


[View source]
abstract def close : Nil #

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

NOTE the default browser context cannot be closed.


[View source]
def cookies(url : String) #

[View source]
abstract def cookies(url : Array(String)) : Array(Cookie) #

If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs are returned.


[View source]
def cookies #

[View source]
abstract def 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:


[View source]
def expose_binding(name : String, playwright_binding : Page::Binding) : Nil #

[View source]
abstract def 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:


[View source]
abstract def 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.


[View source]
def grant_permissions(permissions : Array(String)) : Nil #

[View source]
abstract def new_page : Page #

Creates a new page in the browser context.


[View source]
abstract def pages : Array(Page) #

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().


[View source]
abstract def remove_listener(type : EventType, listener : Listener(EventType)) #

[View source]
abstract def route(url : String, handler : Consumer(Route)) #

[View source]
abstract def route(url : Regex, handler : Consumer(Route)) #

[View source]
abstract def 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.


[View source]
abstract def 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).


[View source]
abstract def 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).


[View source]
abstract def 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.


[View source]
abstract def 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.


[View source]
abstract def set_offline(offline : Bool) : Nil #

[View source]
abstract def storage_state(options : StorageStateOptions | Nil) : StorageState #

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


[View source]
def storage_state : StorageState #

[View source]
abstract def unroute(url : String, handler : Consumer(Route) | Nil) #

[View source]
abstract def unroute(url : Regex, handler : Consumer(Route) | Nil) #

[View source]
abstract def 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.


[View source]
def unroute(url : String) #

[View source]
def unroute(url : Regex) #

[View source]
def unroute(url : String -> Bool) #

[View source]
def wait_for_event(event : EventType, predicate : Event(EventType) -> Bool) : Deferred(Event(EventType)) #

[View source]
abstract def 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.


[View source]
def wait_for_event(event : EventType) : Deferred(Event(EventType)) #

[View source]