This document explains the Browser and BrowserContext APIs in Playwright, including browser launching via BrowserType, connection strategies (CDP and WebSocket), and the management of isolated browsing sessions. It covers the internal implementation of context-level features such as permissions, cookies, storage state, and geolocation.
Playwright uses a hierarchical model where a BrowserType launches a Browser instance, which contains one or more BrowserContext instances. Each context provides a completely isolated session, containing its own pages, cookies, and local storage.
Title: Playwright Object Hierarchy
Sources: packages/playwright-core/src/client/browserType.ts66-84 packages/playwright-core/src/client/browser.ts62-64 packages/playwright-core/src/client/browser.ts143-151
Browsers are managed by BrowserType implementations. Playwright provides specialized classes for Chromium, Firefox, and WebKit to handle engine-specific protocols and process launching.
Playwright supports launching local instances, connecting to remote instances via WebSocket, or connecting via the Chrome DevTools Protocol (CDP).
Title: BrowserType Implementation Space
Key APIs:
launch(): Starts a local browser process. It wraps the launch channel call and connects the resulting Browser object to the BrowserType instance packages/playwright-core/src/client/browserType.ts66-84connect(): Attaches to a remote PlaywrightServer via WebSocket. It utilizes connectToBrowser to establish the connection and wire up the client-side Browser object packages/playwright-core/src/client/browserType.ts124-139connectOverCDP(): Connects to an existing browser instance using the Chrome DevTools Protocol. This is primarily used for Chromium and supports managing downloads and traces via a local artifactsDir packages/playwright-core/src/client/browserType.ts141-158Sources: packages/playwright-core/src/client/browserType.ts66-84 packages/playwright-core/src/client/browserType.ts124-139 packages/playwright-core/src/client/browserType.ts141-158
The Browser class tracks active contexts and handles global browser events like disconnection. It serves as a ChannelOwner for the BrowserChannel packages/playwright-core/src/client/browser.ts33-56
| Property/Method | Description | File |
|---|---|---|
newContext() | Creates a new isolated session. | packages/playwright-core/src/client/browser.ts62-64 |
newPage() | Convenience method to create a context and a page in one call. | packages/playwright-core/src/client/browser.ts143-151 |
version() | Returns the browser version string. | packages/playwright-core/src/client/browser.ts130-132 |
isConnected() | Returns whether the browser is still connected to the server. | packages/playwright-core/src/client/browser.ts153-155 |
Sources: packages/playwright-core/src/client/browser.ts33-56 packages/playwright-core/src/client/browser.ts62-64 packages/playwright-core/src/client/browser.ts143-151
BrowserContext is the primary unit of isolation. It manages its own set of pages, cookies, and storage state.
Title: BrowserContext Internal Structure
Isolation boundaries:
context.cookies() and added via context.addCookies() tests/library/browsercontext-cookies.spec.ts20-41storageState. This is a core feature for authentication reuse docs/src/auth.md71-77Sources: packages/playwright-core/src/client/browser.ts81-94 docs/src/auth.md71-77 tests/library/browsercontext-cookies.spec.ts20-41
A "Persistent Context" uses a physical disk directory for the user profile, allowing state to persist across browser restarts.
browserType.launchPersistentContext(userDataDir, ...) packages/playwright-core/src/client/browserType.ts93-122userDataDir being mandatory packages/playwright-core/src/client/browserType.ts109-111Sources: packages/playwright-core/src/client/browserType.ts93-122
Remote connections are facilitated by the PlaywrightServer, which exposes a WebSocket endpoint for clients to connect to.
The server handles incoming connections and dispatches them to the appropriate internal controllers.
User-Agent validation packages/playwright-core/src/remote/playwrightServer.ts69-85Semaphore to enforce maxConnections packages/playwright-core/src/remote/playwrightConnection.ts34-50PlaywrightDispatcher packages/playwright-core/src/remote/playwrightConnection.ts52-80Sources: packages/playwright-core/src/remote/playwrightServer.ts65-92 packages/playwright-core/src/remote/playwrightConnection.ts34-80
When a client connects, the PlaywrightConnection initializes a RootDispatcher. This dispatcher sets up the PlaywrightDispatcher, which in turn manages browsers and contexts. If the transport closes, the connection performs a cleanup of all allocated resources, including stopping pending operations packages/playwright-core/src/remote/playwrightConnection.ts91-118 packages/playwright-core/src/remote/playwrightConnection.ts126-136
Sources: packages/playwright-core/src/remote/playwrightConnection.ts91-118 packages/playwright-core/src/remote/playwrightConnection.ts126-136
Refresh this wiki