Skip to main content
BrowserContext provides isolated sessions within a browser. Each context has its own cookies, cache, and storage.

Inheritance

Extends: ChannelOwner Implements: EventEmitter

Events

  • page: Emitted when a new page is created
  • close: Emitted when context is closed
  • request: Emitted when a request is made
  • response: Emitted when a response is received
  • requestfailed: Emitted when a request fails
  • requestfinished: Emitted when a request finishes
  • console: Emitted when console message is logged
  • dialog: Emitted when a dialog appears
  • weberror: Emitted when uncaught exception occurs
  • serviceworker: Emitted when service worker is created

Properties

request

APIRequestContext
API request context for making HTTP requests.

tracing

Tracing
Tracing instance for recording traces.

clock

Clock
Clock instance for controlling time.

Methods

browser

Returns the browser that owns this context.
Returns: Browser | null - Parent browser or null for persistent contexts

pages

Returns all open pages in this context.
Returns: Page[] - Array of pages

newPage

Creates a new page in this context.
Returns: Promise<Page> - New page

cookies

Returns cookies.
string | string[]
URL(s) to get cookies for. If not specified, returns all cookies.
Returns: Promise<Cookie[]> - Array of cookies

addCookies

Adds cookies to the context.
Cookie[]
required
Array of cookie objects to addEach cookie should have:
  • name (string): Cookie name
  • value (string): Cookie value
  • url or domain (string): Cookie domain
  • path (string, optional): Cookie path
  • expires (number, optional): Expiration timestamp
  • httpOnly (boolean, optional): HTTP only flag
  • secure (boolean, optional): Secure flag
  • sameSite (‘Strict’ | ‘Lax’ | ‘None’, optional): SameSite attribute
Returns: Promise<void>

clearCookies

Clears cookies.
ClearCookiesOptions
Cookie filter options
  • name (string | RegExp): Cookie name or pattern
  • domain (string | RegExp): Cookie domain or pattern
  • path (string | RegExp): Cookie path or pattern
Returns: Promise<void>

setDefaultNavigationTimeout

Sets default navigation timeout.
number
required
Timeout in milliseconds (0 to disable)

setDefaultTimeout

Sets default timeout for all operations.
number
required
Timeout in milliseconds (0 to disable)

grantPermissions

Grants specified permissions.
string[]
required
Permissions to grant (e.g., ‘geolocation’, ‘notifications’)
object
  • origin (string): Origin to grant permissions to
Returns: Promise<void>

clearPermissions

Clears all granted permissions.
Returns: Promise<void>

setGeolocation

Sets geolocation.
object | null
required
Geolocation coordinates or null to clear
  • latitude (number): Latitude
  • longitude (number): Longitude
  • accuracy (number, optional): Accuracy in meters
Returns: Promise<void>

setExtraHTTPHeaders

Sets extra HTTP headers.
Headers
required
HTTP headers object
Returns: Promise<void>

setOffline

Sets offline mode.
boolean
required
Whether to enable offline mode
Returns: Promise<void>

setHTTPCredentials

Sets HTTP credentials for authentication.
object | null
required
Credentials or null to clear
  • username (string): Username
  • password (string): Password
Returns: Promise<void>

addInitScript

Adds a script to evaluate in every page.
Function | string | object
required
Script to evaluate
any
Argument to pass to the script
Returns: Promise<void>

exposeBinding

Exposes a binding.
string
required
Binding name
Function
required
Callback function
object
  • handle (boolean): Pass JSHandle instead of value
Returns: Promise<void>

exposeFunction

Exposes a function.
string
required
Function name
Function
required
Function to expose
Returns: Promise<void>

route

Routes matching URLs to a handler.
string | RegExp
required
URL pattern to match
Function
required
Route handler function
object
  • times (number): Maximum number of times to handle
Returns: Promise<void>

unroute

Removes a route.
string | RegExp
required
URL pattern
Function
Handler to remove (removes all if not specified)
Returns: Promise<void>

unrouteAll

Removes all routes.
object
  • behavior (string): How to handle pending routes
Returns: Promise<void>

routeFromHAR

Routes requests from a HAR file.
string
required
Path to HAR file
RouteFromHAROptions
HAR routing options
  • url (string | RegExp): URL pattern to match
  • notFound (‘abort’ | ‘fallback’): Action when request not found
  • update (boolean): Update HAR file with new requests
Returns: Promise<void>

storageState

Returns storage state.
object
  • path (string): File path to save storage state to
Returns: Promise<StorageState> - Storage state object

waitForEvent

Waits for an event.
string
required
Event name
WaitForEventOptions | Function
Options or predicate function
Returns: Promise<any> - Event data

close

Closes the context.
object
  • reason (string): Close reason
Returns: Promise<void>

newCDPSession

Creates a CDP session.
Page | Frame
required
Page or frame to attach to
Returns: Promise<CDPSession> - CDP session

serviceWorkers

Returns all service workers.
Returns: Worker[] - Array of service workers

Usage Examples

Basic Context Usage

Request Interception

Storage State Persistence