Configuration options
Add the package options to Playwright's use configuration. WebextOptions provides type checking
for the extension-specific options:
import { defineConfig } from '@playwright/test';
import type { WebextOptions } from 'playwright-webext';
export default defineConfig<WebextOptions>({
use: {
extensionPath: './dist',
// ...
},
});
Extension options
extensionPath
Type: string.
Required.
The directory containing your unpacked extension. Relative paths resolve from the Playwright configuration file's directory, or the current working directory when there is no config file. Absolute paths are also accepted.
Build the extension before running tests. The extension must use Chromium Manifest V3; a background
service worker is optional and detected automatically. This path is also the current build used by
extension.upgrade().
Extensions without a worker support manifest access, extension pages, and lifecycle operations.
Worker evaluation, event dispatch, storage helpers, and waitForFunction() require a declared worker.
extensionAutoInstall
Type: boolean.
Default: true.
Installs the configured extension before the test body runs. Set it to false when a test needs to
control installation, such as installing an older build for a migration test:
import { test } from 'playwright-webext';
test.use({
extensionAutoInstall: false,
});
test('installs an older build and upgrades it', async ({ extension }) => {
await extension.install('./dist-v1');
// ...
await extension.upgrade();
});
The example requires both an older build in dist-v1/ and the current build at extensionPath.
See Data Migration for assertions on the upgrade's effects.
extensionPath is required even when automatic installation is disabled. extension.context
exists before installation, but metadata and worker operations require an installed, ready extension.
extensionInitScript
Type: string | { content: string } | (string | { content: string })[].
Default: unset.
JavaScript that runs before your extension code in the service worker and all extension pages. If the extension has no service worker, the init script runs only in extension pages.
import { defineConfig } from '@playwright/test';
import type { WebextOptions } from 'playwright-webext';
export default defineConfig<WebextOptions>({
use: {
extensionPath: './dist',
extensionInitScript: './test/extension-init.js',
},
});
Math.random = () => 42;
You can also pass inline source:
extensionInitScript: { content: 'Math.random = () => 42;' },
Pass an array to run multiple scripts in order. Each item has its own scope; use globalThis to
share values between scripts:
extensionInitScript: [
'./test/extension-init.js',
{ content: 'Date.now = () => 0;' },
],
To pass values into inline source, use JSON.stringify() when interpolating them. It adds the
quotes and escaping needed for string values containing quotes, backslashes, or newlines:
import { test } from 'playwright-webext';
const userName = 'Alex "AJ" Smith';
test.use({
extensionInitScript: {
content: `globalThis.testUserName = ${JSON.stringify(userName)};`,
},
});
To run different code in the worker and popup, check their paths:
const manifest = chrome.runtime.getManifest();
// For service worker
if (location.pathname === `/${manifest.background?.service_worker}`) {
// ...
}
// For popup
if (location.pathname === `/${manifest.action?.default_popup}`) {
// ...
}
Rules
- Relative paths resolve from the Playwright configuration directory, or from the current working
directory with
launchWithExtension(). - Use plain JavaScript without imports or exports.
- Use
globalThisfor values that extension code needs to access. - The extension does not wait for async work to finish.
- The script runs again when an extension page loads or the worker restarts.
- It does not run on ordinary websites or in content scripts.
Under the hood
For extension pages, the package uses Playwright's context.addInitScript() with a check that skips
ordinary websites. This API does not run in service workers, so the package changes the prepared
manifest to point to a worker wrapper. Schematically:
{
"background": {
- "service_worker": "worker.js"
+ "service_worker": "webext-wrapper.js"
}
}
The wrapper loads your init script first, then the original worker:
+import './webext-init-script.js';
+import './worker.js';
These changes apply in a temporary directory, leaving the source unchanged.
extensionClock
Type: ExtensionClockOptions.
Default: unset.
Sets the date in the service worker and all pages and frames in extension.context before the
extension loads. By default, the date stays fixed while native timers keep running. Set
timers: true to control timers too. Omit extensionClock to keep the real date and timers.
| Option | Type | Default | Effect |
|---|---|---|---|
time | number | string | Date | Browser’s current time | Fixed or starting date; numbers are Unix milliseconds. |
timers | boolean | false | Also emulates timers, allowing runFor() and fastForward() to execute due callbacks. |
alarms | boolean | false | Emulates alarms in the live service worker, independently of JavaScript timers. |
paused | boolean | false | Starts the emulated clock paused. Requires timers: true or alarms: true. |
With timers: true, the worker emulates setTimeout, clearTimeout, setInterval,
clearInterval, and performance additionally to Date. Pages use Playwright's clock emulation.
test.use({
extensionClock: { time: '2026-01-01T15:00:00' },
});
Omit time to start from the browser’s current time at installation, for example
extensionClock: { timers: true } or extensionClock: { alarms: true }.
With extensionClock: {}, the date stays fixed at that initial time.
For extensionClock.time, date-time strings without a timezone use the browser's timezone.
Time-only strings in HH:mm format, such as '23:00', use the browser's current date and timezone,
with seconds and milliseconds set to zero.
Strings with Z or an offset identify an absolute instant: the browser displays it in its own
timezone, so the local hour can differ from the string.
For a browser in Europe/Berlin (UTC+01:00 in January):
| Without timezone | With timezone | |
|---|---|---|
extensionClock.time | 2030-01-01T15:00 | 2030-01-01T15:00Z |
| Browser-local date/time | 2030-01-01T15:00 | 2030-01-01T16:00 |
This does not change the browser's timezone. Numbers and Date objects retain their timestamps.
Playwright's timezoneId option currently does not apply to service workers.
See playwright#43983.
Date-only strings such as '2030-01-01' use midnight in the browser's timezone, equivalent to
'2030-01-01T00:00:00'. This differs from JavaScript's Date parsing, which treats date-only strings as UTC.
Use extension.clock.setSystemTime() to change the system date. Without alarm emulation,
setFixedTime() fixes the reported date.
With timers: true or alarms: true, use runFor() or fastForward() to advance by a duration.
With timers: true or alarms: true, time advances automatically unless paused: true is set;
pauseAt() and resume() control automatic advancement later.
See Clock emulation for examples and the API reference
for method details. The same options are available in launchWithExtension().
Forwarded Playwright options
- browserName (Chromium only)
- channel
- headless
- launchOptions
- contextOptions
Chromium is used by default. To run in Microsoft Edge, set use.channel: 'msedge'.
Both browsers use browserName: 'chromium'.
Context options, including baseURL, storageState, permissions, locale, and viewport,
apply to extension.context.
Set them directly in use or through use.contextOptions. Individual options take precedence over
values in contextOptions.
storageState accepts an object or a JSON file path and is restored before extension installation.
Traces, screenshots, and videos
Playwright's trace and
screenshot settings
apply automatically to the extension context. For example, screenshot: 'only-on-failure' attaches
screenshots to the report for failed tests. Pages must remain open until context teardown to appear
in these screenshots.
The
video setting controls video
recording, including retention modes, retries, and recording size. Retained videos are attached to
the test report after the extension context closes. Custom fixtures using launchWithExtension()
do not receive this video integration; see shared context.
See Using context for direct access and mapping the context fixture.