@playwright-repl/playwright-crx
v1.23.0
Published
> **Fork notice:** This is a fork of [ruifigueira/playwright-crx](https://github.com/ruifigueira/playwright-crx) by Rui Figueira, published as [`@playwright-repl/playwright-crx`](https://www.npmjs.com/package/@playwright-repl/playwright-crx) for use with
Readme
Playwright CRX
Fork notice: This is a fork of ruifigueira/playwright-crx by Rui Figueira, published as
@playwright-repl/playwright-crxfor use with playwright-repl. It follows newer Playwright releases than upstream and carries the patches listed under Fork patches. See CHANGELOG.md for details.
This package contains the Chrome Extensions flavor of the Playwright library.
For that, it relies on chrome.debugger to implement playwright's ConnectionTransport interface.
NOTE: If you want to write end-to-end tests, you should use @playwright/test instead.
Requirements
- Node.js 20+
- Bundles Playwright 1.65.0 (Chromium-based browsers only)
Install
npm install @playwright-repl/playwright-crxThe package is ESM only.
API
It's possible to use playwright-crx as a library to create new chrome extensions.
Here's a simple example of a background service worker for a chrome extension using playwright-crx:
import { crx, expect } from '@playwright-repl/playwright-crx/test';
// if you don't need assertions, you can reduce the bundle size by importing crx from the main entry
// import { crx } from '@playwright-repl/playwright-crx';
chrome.action.onClicked.addListener(async ({ id: tabId }) => {
const crxApp = await crx.start({ slowMo: 500 });
try {
// tries to connect to the active tab, or creates a new one
const page = await crxApp.attach(tabId!).catch(() => crxApp.newPage());
await page.goto('https://demo.playwright.dev/todomvc/#/');
await page.getByPlaceholder('What needs to be done?').click();
await page.getByPlaceholder('What needs to be done?').fill('Hello World!');
await page.getByPlaceholder('What needs to be done?').press('Enter');
// assertions work too
await expect(page.getByTestId('todo-title')).toHaveText('Hello World!');
} finally {
// page stays open, but no longer controlled by playwright
await crxApp.detach(page);
// releases chrome.debugger
await crxApp.close();
}
});A more complete example can be found in examples/todomvc-crx.
Tracing
Playwright CRX also supports tracing, compatible with Playwright Trace Viewer.
Here's an example on how to run it:
await page.context().tracing.start({ screenshots: true, snapshots: true });
await page.goto('https://demo.playwright.dev/todomvc');
const newTodo = page.getByPlaceholder('What needs to be done?');
await newTodo.fill('buy some cheese');
await newTodo.press('Enter');
await expect(page.getByTestId('todo-title')).toHaveText('buy some cheese');
// stores in memfs and then reads its data
await page.context().tracing.stop({ path: '/tmp/trace.zip' });
const data = crx.fs.readFileSync('/tmp/trace.zip');
// opens playwright traceviewer
const tracePage = await crxApp.newPage();
await tracePage.goto('https://trace.playwright.dev');
const [filechooser] = await Promise.all([
tracePage.waitForEvent('filechooser'),
tracePage.getByRole('button', { name: 'Select file(s)' }).click(),
]);
// uploads the trace data buffer (file paths from memfs are not supported)
await filechooser.setFiles({
name: 'trace.zip',
mimeType: 'application/zip',
buffer: Buffer.from(data),
});You can give it a try with examples/todomvc-crx.
Recorder
Upstream also ships a recorder / player Chrome extension built on this library. This fork doesn't include it; see ruifigueira/playwright-crx if you need it.
Fork patches
Changes to the vendored Playwright source in playwright/, which must be re-checked after every Playwright upgrade:
- CSP
unsafe-evalguard (packages/playwright-core/src/server/javascript.ts): skips the function serialization check when CSP blocksunsafe-eval, as it does in MV3 extension service workers. CrxApplicationmetainfo (packages/isomorphic/protocolMetainfo.ts): marksattach,attachAll,detachanddetachAllas internal, so they don't show up as failed actions in the Trace Viewer.- Screencast frame-size rounding (
packages/playwright-core/src/server/chromium/crPage.ts, #36): rounds fractional viewport sizes so screencast frames aren't dropped at non-integer display scaling. - Main frame session fallback (
packages/playwright-core/src/server/chromium/crPage.ts): when attaching to an existing tab withchrome.debugger.attach, the main frame id can differ from the target id._sessionForFramefalls back to the session stored under the target id instead of throwingFrame has been detached.
npm run check:patches (also run by npm run build) fails if any of these is missing. Add new vendored patches to utils/check_vendor_patches.cjs.
Changes in this package's own source (src/):
- Graceful detach: detaching a page whose initialization failed no longer throws, so
close()still releaseschrome.debugger. - Restart after close:
crx.start()works again aftercrxApp.close().
Build
To build playwright-crx:
npm ci
npm run buildTests
npm run test:install
npm testTests run in a headed Chromium with the tests/test-extension extension loaded. Set HEADLESS=1 to run headless. To debug the extension's service worker, see CONTRIBUTING.md.
Updating Playwright
Playwright is vendored as a git subtree in playwright/. Upgrade one minor version at a time, on its own branch (replace v1.65.0 with the desired release tag):
git subtree pull --prefix=playwright https://github.com/microsoft/playwright.git v1.65.0 --squashThen:
Check the vendored patches. The squashed commit message ends with
git-subtree-split: <sha>, the upstream commit that was pulled. Compare it with the vendored tree:git diff <split sha> HEAD:playwright --statThe only files listed should be the ones under Fork patches, and
npm run check:patchesmust pass. If a patch is missing, the pull dropped it: re-apply it.Bump the test harness. Set
@playwright/testintests/package.jsonto the same version, so the tests run against the same Chromium.Merge the PR with a merge commit. Never squash or rebase a subtree PR.
git subtreefinds the base for the next pull by looking for the last commit with agit-subtree-splitline. If the PR is squashed, that commit becomes the squash commit, which also contains our patches. The next pull then treats the patches as changes upstream reverted, and drops them without a conflict. That is how the main frame session fallback was lost in the 1.55 upgrade.
Release
Releases are published by hand:
Bump
versioninpackage.json(andpackage-lock.json, e.g. withnpm version <x.y.z> --no-git-tag-version).Add a section for the version to CHANGELOG.md.
Merge to
main, then tag it:git tag v<x.y.z> && git push origin v<x.y.z>.Create a GitHub release for the tag, using the CHANGELOG section as notes.
Publish to npm from a clean build:
npm ci npm run build npm publish
