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.

def handle_worker(worker)
puts "worker created: #{worker.url}"
worker.once("close", -> (w) { puts "worker destroyed: #{w.url}" })
end

page.on('worker', method(:handle_worker))

puts "current workers:"
page.workers.each do |worker|
puts " #{worker.url}"
end

evaluate

def evaluate(expression, arg: nil)

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.

evaluate_handle

def evaluate_handle(expression, arg: nil)

Returns the return value of expression as a JSHandle.

The only difference between Worker#evaluate and Worker#evaluate_handle is that Worker#evaluate_handle returns JSHandle.

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

url

def url

expect_event

def expect_event(event, predicate: nil, timeout: nil, &block)

Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value. Will throw an error if the page is closed before the event is fired. Returns the event data value.

Usage

message = worker.expect_event("console") do
worker.evaluate("console.log(42)")
end