Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
`go_to` instead

### Fixed
- `worker.close` never closed a dedicated worker, Chrome refuses to close it from outside, it closes itself now
- `page.on(:request, &:continue)` never continued the request, it called `continue(index, total)` and the error was
dropped. Symbol procs and lambdas taking one argument get only the object now, for `:request`, `:response`, `:auth`
and `:dialog`
Expand Down
2 changes: 1 addition & 1 deletion lib/ferrum/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def build_worker
raise NoSuchTargetError, "No session with #{type} #{id}, attach to it with Context#attach_target first"
end

Worker.new(client, target_id: id, url: url)
Worker.new(client, target_id: id, url: url, type: type)
end

# Closes the WebSocket connection, without closing the target itself
Expand Down
14 changes: 10 additions & 4 deletions lib/ferrum/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ class Worker
# @return [Client, SessionClient]
attr_reader :client

attr_reader :target_id, :url
attr_reader :target_id, :url, :type

def initialize(client, target_id:, url:)
def initialize(client, target_id:, url:, type: "worker")
@client = client
@target_id = target_id
@url = url
@type = type
@options = client.options
@page = self
@execution_id = Concurrent::MVar.new
Expand Down Expand Up @@ -64,11 +65,16 @@ def main_frame
nil
end

# Closes the target in the browser, and its connection.
# Closes the worker in the browser, and its connection. Chrome refuses
# `Target.closeTarget` for a dedicated worker, so it closes itself instead.
#
# @return [Boolean]
def close
client.command("Target.closeTarget", async: true, targetId: target_id)
if type == "worker"
client.command("Runtime.evaluate", async: true, expression: "self.close()")
else
client.command("Target.closeTarget", async: true, targetId: target_id)
end
close_connection

true
Expand Down
4 changes: 3 additions & 1 deletion sig/ferrum/worker.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ module Ferrum
attr_reader client: (Client | SessionClient)
attr_reader target_id: String
attr_reader url: String
attr_reader type: String
attr_reader network: Network
attr_reader console: Console

@client: (Client | SessionClient)
@target_id: String
@url: String
@type: String
@options: Browser::Options
@page: Worker
@execution_id: untyped
@network: Network
@console: Console

def initialize: ((Client | SessionClient) client, target_id: String, url: String) -> void
def initialize: ((Client | SessionClient) client, target_id: String, url: String, ?type: String) -> void

def timeout: () -> ::Numeric

Expand Down
9 changes: 9 additions & 0 deletions spec/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ def spawn_worker
expect(spawn_worker.main_frame).to be_nil
end
end

describe "#close" do
it "closes a dedicated worker" do
worker = spawn_worker
worker.close

wait_for { browser.targets.key?(worker.target_id) }.to be(false)
end
end
end
Loading