Browser Management
Relevant source files
The following files were used as context for generating this wiki page:
- packages/playwright-core/src/server/registry/browserFetcher.ts
- packages/playwright-core/browsers.json
- packages/playwright-browser-chromium/install.js
- packages/playwright-core/src/server/registry/index.ts
- packages/playwright-core/src/cli/program.ts
- packages/playwright/package.json
- packages/playwright-webkit/package.json
- tests/installation/playwright-cli-install-should-work.spec.ts
Playwright's Browser Management system is responsible for downloading, installing, and locating the correct browser binaries required for automation. This ensures a hermetic and consistent testing environment by coupling specific browser versions with a given Playwright release. The system handles not only the main browsers (Chromium, Firefox, WebKit) but also associated tools like ffmpeg and system-installed browser channels.
The core of this system is the Registry, which manages browser executables based on a central configuration file. It provides a command-line interface (CLI) for users to manage these installations, validates host requirements, and performs garbage collection of unused browser binaries to maintain a clean environment.
Core Components
The browser management architecture revolves around a central registry, a JSON configuration file, and installation scripts triggered by the package manager.
The Registry
The Registry class is the central component for managing browser executables. It is responsible for discovering, installing, and providing paths to the various browser binaries and tools.
Key responsibilities include:
- Parsing
browsers.jsonto understand which browsers and revisions are available. - Determining download URLs for each executable based on the host platform.
- Locating both hermetically-installed browsers and system-wide browser channels (e.g., Chrome, Edge).
- Managing the installation directory and performing garbage collection of unused browsers.
- Validating host dependencies required to run the browsers.
The Registry class constructor initializes a list of Executable objects, each representing a browser or tool that Playwright can manage.
Sources: packages/playwright-core/src/server/registry/index.ts:604-945
Browser Configuration (browsers.json)
This JSON file is the source of truth for which browsers and tools are managed by Playwright. It defines each browser's name, revision number, and whether it should be installed by default.
The structure of an entry in the browsers array is as follows:
| Field | Type | Description |
|---|---|---|
name | string | The unique name of the browser or tool (e.g., 'chromium', 'firefox', 'ffmpeg'). |
revision | string | The specific revision number of the binary to download. |
browserVersion | string | The user-facing version of the browser (e.g., "105.0.5195.19"). |
installByDefault | boolean | If true, npx playwright install will install this executable without it being explicitly named. |
revisionOverrides | object | A map of host platforms to specific revision numbers, used for platform-specific builds. |
Sources: packages/playwright-core/browsers.json
Installation Scripts
Browser downloads can be triggered automatically upon package installation. Packages like playwright-webkit and playwright-browser-chromium contain an install.js script that is executed by the package manager (e.g., npm, yarn).
// packages/playwright-webkit/package.json
{
"name": "playwright-webkit",
"version": "1.56.0-next",
...
"scripts": {
"install": "node install.js"
},
...
}
Sources: packages/playwright-webkit/package.json:19-21
This script is a simple wrapper that invokes the main browser installation logic within playwright-core.
Sources: packages/playwright-browser-chromium/install.js
Installation and Download Flow
Playwright provides a powerful CLI and a robust download mechanism to manage browser binaries.
CLI Commands
The primary user-facing tool for browser management is the playwright CLI. The install command is the main entry point.
The following diagram illustrates the high-level flow when a user runs npx playwright install.
This diagram shows the process of the playwright install command.
Sources: packages/playwright-core/src/cli/program.ts:182-253, packages/playwright-core/src/server/registry/index.ts:981-1059
Key options for the install command include:
| Option | Description |
|---|---|
[browser...] | Specify one or more browsers to install (e.g., chromium, firefox). If omitted, default browsers are installed. |
--with-deps | Installs necessary system dependencies for the browsers. |
--dry-run | Prints download URLs and installation paths without performing the installation. |
--force | Forces re-installation of system-wide browser channels like Chrome or Edge. |
--list | Prints a list of all browsers found across all Playwright installations. |
Sources: packages/playwright-core/src/cli/program.ts:184-191
Download Mechanism
When an installation is triggered, Playwright fetches the browser binaries from a CDN. The system is designed for reliability with support for mirrors and retries.
The download sequence is as follows:
This diagram shows the browser download process, including the CDN fallback mechanism.
The primary CDN endpoints are defined in index.ts. A custom download host can be specified via the PLAYWRIGHT_DOWNLOAD_HOST environment variable.
Sources: packages/playwright-core/src/server/registry/index.ts:40-52, packages/playwright-core/src/server/registry/browserFetcher.ts, tests/installation/playwright-cdn.spec.ts:16-21
Installation Directory
Browser binaries are stored in a central cache directory. The location can be configured via the PLAYWRIGHT_BROWSERS_PATH environment variable. If not set, it defaults to a platform-specific cache location:
- Linux:
$XDG_CACHE_HOME/ms-playwrightor~/.cache/ms-playwright - macOS:
~/Library/Caches/ms-playwright - Windows:
%LOCALAPPDATA%/ms-playwright
Sources: packages/playwright-core/src/server/registry/index.ts:553-584
Browser Executables and Channels
Playwright distinguishes between its own hermetically-installed browsers and those already present on the system.
Executable Paths
The Registry maintains a mapping of executable names to their relative paths within the installation directory for each operating system. This ensures that Playwright can always locate the correct binary to launch.
| Browser | Platform | Relative Path |
|---|---|---|
chromium | Linux | chrome-linux/chrome |
chromium | macOS | chrome-mac/Chromium.app/Contents/MacOS/Chromium |
chromium | Windows | chrome-win/chrome.exe |
firefox | Linux | firefox/firefox |
webkit | Windows | Playwright.exe |
ffmpeg | macOS | ffmpeg-mac |
Sources: packages/playwright-core/src/server/registry/index.ts:63-95
System Browser Channels
In addition to its own managed browsers, Playwright can drive existing system-wide installations of browsers like Chrome, Chrome Beta, and Edge. These are referred to as "channels". The Registry attempts to find them in standard installation locations.
Sources: packages/playwright-core/src/server/registry/index.ts:803-896
Cache Management and Validation
To prevent the browser cache from growing indefinitely, Playwright includes mechanisms for garbage collection and validation.
Installation Cache
When playwright install runs, it creates a lock file (__dirlock) in the registry directory to prevent concurrent modifications. It also creates a symbolic link in a .links subdirectory, pointing back to the node_modules/playwright-core directory that triggered the installation.
This linking mechanism allows Playwright to track which installations are using which browser binaries.
Sources: packages/playwright-core/src/server/registry/index.ts:982-995
Garbage Collection
The _validateInstallationCache method is responsible for cleaning up stale browsers. It works by:
- Reading all links in the
.linksdirectory to identify active Playwright installations. - Building a set of all required browser paths from these active installations.
- Scanning the registry directory for all installed browser folders.
- Deleting any browser folder that is not in the set of required paths.
This process is typically run automatically during playwright install and can be disabled by setting the PLAYWRIGHT_SKIP_BROWSER_GC environment variable.
Sources: packages/playwright-core/src/server/registry/index.ts:1000-1001, packages/playwright-core/src/server/registry/index.ts:1107-1110
Host Requirements Validation
After installation, Playwright can validate that the host system has the necessary dependencies to run the downloaded browsers. This is done by the _validateHostRequirements method, which checks for required libraries on Linux and Windows. This validation is cached and only re-run periodically.
Sources: packages/playwright-core/src/server/registry/index.ts:960-964, packages/playwright-core/src/server/registry/index.ts:1082-1099