Implementation:Microsoft Playwright BrowserContextDispatcher
| Knowledge Sources | |
|---|---|
| Domains | RPC Protocol, Browser Context Management |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for exposing BrowserContext operations over the RPC protocol provided by the Playwright library.
Description
The `BrowserContextDispatcher` class bridges the server-side `BrowserContext` to the client via the RPC dispatcher system. It implements the `channels.BrowserContextChannel` interface, handling all context-level operations including page creation, cookie management, route interception, permission granting, storage state serialization, tracing, and recorder integration. The class manages event subscriptions (allowing clients to opt-in to specific events like console, dialog, request, response), maintains binding and init script registrations, handles WebSocket interception patterns, dialog handling, and clock control. It creates `PageDispatcher` instances for new pages and translates between protocol types and server types.
Usage
Use this dispatcher when the Playwright server needs to expose browser context capabilities to remote clients. It is created automatically when a client creates or connects to a browser context.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts
Signature
export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channels.BrowserContextChannel, DispatcherScope> implements channels.BrowserContextChannel {
_type_EventTarget: true;
_type_BrowserContext: true;
private _context: BrowserContext;
private _subscriptions: Set<channels.BrowserContextUpdateSubscriptionParams['event']>;
constructor(parentScope: DispatcherScope, context: BrowserContext);
async newPage(params: channels.BrowserContextNewPageParams): Promise<channels.BrowserContextNewPageResult>;
async addCookies(params: channels.BrowserContextAddCookiesParams): Promise<void>;
async clearCookies(params: channels.BrowserContextClearCookiesParams): Promise<void>;
async setNetworkInterceptionPatterns(params: channels.BrowserContextSetNetworkInterceptionPatternsParams): Promise<void>;
async storageState(params: channels.BrowserContextStorageStateParams): Promise<channels.BrowserContextStorageStateResult>;
async close(params: channels.BrowserContextCloseParams): Promise<void>;
}
Import
import { BrowserContextDispatcher } from '../server/dispatchers/browserContextDispatcher';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| parentScope | DispatcherScope | Yes | Parent dispatcher scope for object hierarchy |
| context | BrowserContext | Yes | Server-side browser context to expose |
| params | various channel params | Yes | Method-specific parameters from the client |
Outputs
| Name | Type | Description |
|---|---|---|
| PageDispatcher | PageDispatcher | Dispatcher for newly created pages |
| storageState | channels.BrowserContextStorageStateResult | Serialized cookies and storage |
| events | channel events | Page, console, dialog, request, response events |
Usage Examples
// Created when a browser context is established
const contextDispatcher = new BrowserContextDispatcher(browserDispatcher, serverContext);
// Client calls are dispatched to the server context
const pageResult = await contextDispatcher.newPage({});
await contextDispatcher.addCookies({ cookies: [{ name: 'test', value: '1', url: 'https://example.com' }] });
const state = await contextDispatcher.storageState({});