Skip to main content
Version: 1.16

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());

Worker.onClose(handler)#

Emitted when this dedicated WebWorker is terminated.

Worker.evaluate(expression[, arg])#

  • expression <String> JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted as a function. Otherwise, evaluated as an expression.#
  • arg <EvaluationArgument> Optional argument to pass to expression.#
  • returns: <Object>#

Returns the return value of expression.

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

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

Worker.evaluateHandle(expression[, arg])#

  • expression <String> JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted as a function. Otherwise, evaluated as an expression.#
  • arg <EvaluationArgument> Optional argument to pass to expression.#
  • returns: <JSHandle>#

Returns the return value of expression as a JSHandle.

The only difference between Worker.evaluate(expression[, arg]) and Worker.evaluateHandle(expression[, arg]) is that Worker.evaluateHandle(expression[, arg]) returns JSHandle.

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

Worker.url()#

Worker.waitForClose([options], callback)#

  • options <Worker.WaitForCloseOptions>
  • callback <Runnable> Callback that performs the action triggering the event.#
  • returns: <Worker>#

Performs action and waits for the Worker to close.