Skip to content

Worker ​

Added in: v1.8

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.

java
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 ​

worker.evaluate() ​

Added in: v1.8

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.

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

  • Serializable

worker.evaluateHandle() ​

Added in: v1.8

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.

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

  • JSHandle

worker.url() ​

Added in: v1.8

Returns

  • string

worker.waitForClose() ​

Added in: v1.10

Performs action and waits for the Worker to close.

Arguments

  • options Object (optional)

    • timeout float (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

  • Worker

worker.waitForConsoleMessage() ​

Added in: v1.57

Performs action and waits for a console message.

Arguments

  • options Object (optional)

    • predicate function(ConsoleMessage): boolean (optional)

      Receives the ConsoleMessage object and resolves to true when the waiting should resolve.

    • timeout float (optional)

      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

    Callback that performs the action triggering the event.

Returns

  • ConsoleMessage

Events ​

worker.onClose() ​

Added in: v1.8

Emitted when this dedicated WebWorker is terminated.

Event data

  • Worker

worker.onConsole() ​

Added in: v1.57

Emitted when JavaScript within the worker calls one of console API methods, e.g. console.log or console.dir.

Event data

  • ConsoleMessage

Released under the Apache 2.0 License.