Skip to main content
The Worker class represents a WebWorker or Service Worker. Worker event is emitted on the page object to signal a worker creation.

Events

close

Emitted when the worker is closed.

console

Emitted when JavaScript within the worker calls console.log or other console API methods.
You need to subscribe to the ‘console’ event before the worker sends any console messages.

Methods

url

Returns the URL of the worker. Returns: string - Worker URL

evaluate

Evaluates a function in the worker context.
function
required
Function to be evaluated in the worker context
any
Optional argument to pass to the function
Returns: Promise<R> - Promise that resolves to the return value

evaluateHandle

Evaluates a function in the worker context and returns a JSHandle.
function
required
Function to be evaluated
any
Optional argument to pass to the function
Returns: Promise<JSHandle> - Promise that resolves to a JSHandle

waitForEvent

Waits for an event to fire and returns its value.
string
required
Event name (e.g., ‘close’, ‘console’)
object
Optional waiting parameters
Returns: Promise<any> - Promise that resolves to the event data

Examples

Monitoring worker creation

Communicating with workers

Testing Service Workers

Accessing worker from page

Best practices

Always set up event listeners (console, close) before the worker is created to avoid missing events.
Workers run in a separate execution context from the page. They don’t have access to the DOM or the page’s JavaScript scope.