Worker
Worker 类代表 WebWorker。worker 事件在页面对象上触发,以指示工作线程创建。当工作线程消失时,工作线程对象上会触发 close 事件。
¥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返回 expression 的返回值。
¥Returns the return value of expression.
如果传递给 Worker.evaluate() 的函数返回 Promise,那么 Worker.evaluate() 将等待 Promise 解析并返回其值。
¥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.
如果传递给 Worker.evaluate() 的函数返回非 可串行化 值,则 Worker.evaluate() 返回 undefined。Playwright 还支持传输一些 JSON 无法序列化的附加值:-0、NaN、Infinity、-Infinity。
¥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
要在浏览器上下文中执行的 JavaScript 表达式。如果表达式的计算结果为函数,则会自动调用该函数。
¥JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.
- argEvaluationArgument (optional)#
传递给 expression 的可选参数。
¥Optional argument to pass to expression.
返回
¥Returns
evaluateHandle
Added before v1.9将 expression 的返回值作为 JSHandle 返回。
¥Returns the return value of expression as a JSHandle.
Worker.evaluate() 和 Worker.evaluateHandle() 之间的唯一区别是 Worker.evaluateHandle() 返回 JSHandle。
¥The only difference between Worker.evaluate() and Worker.evaluateHandle() is that Worker.evaluateHandle() returns JSHandle.
如果传递给 Worker.evaluateHandle() 的函数返回 Promise,那么 Worker.evaluateHandle() 将等待 Promise 解析并返回其值。
¥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
要在浏览器上下文中执行的 JavaScript 表达式。如果表达式的计算结果为函数,则会自动调用该函数。
¥JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.
- argEvaluationArgument (optional)#
传递给 expression 的可选参数。
¥Optional argument to pass to expression.
返回
¥Returns
url
Added before v1.9用法
¥Usage
Worker.url();
返回
¥Returns
waitForClose
Added in: v1.10执行操作并等待 Worker 关闭。
¥Performs action and waits for the Worker to close.
用法
¥Usage
Worker.waitForClose(callback);
Worker.waitForClose(callback, options);
参数
¥Arguments
- 
optionsWorker.WaitForCloseOptions(可选)¥ optionsWorker.WaitForCloseOptions(optional)
等待的最长时间(以毫秒为单位)。默认为 30000(30 秒)。通过 0 禁用超时。可以使用 BrowserContext.setDefaultTimeout() 更改默认值。
¥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 that performs the action triggering the event.
返回
¥Returns
事件
¥Events
onClose(handler)
Added before v1.9当该专用 WebWorker 终止时触发。
¥Emitted when this dedicated WebWorker is terminated.
用法
¥Usage
Worker.onClose(handler)
事件数据
¥Event data