Implementation:Microsoft Playwright Server Browser
| Knowledge Sources | |
|---|---|
| Domains | Server, Browser |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for defining the abstract base class for all browser implementations on the server side provided by the Playwright library.
Description
The `Browser` abstract class extends `SdkObject` and serves as the base for all browser engine implementations (Chromium, Firefox, WebKit). It defines the `BrowserOptions` type containing launch configuration (name, channel, artifact directories, browser process, proxy settings, protocol logger, etc.) and the `BrowserProcess` interface for managing the underlying browser process. The class provides abstract methods for context management (`newContext`, `contexts`, `isConnected`), version information (`version`), and lifecycle control. It emits `Context` and `Disconnected` events, handles download management with `Artifact` instances, and coordinates browser context creation with `validateBrowserContextOptions`.
Usage
Use Server Browser as the base class when implementing a new browser engine backend, or when working with browser instances generically across Chromium, Firefox, and WebKit.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/server/browser.ts
Signature
export interface BrowserProcess {
onclose?: ((exitCode: number | null, signal: string | null) => void);
process?: ChildProcess;
kill(): Promise<void>;
close(): Promise<void>;
}
export type BrowserOptions = {
name: string;
isChromium: boolean;
channel?: string;
artifactsDir: string;
downloadsPath: string;
tracesDir: string;
headful?: boolean;
persistent?: types.BrowserContextOptions;
browserProcess: BrowserProcess;
// ... additional options
};
export abstract class Browser extends SdkObject {
static Events: { Context: string; Disconnected: string };
readonly options: BrowserOptions;
abstract newContext(options: types.BrowserContextOptions): Promise<BrowserContext>;
abstract contexts(): BrowserContext[];
abstract isConnected(): boolean;
abstract version(): string;
}
Import
import { Browser } from '../server/browser';
import type { BrowserOptions, BrowserProcess } from '../server/browser';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| parent | SdkObject | Yes | Parent SDK object for instrumentation hierarchy |
| options | BrowserOptions | Yes | Full browser configuration including process, paths, and logging |
Outputs
| Name | Type | Description |
|---|---|---|
| context | BrowserContext | Newly created browser context |
| contexts | BrowserContext[] | All active browser contexts |
| version | string | Browser version string |
Usage Examples
import { Browser } from '../server/browser';
// Browser is abstract; use via subclasses like CRBrowser, BidiBrowser, WKBrowser
const context = await browser.newContext({
viewport: { width: 1280, height: 720 },
userAgent: 'custom-agent',
});
const contexts = browser.contexts();
console.log('Browser version:', browser.version());