Implementation:Microsoft Playwright Client Worker
| Knowledge Sources | |
|---|---|
| Domains | Browser Automation, Web Workers |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for representing and interacting with Web Workers and Service Workers in browser pages provided by the Playwright library.
Description
The Worker class extends ChannelOwner and implements the api.Worker interface. It represents a Web Worker (attached to a Page) or a Service Worker (attached to a BrowserContext). The class maintains optional references to its parent _page or _context.
The constructor sets up console event subscription mapping, handles the 'close' channel event to clean up references from the parent page/context, emits the close event, and closes the _closedScope (a LongStandingScope) with an appropriate error.
Key methods include:
- url(): Returns the worker's script URL from the initializer.
- evaluate(): Executes a function in the worker's execution context and returns the deserialized result.
- evaluateHandle(): Executes a function and returns a JSHandle to the result.
- waitForEvent(): Waits for a named event with timeout and predicate support, using the
Waiterclass.
Usage
Use the Worker class to interact with Web Workers or Service Workers. Obtain instances from the 'worker' event on a Page or the 'serviceworker' event on a BrowserContext.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File:
packages/playwright-core/src/client/worker.ts
Signature
export class Worker extends ChannelOwner<channels.WorkerChannel> implements api.Worker {
_page: Page | undefined;
_context: BrowserContext | undefined;
readonly _closedScope: LongStandingScope;
static from(worker: channels.WorkerChannel): Worker;
static fromNullable(worker: channels.WorkerChannel | undefined): Worker | null;
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.WorkerInitializer);
url(): string;
async evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg?: Arg): Promise<R>;
async evaluateHandle<R, Arg>(pageFunction: PageFunction<Arg, R>, arg?: Arg): Promise<SmartHandle<R>>;
async waitForEvent(event: string, optionsOrPredicate?: WaitForEventOptions): Promise<any>;
}
Import
import { Worker } from 'playwright-core/src/client/worker';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| pageFunction | string | Yes | The function or expression to evaluate in the worker context |
| arg | any |
No | Argument to pass to the evaluation function |
| event | string |
Yes | The event name to wait for |
| optionsOrPredicate | WaitForEventOptions |
No | Timeout and predicate options for waitForEvent |
Outputs
| Name | Type | Description |
|---|---|---|
| url() | string |
The URL of the worker's script |
| evaluate() | Promise<R> |
The deserialized return value from the worker evaluation |
| evaluateHandle() | Promise<SmartHandle<R>> |
A JSHandle referencing the return value in the worker context |
| waitForEvent() | Promise<any> |
The event argument when the awaited event fires |
Usage Examples
// Listen for new workers
page.on('worker', (worker) => {
console.log('Worker created:', worker.url());
worker.on('close', () => {
console.log('Worker closed:', worker.url());
});
});
// Evaluate in a worker
const workers = page.workers();
for (const worker of workers) {
const result = await worker.evaluate(() => {
return self.location.href;
});
console.log('Worker URL:', result);
}
// Wait for a worker event
const worker = await page.waitForEvent('worker');
console.log('New worker:', worker.url());