diff --git a/CHANGELOG.md b/CHANGELOG.md index d28d2acf..25cd9de7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/lib/ferrum/target.rb b/lib/ferrum/target.rb index 882998b9..882ef420 100644 --- a/lib/ferrum/target.rb +++ b/lib/ferrum/target.rb @@ -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 diff --git a/lib/ferrum/worker.rb b/lib/ferrum/worker.rb index fbe3d869..bb69a61b 100644 --- a/lib/ferrum/worker.rb +++ b/lib/ferrum/worker.rb @@ -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 @@ -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 diff --git a/sig/ferrum/worker.rbs b/sig/ferrum/worker.rbs index ae505d47..04f24258 100644 --- a/sig/ferrum/worker.rbs +++ b/sig/ferrum/worker.rbs @@ -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 diff --git a/spec/worker_spec.rb b/spec/worker_spec.rb index 0f9b06eb..13ed1443 100644 --- a/spec/worker_spec.rb +++ b/spec/worker_spec.rb @@ -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