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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,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
Expand All @@ -62,6 +64,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`
Expand Down
2 changes: 1 addition & 1 deletion docs/2-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/ferrum/browser/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)."
)
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 4 additions & 6 deletions lib/ferrum/concurrency/dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
#
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/ferrum/interceptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
4 changes: 1 addition & 3 deletions lib/ferrum/page/tracing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion sig/ferrum/browser/options.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sig/ferrum/concurrency/dispatcher.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions spec/page/tracing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
9 changes: 9 additions & 0 deletions spec/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/browser/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/concurrency/dispatcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading