Skip to main content
Context other methods for the Playwright driver.

browser

Returns the browser this context belongs to. Upstream docs
result
RemotePlaywrightBrowser | null
Return value
await context.browser();

serviceWorkers

Returns all service workers in the context.
Service workers are only available on Chromium-based browsers.
Upstream docs
result
Array<{ url: string; workerId: string }>
Return value
await context.serviceWorkers();

newCDPSession

Returns the newly created CDP session attached to the given page.
CDP Sessions are only supported on Chromium-based browsers.
Upstream docs
page
RemotePlaywrightPage
required
Target page to create CDP session for.
result
{ sessionId: string }
Return value
await context.newCDPSession(/* RemotePlaywrightPage */);

grantPermissions

Grants specified permissions to the browser context.
Only grants corresponding permissions to the given origin if specified.
Upstream docs
permissions
PlaywrightPermission[]
required
options
PlaywrightGrantPermissionsOptions
await context.grantPermissions(/* PlaywrightPermission[] */);

clearPermissions

Clears all permission overrides for the browser context. Upstream docs
await context.clearPermissions();

addInitScript

Adds a script which would be evaluated in one of the following scenarios: whenever a page is created in the context or navigated.
The script is evaluated before any page script. Use when you want to inject JavaScript before the page loads.
Upstream docs
script
string | { path?: string; content?: string }
required
arg
unknown
await context.addInitScript(/* string | { path?: string; content?: string } */);

exposeBinding

Adds a function called name on the window object of every frame in every page in the context.
When called, the function executes callback and returns a Promise which resolves to the return value of callback. The first argument of the callback function contains information about the caller.
Upstream docs
name
string
required
callback
string
required
options
{ handle?: boolean }
await context.exposeBinding('...', '...');

route

Routing provides the capability to modify network requests that are made by any page in the browser context.
Once routing is enabled, every request matching the url pattern will stall unless it is continued, fulfilled, or aborted.
Upstream docs
url
string | RegExp | ((url: URL) => boolean)
required
handler
string
required
options
PlaywrightRouteOptions
await context.route(/* string | RegExp | ((url: URL) => boolean) */, '...');

routeFromHAR

If specified, network requests that are made in the context will be served from the HAR file.
Read more about Replaying from HAR.
Upstream docs
har
string
required
Path to a HAR file with recorded network data.
options
PlaywrightRouteFromHAROptions
await context.routeFromHAR('...');

unroute

Removes a route created with browserContext.route(). When handler is not specified, removes all routes for the url. Upstream docs
url
string | RegExp | ((url: URL) => boolean)
required
handler
string
await context.unroute(/* string | RegExp | ((url: URL) => boolean) */);

unrouteAll

Removes all routes created with browserContext.route() and browserContext.routeFromHAR(). Upstream docs
options
PlaywrightUnrouteAllOptions
await context.unrouteAll();

setExtraHTTPHeaders

Sets extra HTTP headers that will be sent with every request in the context.
These headers are merged with page-specific extra HTTP headers set with page.setExtraHTTPHeaders(). If page overrides a particular header, page-specific header value will be used instead of the browser context header value.
Upstream docs
headers
Record<string, string>
required
await context.setExtraHTTPHeaders(/* Record<string, string> */);

setHTTPCredentials

Sets HTTP credentials for HTTP authentication.
Deprecated: Browsers may cache credentials after successful authentication. Create a new browser context instead.
Upstream docs
httpCredentials
PlaywrightHttpCredentials | null
required
await context.setHTTPCredentials(/* PlaywrightHttpCredentials | null */);

setOffline

Sets whether to emulate network being offline for the browser context. Upstream docs
offline
boolean
required
await context.setOffline(true);

setGeolocation

Sets the contexts geolocation. Passing null clears geolocation.
Requires the geolocation permission to be granted.
Upstream docs
geolocation
PlaywrightGeolocation | null
required
await context.setGeolocation(/* PlaywrightGeolocation | null */);

setDefaultNavigationTimeout

Sets the default maximum navigation timeout for this context.
This setting will change the default maximum navigation time for page.goto(), page.reload(), page.goBack(), page.goForward(), page.waitForNavigation().
Upstream docs
timeout
number
required
await context.setDefaultNavigationTimeout(0);

setDefaultTimeout

Sets the default maximum time for all methods accepting timeout option. Upstream docs
timeout
number
required
await context.setDefaultTimeout(0);

storageState

Returns storage state for this browser context.
Contains current cookies and local storage snapshot.
Upstream docs
options
{ path?: string }
result
PlaywrightStorageState
Return value
await context.storageState();

routeWebSocket

Allows modifying WebSocket connections made by pages in the context.
Only WebSockets created after this method was called will be routed. It is recommended to call this method before creating any pages.
Upstream docs
url
string | RegExp | ((url: URL) => boolean)
required
URL pattern to match WebSocket connections.
handler
string
required
Handler function to route the WebSocket.
await context.routeWebSocket(/* string | RegExp | ((url: URL) => boolean) */, '...');

backgroundPages

Returns all background pages in the context.
Deprecated. Background pages are only supported on Chromium-based browsers.
Upstream docs
result
RemotePlaywrightPage[]
Return value
await context.backgroundPages();