Playwright Browser Context

View as Markdown

All Browser Context methods available when using the Playwright driver.

Access via runtime.browserContext after launching a Playwright runtime.

Every method below is a remote call. The SDK translates it into a structured step sent to a single endpoint:

POST /v1/workspaces/{workspaceId}/execute
1{
2 "runtime": "my-browser",
3 "steps": [
4 {
5 "call": "context.goto",
6 "args": ["https://example.com"]
7 }
8 ]
9}

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.

1await runtime.browserContext.waitForEvent('...');
ParameterTypeRequiredDescription
eventstringYes
optionsOrPredicatePlaywrightWaitForEventOptions | ((event: unknown) => boolean)No

Returns T

Upstream docs


Cookies

addCookies(cookies)

Adds cookies into this browser context. All pages within this context will have these cookies installed.

1await runtime.browserContext.addCookies(/* PlaywrightCookie[] */);
ParameterTypeRequiredDescription
cookiesPlaywrightCookie[]Yes

Upstream docs


cookies(urls?)

Returns cookies for the specified URLs, or all cookies if no URLs are specified.

1await runtime.browserContext.cookies();
ParameterTypeRequiredDescription
urlsstring | string[]No

Returns PlaywrightCookie[]

Upstream docs


clearCookies(options?)

Removes cookies from context. Accepts optional filter to remove specific cookies.

If no options are provided, removes all cookies from the context.

1await runtime.browserContext.clearCookies();
ParameterTypeRequiredDescription
optionsPlaywrightClearCookiesOptionsNo

Upstream docs


Lifecycle

pages()

Returns all open pages in the context.

1await runtime.browserContext.pages();

Returns RemotePlaywrightPage[]

Upstream docs


newPage()

Creates a new page in the browser context.

1await runtime.browserContext.newPage();

Returns RemotePlaywrightPage

Upstream docs


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.

1await runtime.browserContext.close();
ParameterTypeRequiredDescription
options{ reason?: string }No

Upstream docs


Other

browser()

Returns the browser this context belongs to.

1runtime.browserContext.browser();

Returns RemotePlaywrightBrowser \| null

Upstream docs


serviceWorkers()

Returns all service workers in the context.

Service workers are only available on Chromium-based browsers.

1await runtime.browserContext.serviceWorkers();

Returns Array<{ url: string; workerId: string }>

Upstream docs


newCDPSession(page)

Returns the newly created CDP session attached to the given page.

CDP Sessions are only supported on Chromium-based browsers.

1await runtime.browserContext.newCDPSession(/* RemotePlaywrightPage */);
ParameterTypeRequiredDescription
pageHasHandleIdYesTarget page to create CDP session for.

Returns { sessionId: string }

Upstream docs


grantPermissions(permissions, options?)

Grants specified permissions to the browser context.

Only grants corresponding permissions to the given origin if specified.

1await runtime.browserContext.grantPermissions(/* PlaywrightPermission[] */);
ParameterTypeRequiredDescription
permissionsPlaywrightPermission[]Yes
optionsPlaywrightGrantPermissionsOptionsNo

Upstream docs


clearPermissions()

Clears all permission overrides for the browser context.

1await runtime.browserContext.clearPermissions();

Upstream docs


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.

1await runtime.browserContext.addInitScript(/* script */);
ParameterTypeRequiredDescription
scriptFunction | string | { path?: string; content?: string }Yes
argunknownNo

Upstream docs


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.

1await runtime.browserContext.exposeBinding('...', '...');
ParameterTypeRequiredDescription
namestringYes
callbackFunctionYes
options{ handle?: boolean }No

Upstream docs


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.

1await runtime.browserContext.route(/* url */, '...');
ParameterTypeRequiredDescription
urlstring | RegExp | ((url: URL) => boolean)Yes
handlerFunctionYes
optionsPlaywrightRouteOptionsNo

Upstream docs


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.

1await runtime.browserContext.routeFromHAR('...');
ParameterTypeRequiredDescription
harstringYesPath to a HAR file with recorded network data.
optionsPlaywrightRouteFromHAROptionsNo

Upstream docs


unroute(url, handler?)

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

1await runtime.browserContext.unroute(/* url */);
ParameterTypeRequiredDescription
urlstring | RegExp | ((url: URL) => boolean)Yes
handlerFunctionNo

Upstream docs


unrouteAll(options?)

Removes all routes created with browserContext.route() and browserContext.routeFromHAR().

1await runtime.browserContext.unrouteAll();
ParameterTypeRequiredDescription
optionsPlaywrightUnrouteAllOptionsNo

Upstream docs


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.

1await runtime.browserContext.setExtraHTTPHeaders(/* Record<string, string> */);
ParameterTypeRequiredDescription
headersRecord&lt;string, string&gt;Yes

Upstream docs


setHTTPCredentials(httpCredentials)

Sets HTTP credentials for HTTP authentication.

Deprecated: Browsers may cache credentials after successful authentication. Create a new browser context instead.

1await runtime.browserContext.setHTTPCredentials(/* httpCredentials */);
ParameterTypeRequiredDescription
httpCredentialsPlaywrightHttpCredentials | nullYes

Upstream docs


setOffline(offline)

Sets whether to emulate network being offline for the browser context.

1await runtime.browserContext.setOffline(true);
ParameterTypeRequiredDescription
offlinebooleanYes

Upstream docs


setGeolocation(geolocation)

Sets the contexts geolocation. Passing null clears geolocation.

Requires the geolocation permission to be granted.

1await runtime.browserContext.setGeolocation(/* geolocation */);
ParameterTypeRequiredDescription
geolocationPlaywrightGeolocation | nullYes

Upstream docs


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

1await runtime.browserContext.setDefaultNavigationTimeout(0);
ParameterTypeRequiredDescription
timeoutnumberYes

Upstream docs


setDefaultTimeout(timeout)

Sets the default maximum time for all methods accepting timeout option.

1await runtime.browserContext.setDefaultTimeout(0);
ParameterTypeRequiredDescription
timeoutnumberYes

Upstream docs


storageState(options?)

Returns storage state for this browser context.

Contains current cookies and local storage snapshot.

1await runtime.browserContext.storageState();
ParameterTypeRequiredDescription
options{ path?: string }No

Returns PlaywrightStorageState

Upstream docs


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.

1await runtime.browserContext.routeWebSocket(/* url */, '...');
ParameterTypeRequiredDescription
urlstring | RegExp | ((url: URL) =&gt; boolean)YesURL pattern to match WebSocket connections.
handlerFunctionYesHandler function to route the WebSocket.

Upstream docs


backgroundPages()

Returns all background pages in the context.

Deprecated. Background pages are only supported on Chromium-based browsers.

1await runtime.browserContext.backgroundPages();

Returns RemotePlaywrightPage[]

Upstream docs