BrowserContext
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 creating isolated non-persistent browser contexts with Browser.newContext() method. Non-persistent browser contexts don't write any browsing data to disk.
// Create a new incognito browser context
BrowserContext context = browser.newContext();
// Create a new page inside context.
Page page = context.newPage();
page.navigate("https://example.com");
// Dispose context once it is no longer needed.
context.close();
Methods
addCookies
Added before v1.9Adds cookies into this browser context. All pages within this context will have these cookies installed. Cookies can be obtained via BrowserContext.cookies().
Usage
browserContext.addCookies(Arrays.asList(cookieObject1, cookieObject2));
Arguments
cookiesList<Cookie>#-
setNameString -
setValueString -
setUrlString (optional)Either
urlor bothdomainandpathare required. Optional. -
setDomainString (optional)For the cookie to apply to all subdomains as well, prefix domain with a dot, like this: ".example.com". Either
urlor bothdomainandpathare required. Optional. -
setPathString (optional)Either
urlor bothdomainandpathare required. Optional. -
setExpiresdouble (optional)Unix time in seconds. Optional.
-
setHttpOnlyboolean (optional)Optional.
-
setSecureboolean (optional)Optional.
-
setSameSiteenum SameSiteAttribute { STRICT, LAX, NONE }(optional)Optional.
-
setPartitionKeyString (optional)For partitioned third-party cookies (aka CHIPS), the partition key. Optional.
-
Returns
addInitScript
Added before v1.9Adds 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.
Usage
An example of overriding Math.random before the page loads:
// preload.js
Math.random = () => 42;
// In your playwright script, assuming the preload.js file is in same directory.
browserContext.addInitScript(Paths.get("preload.js"));
The order of evaluation of multiple scripts installed via BrowserContext.addInitScript() and Page.addInitScript() is not defined.
Arguments
Returns
- [Disposable]#
browser
Added before v1.9Gets the browser instance that owns the context. Returns null if the context is created outside of normal browser, e.g. Android or Electron.
Usage
BrowserContext.browser();
Returns
clearCookies
Added before v1.9Removes cookies from context. Accepts optional filter.
Usage
context.clearCookies();
context.clearCookies(new BrowserContext.ClearCookiesOptions().setName("session-id"));
context.clearCookies(new BrowserContext.ClearCookiesOptions().setDomain("my-origin.com"));
context.clearCookies(new BrowserContext.ClearCookiesOptions().setPath("/api/v1"));
context.clearCookies(new BrowserContext.ClearCookiesOptions()
.setName("session-id")
.setDomain("my-origin.com"));
Arguments
optionsBrowserContext.ClearCookiesOptions(optional)
Returns
clearPermissions
Added before v1.9Clears all permission overrides for the browser context.
Usage
BrowserContext context = browser.newContext();
context.grantPermissions(Arrays.asList("clipboard-read"));
// do stuff ..
context.clearPermissions();
Returns
close
Added before v1.9Closes the browser context. All the pages that belong to the browser context will be closed.
The default browser context cannot be closed.
Usage
BrowserContext.close();
BrowserContext.close(options);
Arguments
optionsBrowserContext.CloseOptions(optional)
Returns
cookies
Added before v1.9If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs are returned.
Usage
BrowserContext.cookies();
BrowserContext.cookies(urls);
Arguments
Returns