Skip to main content

Worker

The Worker class represents a WebWorker. worker event is emitted on the page object to signal a worker creation. close event is emitted on the worker object when the worker is gone.

page.onWorker(worker -> {
System.out.println("Worker created: " + worker.url());
worker.onClose(worker1 -> System.out.println("Worker destroyed: " + worker1.url()));
});
System.out.println("Current workers:");
for (Worker worker : page.workers())
System.out.println(" " + worker.url());

Methods

evaluate

Added before v1.9 worker.evaluate

Returns the return value of expression.

If the function passed to the Worker.evaluate() returns a Promise, then Worker.evaluate() would wait for the promise to resolve and return its value.

If the function passed to the Worker.evaluate() returns a non-Serializable value, then Worker.evaluate() returns undefined. Playwright also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity.

Usage

Worker.evaluate(expression);
Worker.evaluate(expression, arg);

Arguments

  • expression String#

    JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.

  • arg EvaluationArgument (optional)#

    Optional argument to pass to expression.

Returns


evaluateHandle

Added before v1.9 worker.evaluateHandle

Returns the return value of expression as a JSHandle.

The only difference between Worker.evaluate() and Worker.evaluateHandle() is that Worker.evaluateHandle() returns JSHandle.

If the function passed to the Worker.evaluateHandle() returns a Promise, then Worker.evaluateHandle() would wait for the promise to resolve and return its value.

Usage

Worker.evaluateHandle(expression);
Worker.evaluateHandle(expression, arg);

Arguments

  • expression String#

    JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.

  • arg EvaluationArgument (optional)#

    Optional argument to pass to expression.

Returns


url

Added before v1.9 worker.url

Usage

Worker.url();

Returns


waitForClose

Added in: v1.10 worker.waitForClose

Performs action and waits for the Worker to close.

Usage

Worker.waitForClose(callback);
Worker.waitForClose(callback, options);

Arguments

  • options Worker.WaitForCloseOptions (optional)

    • setTimeout double (optional) Added in: v1.9#

      Maximum time to wait for in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the BrowserContext.setDefaultTimeout().

  • callback Runnable Added in: v1.9#

    Callback that performs the action triggering the event.

Returns


Events

onClose(handler)

Added before v1.9 worker.onClose(handler)

Emitted when this dedicated WebWorker is terminated.

Usage

Worker.onClose(handler)

Event data