Playwright Browser Context
All Browser Context methods available when using the Playwright driver.
Access via runtime.browserContext after launching a Playwright runtime.
How these methods work over HTTP
Every method below is a remote call. The SDK translates it into a structured step sent to a single endpoint:
The call field maps directly to the method name. args is a JSON array of the method’s arguments. You can batch multiple steps in one request.
Waiting
waitForEvent(event, optionsOrPredicate?)
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 T
Cookies
addCookies(cookies)
Adds cookies into this browser context. All pages within this context will have these cookies installed.
cookies(urls?)
Returns cookies for the specified URLs, or all cookies if no URLs are specified.
Returns PlaywrightCookie[]
clearCookies(options?)
Removes cookies from context. Accepts optional filter to remove specific cookies.
If no options are provided, removes all cookies from the context.
Lifecycle
pages()
Returns all open pages in the context.
Returns RemotePlaywrightPage[]
newPage()
Creates a new page in the browser context.
Returns RemotePlaywrightPage
close(options?)
Closes the browser context. All the pages that belong to the browser context will be closed.
The default browser context cannot be closed.
Other
browser()
Returns the browser this context belongs to.
Returns RemotePlaywrightBrowser \| null
serviceWorkers()
Returns all service workers in the context.
Service workers are only available on Chromium-based browsers.
Returns Array<{ url: string; workerId: string }>
newCDPSession(page)
Returns the newly created CDP session attached to the given page.
CDP Sessions are only supported on Chromium-based browsers.
Returns { sessionId: string }
grantPermissions(permissions, options?)
Grants specified permissions to the browser context.
Only grants corresponding permissions to the given origin if specified.
clearPermissions()
Clears all permission overrides for the browser context.
addInitScript(script, arg?)
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.
exposeBinding(name, callback, options?)
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.
route(url, handler, options?)
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.
routeFromHAR(har, options?)
If specified, network requests that are made in the context will be served from the HAR file.
Read more about Replaying from HAR.
unroute(url, handler?)
Removes a route created with browserContext.route(). When handler is not specified, removes all routes for the url.
unrouteAll(options?)
Removes all routes created with browserContext.route() and browserContext.routeFromHAR().
setExtraHTTPHeaders(headers)
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.
setHTTPCredentials(httpCredentials)
Sets HTTP credentials for HTTP authentication.
Deprecated: Browsers may cache credentials after successful authentication. Create a new browser context instead.
setOffline(offline)
Sets whether to emulate network being offline for the browser context.
setGeolocation(geolocation)
Sets the contexts geolocation. Passing null clears geolocation.
Requires the geolocation permission to be granted.
setDefaultNavigationTimeout(timeout)
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().
setDefaultTimeout(timeout)
Sets the default maximum time for all methods accepting timeout option.
storageState(options?)
Returns storage state for this browser context.
Contains current cookies and local storage snapshot.
Returns PlaywrightStorageState
routeWebSocket(url, handler)
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.
backgroundPages()
Returns all background pages in the context.
Deprecated. Background pages are only supported on Chromium-based browsers.
Returns RemotePlaywrightPage[]

