Implementation:Microsoft Playwright CrNetworkManager
| Knowledge Sources | |
|---|---|
| Domains | Network Interception, Chrome DevTools Protocol |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for managing Chromium network requests via CDP provided by the Playwright library.
Description
The `CRNetworkManager` class handles network event processing and request interception for Chromium browsers using the Chrome DevTools Protocol (CDP). It manages multiple CDP sessions (for OOPIF support), tracks in-flight requests via `InterceptableRequest` objects, and handles the complex interplay between `Network.requestWillBeSent` and `Fetch.requestPaused` events. The class supports request interception for route handling, HTTP authentication, offline mode, extra HTTP headers, and response extra info tracking via `ResponseExtraInfoTracker`. It correctly handles race conditions between network events and fetch events, and manages credentials with origin-based authentication.
Usage
Use this class internally when the Chromium backend needs to track, intercept, or modify network requests. It is instantiated by `CRPage` and `CRServiceWorker` to provide the network layer for CDP-based browser automation.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/chromium/crNetworkManager.ts
Signature
export class CRNetworkManager {
private _page: Page | null;
private _serviceWorker: CRServiceWorker | null;
private _requestIdToRequest: Map<string, InterceptableRequest>;
private _credentials: { origin?: string, username: string, password: string } | null;
private _userRequestInterceptionEnabled: boolean;
private _protocolRequestInterceptionEnabled: boolean;
private _offline: boolean;
private _extraHTTPHeaders: types.HeadersArray;
constructor(page: Page | null, serviceWorker: CRServiceWorker | null);
async addSession(session: CRSession, isMain?: boolean, workerFrame?: frames.Frame): Promise<void>;
async removeSession(session: CRSession): Promise<void>;
async authenticate(credentials: { origin?: string, username: string, password: string } | null): Promise<void>;
async setOffline(offline: boolean): Promise<void>;
async setExtraHTTPHeaders(headers: types.HeadersArray): Promise<void>;
async setRequestInterception(value: boolean): Promise<void>;
}
Import
import { CRNetworkManager } from '../server/chromium/crNetworkManager';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| page | Page or null | Yes | The page instance (null for service worker contexts) |
| serviceWorker | CRServiceWorker or null | Yes | The service worker instance (null for page contexts) |
| session | CRSession | Yes | CDP session to add for network event monitoring |
| credentials | object | No | HTTP authentication credentials with optional origin |
| headers | types.HeadersArray | No | Extra HTTP headers to inject into requests |
| offline | boolean | No | Whether to emulate offline network mode |
Outputs
| Name | Type | Description |
|---|---|---|
| network.Request | network.Request | Playwright request objects emitted for tracked requests |
| network.Response | network.Response | Playwright response objects with header and body data |
Usage Examples
// Internal usage within CRPage
const networkManager = new CRNetworkManager(page, null);
// Add CDP session for network tracking
await networkManager.addSession(crSession, true);
// Configure network
await networkManager.setRequestInterception(true);
await networkManager.setExtraHTTPHeaders([{ name: 'X-Custom', value: 'test' }]);
await networkManager.authenticate({ username: 'user', password: 'pass' });
await networkManager.setOffline(true);