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.crInstance Method Summary
-
#add_cookies(cookies : Array(AddCookie))
Adds cookies into this browser context.
-
#add_init_script(script : String, arg : Any) : Nil
Adds a script which would be evaluated in one of the following scenarios:
- #add_init_script(script : String) : Nil
- #add_listener(type : EventType, listener : Listener(EventType))
-
#browser : Browser | Nil
Returns the browser instance of the context.
-
#clear_cookies : Nil
Clears context cookies.
-
#clear_permissions : Nil
Clears all permission overrides for the browser context.
-
#close : Nil
Closes the browser context.
- #cookies(url : String)
-
#cookies(url : Array(String)) : Array(Cookie)
If no URLs are specified, this method returns all cookies.
- #cookies
-
#expose_binding(name : String, playwright_binding : Page::Binding, options : ExposeBindingOptions | Nil) : Nil
The method adds a function called
name
on thewindow
object of every frame in every page in the context. - #expose_binding(name : String, playwright_binding : Page::Binding) : Nil
-
#expose_function(name : String, playwright_function : Page::Function) : Nil
The method adds a function called
name
on thewindow
object of every frame in every page in the context. -
#grant_permissions(permissions : Array(String), options : GrantPermissionsOptions | Nil) : Nil
Grants specified permissions to the browser context.
- #grant_permissions(permissions : Array(String)) : Nil
-
#new_page : Page
Creates a new page in the browser context.
-
#pages : Array(Page)
Returns all open pages in the context.
- #remove_listener(type : EventType, listener : Listener(EventType))
- #route(url : String, handler : Consumer(Route))
- #route(url : Regex, handler : Consumer(Route))
-
#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.
-
#set_default_navigation_timeout(timeout : Int32) : Nil
This setting will change the default maximum navigation time for the following methods and related shortcuts:
-
#set_default_timeout(timeout : Int32) : Nil
This setting will change the default maximum time for all the methods accepting
timeout
option. -
#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.
-
#set_geolocation(geolocation : Geolocation | Nil) : Nil
Sets the context's geolocation.
- #set_offline(offline : Bool) : Nil
-
#storage_state(options : StorageStateOptions | Nil) : StorageState
Returns storage state for this browser context, contains current cookies and local storage snapshot.
- #storage_state : StorageState
- #unroute(url : String, handler : Consumer(Route) | Nil)
- #unroute(url : Regex, handler : Consumer(Route) | Nil)
-
#unroute(url : String -> Bool, handler : Consumer(Route) | Nil)
Removes a route created with
browserContext.route(url, handler)
. - #unroute(url : String)
- #unroute(url : Regex)
- #unroute(url : String -> Bool)
- #wait_for_event(event : EventType, predicate : Event(EventType) -> Bool) : Deferred(Event(EventType))
-
#wait_for_event(event : EventType, options : WaitForEventOptions | Nil) : Deferred(Event(EventType))
Waits for event to fire and passes its value into the predicate function.
- #wait_for_event(event : EventType) : Deferred(Event(EventType))
Instance Method Detail
Adds cookies into this browser context. All pages within this context will have these cookies installed. Cookies can be obtained via browserContext.cookies([urls])
.
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.
Returns the browser instance of the context. If it was launched as a persistent context null gets returned.
Closes the browser context. All the pages that belong to the browser context will be closed.
NOTE the default browser context cannot be closed.
If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs are returned.
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:
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:
Grants specified permissions to the browser context. Only grants corresponding permissions to the given origin if specified.
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()
.
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.
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)
.
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.
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.
Returns storage state for this browser context, contains current cookies and local storage snapshot.
Removes a route created with browserContext.route(url, handler)
. When handler
is not specified, removes all routes for the url
.
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.