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 "incognito" browser contexts with Browser.newContext() method. "Incognito" 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 in: v1.8Adds 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
-
-
setName
String -
setValue
String -
setUrl
String (optional)either url or domain / path are required. Optional.
-
setDomain
String (optional)either url or domain / path are required Optional.
-
setPath
String (optional)either url or domain / path are required Optional.
-
setExpires
double (optional)Unix time in seconds. Optional.
-
setHttpOnly
boolean (optional)Optional.
-
setSecure
boolean (optional)Optional.
-
setSameSite
enum SameSiteAttribute { STRICT, LAX, NONE }
(optional)Optional.
Adds cookies to the browser context.
For the cookie to apply to all subdomains as well, prefix domain with a dot, like this: ".example.com".
-
Returns
addInitScript
Added in: v1.8Adds 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
backgroundPages
Added in: v1.11Background pages are only supported on Chromium-based browsers.
All existing background pages in the context.
Usage
BrowserContext.backgroundPages();
Returns
browser
Added in: v1.8Returns the browser instance of the context. If it was launched as a persistent context null gets returned.
Usage
BrowserContext.browser();
Returns
clearCookies
Added in: v1.8Removes 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
options
BrowserContext.ClearCookiesOptions
(optional)
Returns
clearPermissions
Added in: v1.8Clears 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 in: v1.8Closes 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
options
BrowserContext.CloseOptions
(optional)
Returns
cookies
Added in: v1.8If 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