Browser Patching
Relevant source files
The following files were used as context for generating this wiki page:
Browser Patching in Playwright refers to two distinct but related concepts: the modification of browser source code to enable automation (Core Browser Patching) and the mechanism for updating test snapshots (Test Snapshot Patching). Playwright maintains its own forks of Chromium, Firefox, and WebKit, applying a set of patches to enable cross-browser automation that is reliable and powerful. This includes adding custom functionality and APIs not available in the stock browsers.
On the user-facing side, Playwright provides a powerful test snapshot updating feature. When a snapshot assertion like toMatchAriaSnapshot fails, Playwright can generate a patch file. This patch can be applied to the user's test source code to update the baseline snapshot, a process referred to as "rebaselining".
Core Browser Patching
To provide its powerful automation capabilities, Playwright relies on custom-built versions of browsers. These browsers are not stock builds; they include patches that expose additional functionality. Contributors can also run tests against their own custom browser builds by setting environment variables like CRPATH, WKPATH, or FFPATH.
Sources: CONTRIBUTING.md
Upstream Synchronization
Playwright regularly synchronizes its browser forks with the upstream repositories of Chromium, Firefox, and WebKit. The roll_from_upstream.sh script automates this process.
The diagram below illustrates the general workflow for updating a browser from its upstream source and applying Playwright-specific patches.
This diagram shows the process of pulling the latest code from an upstream browser repository, applying Playwright's custom patches, and building the result.
Sources: browser_patches/roll_from_upstream.sh
Examples of Browser-Specific Patches
Playwright's patches are extensive and vary by browser. They are essential for enabling the rich feature set of the API.
Firefox: Juggler IPC
For Firefox, Playwright introduces a custom Inter-Process Communication (IPC) layer named "Juggler". This is a WebSocket-based protocol that allows the Playwright Node.js library to communicate with the patched Firefox browser. This is necessary because Firefox, unlike Chromium, does not have a built-in remote debugging protocol like CDP.
Sources: browser_patches/firefox/juggler/README.md
WebKit: Custom Embedder
For WebKit, Playwright uses a custom embedder to host the WebKit engine. On Windows, for example, this involves creating a native MainWindow that hosts the WebView. This custom application is responsible for initializing the browser engine and integrating it with the Playwright driver.
Sources: browser_patches/webkit/embedder/Playwright/win/MainWindow.cpp
Test Snapshot Patching (Rebaselining)
Playwright Test includes a feature to help manage snapshot tests, such as those created with toMatchAriaSnapshot. When a snapshot mismatch occurs, instead of failing the test, Playwright can suggest an update. This suggestion is captured and can be applied to the source test files, effectively "patching" them with the new baseline.
Sources: packages/playwright/src/runner/rebase.ts, tests/playwright-test/update-aria-snapshot.spec.ts
Rebaselining Flow
When a test with a snapshot assertion runs and fails due to a mismatch, or if the baseline is missing, a "suggested rebaseline" is generated. This suggestion is then processed at the end of the test run to update the source files.
The following sequence diagram shows the process:
This diagram shows how a snapshot failure during a test run leads to a suggested rebaseline, which is then applied to the source file.
Sources: packages/playwright/src/runner/rebase.ts:34-40, packages/playwright/src/runner/rebase.ts:42-114
Update Methods
The applySuggestedRebaselines function in rebase.ts handles the application of new baselines. It supports several methods for updating the source code, configured via the updateSourceMethod option in playwright.config.ts.
| Method | Description |
|---|---|
patch | (Default) Creates a rebaselines.patch file in the test output directory. The user must apply this patch manually (e.g., using git apply). |
overwrite | Directly overwrites the source test files with the updated snapshot content. This is a destructive action. |
3way | Overwrites the source test files but includes Git-style conflict markers (<<<<<<<, =======, >>>>>>>) where the changes are applied, allowing for manual merge review. |
Sources: packages/playwright/src/runner/rebase.ts:98-111, tests/playwright-test/update-aria-snapshot.spec.ts:602-701
Example of a Generated Patch
When running tests with --update-snapshots, if a snapshot mismatch is found, Playwright will output a message indicating that a patch file has been created.
New baselines created for:
a.spec.ts
git apply test-results/rebaselines.patch
Sources: tests/playwright-test/update-aria-snapshot.spec.ts:43-49
The generated rebaselines.patch file contains a standard diff format that can be applied to the source code.
diff --git a/a.spec.ts b/a.spec.ts
--- a/a.spec.ts
+++ b/a.spec.ts
@@ -3,7 +3,8 @@
test('test', async ({ page }) => {
await page.setContent(\`<h1>hello</h1><h2>bye</h2>\`);
await expect(page.locator('body')).toMatchAriaSnapshot(\`
- - heading "world"
+ - heading "hello" [level=1]
+ - heading "bye" [level=2]
\`);
});
This example shows a patch generated for a.spec.ts, where the ARIA snapshot for the body element is updated to reflect the new content.
Sources: packages/playwright/src/runner/rebase.ts:117-126, tests/playwright-test/update-aria-snapshot.spec.ts:31-42