Skip to main content

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.9 browserContext.addCookies

Adds 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

  • cookies List<Cookie>#
    • setName String

    • setValue String

    • setUrl String (optional)

      Either url or both domain and path are required. Optional.

    • setDomain String (optional)

      For the cookie to apply to all subdomains as well, prefix domain with a dot, like this: ".example.com". Either url or both domain and path are required. Optional.

    • setPath String (optional)

      Either url or both domain and 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.

    • setPartitionKey String (optional)

      For partitioned third-party cookies (aka CHIPS), the partition key. Optional.

Returns


addInitScript​

Added before v1.9 browserContext.addInitScript

Adds 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"));
note

The order of evaluation of multiple scripts installed via BrowserContext.addInitScript() and Page.addInitScript() is not defined.

Arguments

  • script String | Path#

    Script to be evaluated in all pages in the browser context.

Returns

  • [Disposable]#

browser​

Added before v1.9 browserContext.browser

Gets 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.9 browserContext.clearCookies

Removes 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)
    • setDomain String | Pattern (optional) Added in: v1.43#

      Only removes cookies with the given domain.

    • setName String | Pattern (optional) Added in: v1.43#

      Only removes cookies with the given name.

    • setPath String | Pattern (optional) Added in: v1.43#

      Only removes cookies with the given path.

Returns


clearPermissions​

Added before v1.9 browserContext.clearPermissions

Clears 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.9 browserContext.close

Closes the browser context. All the pages that belong to the browser context will be closed.

note

The default browser context cannot be closed.

Usage

BrowserContext.close();
BrowserContext.close(options);

Arguments

  • options BrowserContext.CloseOptions (optional)
    • setReason String (optional) Added in: v1.40#

      The reason to be reported to the operations interrupted by the context closure.

Returns


cookies​

Added before v1.9 browserContext.cookies

If 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