Skip to main content
Version: 23.10.0

BrowserContext class

BrowserContext represents individual user contexts within a browser.

When a browser is launched, it has at least one default browser context. Others can be created using Browser.createBrowserContext(). Each context has isolated storage (cookies/localStorage/etc.)

BrowserContext emits various events which are documented in the BrowserContextEvent enum.

If a page opens another page, e.g. using window.open, the popup will belong to the parent page's browser context.

Signature

export declare abstract class BrowserContext extends EventEmitter<BrowserContextEvents>

Extends: EventEmitter<BrowserContextEvents>

Remarks

In Chrome all non-default contexts are incognito, and default browser context might be incognito if you provide the --incognito argument when launching the browser.

The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the BrowserContext class.

Example

Creating a new browser context:

// Create a new browser context
const context = await browser.createBrowserContext();
// Create a new page inside context.
const page = await context.newPage();
// ... do stuff with page ...
await page.goto('https://example.com');
// Dispose context once it's no longer needed.
await context.close();

Properties

Property

Modifiers

Type

Description

closed

readonly

boolean

Whether this browser context is closed.

id

readonly

string | undefined

Identifier for this browser context.

Methods

Method

Modifiers

Description

browser()

Gets the browser associated with this browser context.

clearPermissionOverrides()

Clears all permission overrides for this browser context.

close()

Closes this browser context and all associated pages.

Remarks:

The default browser context cannot be closed.

cookies()

Gets all cookies in the browser context.

deleteCookie(cookies)

Removes cookie in the browser context

newPage()

Creates a new page in this browser context.

overridePermissions(origin, permissions)

Grants this browser context the given permissions within the given origin.

pages()

Gets a list of all open pages inside this browser context.

Remarks:

Non-visible pages, such as "background_page", will not be listed here. You can find them using Target.page().

setCookie(cookies)

Sets a cookie in the browser context.

targets()

Gets all active targets inside this browser context.

waitForTarget(predicate, options)

Waits until a target matching the given predicate appears and returns it.

This will look all open browser contexts.