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.Worker += (_, worker) =>
{
Console.WriteLine($"Worker created: {worker.Url}");
worker.Close += (_, _) => Console.WriteLine($"Worker closed {worker.Url}");
};

Console.WriteLine("Current Workers:");
foreach(var pageWorker in page.Workers)
{
Console.WriteLine($"\tWorker: {pageWorker.Url}");
}

event Worker.Close

Added in: v1.8

Emitted when this dedicated WebWorker is terminated.

Worker.EvaluateAsync(expression, arg)

Added in: v1.8
  • 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 argument to pass to expression.#
  • returns: <[object]>#

Returns the return value of expression.

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

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

Worker.EvaluateHandleAsync(expression, arg)

Added in: v1.8
  • 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 argument to pass to expression.#
  • returns: <JSHandle>#

Returns the return value of expression as a JSHandle.

The only difference between Worker.EvaluateAsync(expression, arg) and Worker.EvaluateHandleAsync(expression, arg) is that Worker.EvaluateHandleAsync(expression, arg) returns JSHandle.

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

Worker.Url

Added in: v1.8