From aa6122b9334ab54d68e2650038ebf28afcb867cf Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Fri, 20 Nov 2026 09:48:57 +0300 Subject: [PATCH 1/2] ref(events): yield only the params to raw CDP event callbacks The dispatcher passed each callback its index and the total number of callbacks, leaking subscriber internals into on and breaking lambdas and symbol procs taking one argument. Tracing relied on the index and ignored Tracing.tracingComplete unless its callback came first, so a user subscription added before the first record made it time out. --- CHANGELOG.md | 2 ++ lib/ferrum/concurrency/dispatcher.rb | 10 ++++------ lib/ferrum/interceptable.rb | 3 +-- lib/ferrum/page/tracing.rb | 4 +--- sig/ferrum/concurrency/dispatcher.rbs | 2 +- spec/page/tracing_spec.rb | 8 ++++++++ spec/page_spec.rb | 9 +++++++++ spec/unit/concurrency/dispatcher_spec.rb | 8 ++++---- 8 files changed, 30 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b354092..98747fec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ - Every script runs the new way: keyword arguments are named parameters, promises and thenables are awaited, all statements run and the last one's value is returned, and `Node#evaluate` returns nodes and dates, see [upgrading](docs/13-javascript.md#upgrading-to-021) [#673] +- `on` with a raw CDP event yields only the params, not the callback's index and the total number of callbacks ### Deprecated - `cookies.all`, will be removed in 0.22. Keyed by name, it kept one of the cookies sharing a name, use `to_a`, or @@ -62,6 +63,7 @@ ### Fixed - A headful browser with `xvfb: true` sometimes started before Xvfb was ready and exited at once with `ProcessTimeoutError`, Ferrum waits for the display now +- `tracing.record` timed out when `page.on("Tracing.tracingComplete")` was subscribed before its first call - `page.command(method, wait:)` ran a subclass override of `command`, like Cuprite's, twice - `evaluate` of a result nested about 100 levels deep raised an opaque `BrowserError` about Chrome's CBOR stack limit, it raises `ResultTooDeepError` now, a `BrowserError` that says to return less or use `evaluate_handle` diff --git a/lib/ferrum/concurrency/dispatcher.rb b/lib/ferrum/concurrency/dispatcher.rb index c615c101..a01db0a5 100644 --- a/lib/ferrum/concurrency/dispatcher.rb +++ b/lib/ferrum/concurrency/dispatcher.rb @@ -9,7 +9,7 @@ module Concurrency # # @example # dispatcher = Ferrum::Concurrency::Dispatcher.new(lanes: %i[regular priority]) - # dispatcher.on("greeting") { |payload, index, total| puts payload } + # dispatcher.on("greeting") { |payload| puts payload } # dispatcher.publish("greeting", "hello", lane: :priority) # class Dispatcher @@ -51,8 +51,7 @@ def publish(key, payload, lane: @queues.each_key.first) end # - # Registers a callback for a key. It receives the payload, its own index among the callbacks of the key and - # their total number. + # Registers a callback for a key. It receives the payload. # # @param [String] key # @@ -129,9 +128,8 @@ def start end def call(key, payload) - total = @on[key]&.size.to_i - @on[key]&.each_with_index do |(_, block), index| - block.call(payload, index, total) + @on[key]&.each do |(_, block)| + block.call(payload) rescue StandardError => e # A raising callback used to terminate the dispatch thread, and with it every # event for the rest of the browser's life: targets stopped being registered diff --git a/lib/ferrum/interceptable.rb b/lib/ferrum/interceptable.rb index 0aaab60b..1ce1f3f0 100644 --- a/lib/ferrum/interceptable.rb +++ b/lib/ferrum/interceptable.rb @@ -24,8 +24,7 @@ module Interceptable # `:response`, the finished {Network::Exchange}. For `:auth`, the # {Network::AuthRequest} challenge. For `:console`, the new # {Console::Message}. For any other (raw CDP) event name, the event's - # params `Hash`, followed by the callback's index and the total number - # of callbacks registered for the event. + # params `Hash`. # # @return [Integer] # The subscription id, used to unsubscribe via {#off}. diff --git a/lib/ferrum/page/tracing.rb b/lib/ferrum/page/tracing.rb index d584f0c1..ba186587 100644 --- a/lib/ferrum/page/tracing.rb +++ b/lib/ferrum/page/tracing.rb @@ -90,9 +90,7 @@ def stop def subscribe_tracing_complete return if @subscribed_tracing_complete - @page.on("Tracing.tracingComplete") do |event, index| - next if index.to_i != 0 - + @page.on("Tracing.tracingComplete") do |event| @pending.set(stream_handle(event["stream"])) rescue StandardError => e @failure = e diff --git a/sig/ferrum/concurrency/dispatcher.rbs b/sig/ferrum/concurrency/dispatcher.rbs index fdc6c17b..6e869836 100644 --- a/sig/ferrum/concurrency/dispatcher.rbs +++ b/sig/ferrum/concurrency/dispatcher.rbs @@ -13,7 +13,7 @@ module Ferrum def publish: (String key, untyped payload, ?lane: Symbol) -> void - def on: (String key) { (untyped, Integer, Integer) -> void } -> Integer + def on: (String key) { (untyped) -> void } -> Integer def off: (String key, Integer id) -> bool diff --git a/spec/page/tracing_spec.rb b/spec/page/tracing_spec.rb index 73b9f8fb..c36dbf5b 100644 --- a/spec/page/tracing_spec.rb +++ b/spec/page/tracing_spec.rb @@ -85,6 +85,14 @@ def force_repaints expect(File.exist?(file_path3)).to be(true) end + it "outputs a trace when the tracing complete event already has a subscriber" do + page.on("Tracing.tracingComplete") { nil } + + page.tracing.record(path: file_path, timeout: 5) { page.go_to } + + expect(File.exist?(file_path)).to be(true) + end + it "returns base64 encoded string" do trace = page.tracing.record(encoding: :base64) { page.go_to } diff --git a/spec/page_spec.rb b/spec/page_spec.rb index a2206534..44a0dc29 100644 --- a/spec/page_spec.rb +++ b/spec/page_spec.rb @@ -550,6 +550,15 @@ def is_mobile? page.evaluate("console.log('hello')") wait_for { message }.to eq("hello") end + + it "yields only the params" do + message = nil + + page.on("Runtime.consoleAPICalled", &->(params) { message = params.dig("args", 0, "value") }) + + page.evaluate("console.log('hello')") + wait_for { message }.to eq("hello") + end end context "with :request" do diff --git a/spec/unit/concurrency/dispatcher_spec.rb b/spec/unit/concurrency/dispatcher_spec.rb index ccb74bfc..ba436872 100644 --- a/spec/unit/concurrency/dispatcher_spec.rb +++ b/spec/unit/concurrency/dispatcher_spec.rb @@ -15,14 +15,14 @@ expect(called.pop).to eq("hello") end - it "passes the index of the callback and the total" do + it "passes only the payload to every callback of the key" do called = Queue.new - dispatcher.on("greeting") { |_, index, total| called << [index, total] } - dispatcher.on("greeting") { |_, index, total| called << [index, total] } + dispatcher.on("greeting", &->(payload) { called << [:first, payload] }) + dispatcher.on("greeting", &->(payload) { called << [:second, payload] }) dispatcher.publish("greeting", "hello") - expect([called.pop, called.pop]).to eq([[0, 2], [1, 2]]) + expect([called.pop, called.pop]).to eq([[:first, "hello"], [:second, "hello"]]) end it "delivers in order within a lane" do From eb9014517849039ce1d5fff8113a82e70ffddb05 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Fri, 20 Nov 2026 12:39:09 +0500 Subject: [PATCH 2/2] chore(options): deprecate flatten: false and schedule the option for removal in 0.22 (#709) --- CHANGELOG.md | 1 + docs/2-customization.md | 2 +- lib/ferrum/browser.rb | 2 ++ lib/ferrum/browser/options.rb | 8 ++++++++ sig/ferrum/browser/options.rbs | 4 +++- spec/unit/browser/options_spec.rb | 9 +++++++++ 6 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98747fec..cbb4ddfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ `page.on(:console)` instead - `:pending_connection_errors` and `PendingConnectionsError`, will be removed in 0.22. Pass `on_timeout: :raise` to `go_to` instead +- `flatten: false`, the option will be removed in 0.22 and every page will share the browser's websocket connection - `FERRUM_DEBUG`, will be removed in 0.22. Use `FERRUM_LOG=*` instead - `:protocol_timeout`, `browser.protocol_timeout`, `browser.protocol_timeout=` and `FERRUM_PROTOCOL_TIMEOUT`, will be removed in 0.22. They are ignored and warn, `:timeout` bounds everything else diff --git a/docs/2-customization.md b/docs/2-customization.md index 89fb864a..ca398da4 100644 --- a/docs/2-customization.md +++ b/docs/2-customization.md @@ -17,7 +17,7 @@ Ferrum::Browser.new(options) * `:xvfb` (Boolean) - Run browser in a virtual framebuffer, `false` by default. * `:flatten` (Boolean) - Use one websocket connection to the browser and all the pages in flatten mode, `true` by default. When set to `false`, each page/target opens its own dedicated websocket connection instead - of sharing the browser's connection. + of sharing the browser's connection. Passing `false` is deprecated, the option will be removed in 0.22. * `:window_size` (Array) - The dimensions of the browser window in which to test, expressed as a 2-element array, e.g. [1024, 768]. Default: [1024, 768] * `:extensions` (Array[String | Hash]) - An array of paths to files or JS diff --git a/lib/ferrum/browser.rb b/lib/ferrum/browser.rb index fe03ae3e..6d62b018 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -70,6 +70,8 @@ class Browser # # @option options [Boolean] :flatten (true) # Use one websocket connection to the browser and all the pages in flatten mode. + # Passing `false` is deprecated, the option will be removed in Ferrum 0.22 + # and every page will share the browser's connection. # # @option options [(Integer, Integer)] :window_size ([1024, 768]) # The dimensions of the browser window in which to test, expressed as a diff --git a/lib/ferrum/browser/options.rb b/lib/ferrum/browser/options.rb index 7da0b7c8..c77928b7 100644 --- a/lib/ferrum/browser/options.rb +++ b/lib/ferrum/browser/options.rb @@ -52,6 +52,7 @@ def initialize(options = nil) @incognito = @options.fetch(:incognito, true) @dockerize = @options.fetch(:dockerize, false) @flatten = @options.fetch(:flatten, true) + warn_flatten unless @flatten @pending_connection_errors = fetch_deprecated( :pending_connection_errors, "Pass on_timeout: :raise to go_to instead: page.go_to(url, on_timeout: :raise)." ) @@ -171,6 +172,13 @@ def warn_protocol_timeout Utils::Deprecate.warn(":protocol_timeout #{PROTOCOL_TIMEOUT_REMOVED}", user_code: true) end + def warn_flatten + Utils::Deprecate.warn( + "flatten: false will be removed in Ferrum 0.22. Every page will share the browser's websocket connection.", + user_code: true + ) + end + def fetch_deprecated(name, instead) return false unless @options.key?(name) diff --git a/sig/ferrum/browser/options.rbs b/sig/ferrum/browser/options.rbs index 4b53f42b..2385dea1 100644 --- a/sig/ferrum/browser/options.rbs +++ b/sig/ferrum/browser/options.rbs @@ -61,7 +61,7 @@ module Ferrum attr_reader incognito: bool - attr_reader flatten: bool + %a{deprecated} attr_reader flatten: bool attr_reader ignore_default_browser_options: bool? @@ -113,6 +113,8 @@ module Ferrum def warn_protocol_timeout: () -> void + def warn_flatten: () -> void + def fetch_deprecated: (Symbol name, String instead) -> bool def read_extension: (String | Hash[Symbol, String] extension) -> String diff --git a/spec/unit/browser/options_spec.rb b/spec/unit/browser/options_spec.rb index 7d59804f..9f80c241 100644 --- a/spec/unit/browser/options_spec.rb +++ b/spec/unit/browser/options_spec.rb @@ -11,6 +11,15 @@ expect { described_class.new(protocol_timeout: 10) } .to output(/:protocol_timeout is ignored.*called from #{__FILE__}/m).to_stderr end + + it "warns that flatten: false will be removed" do + expect { described_class.new(flatten: false) } + .to output(/flatten: false will be removed in Ferrum 0.22.*called from #{__FILE__}/m).to_stderr + end + + it "stays silent when :flatten is true" do + expect { described_class.new(flatten: true) }.not_to output.to_stderr + end end describe "#protocol_timeout" do