From 0024337a4a82ed3438635fde30f595977f0f39f9 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Fri, 20 Nov 2026 22:41:47 +0300 Subject: [PATCH] fix(browser): delegate off, window_bounds, tracing, activate and document_size to page Browser delegated on but not off, position but not window_bounds, and missed tracing, activate and document_size. Document that browser.command goes to the browser itself, unlike on, which subscribes on the default page. Also fix two RBS signatures: drop set_window_bounds, which no longer exists, and protected in Deadline, which RBS can't parse. --- CHANGELOG.md | 2 ++ docs/1-introduction.md | 13 ++++++++++++ lib/ferrum/browser.rb | 10 +++++---- sig/ferrum/deadline.rbs | 2 -- sig/ferrum/page.rbs | 2 -- spec/browser_spec.rb | 45 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dd75feb..fbc772e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ otherwise stops sending once the user agent is overridden - `go_to(url, on_timeout: :raise)` raises `NavigationTimeoutError`, a `TimeoutError` naming the requests still pending, when the page doesn't load in time +- `browser.off`, `window_bounds`, `window_bounds=`, `tracing`, `activate` and `document_size` reach the page like the + rest of its methods ### Changed - `client.command` needs a `timeout:` and has no `async: true`, use `client.notify` to send without waiting, and diff --git a/docs/1-introduction.md b/docs/1-introduction.md index c4d636fe..2d0bb821 100644 --- a/docs/1-introduction.md +++ b/docs/1-introduction.md @@ -128,6 +128,19 @@ thread = browser.quit(wait: false) thread.join # only needed if you must wait for cleanup to finish, e.g. before reusing a fixed port ``` +## Raw CDP + +Most `Browser` methods are shortcuts to its default page, `browser.on` included, but `browser.command` isn't: it +talks to the browser itself, for the `Browser.*`, `Target.*` and other browser-wide domains. Send page commands, +like `Page.*`, `Runtime.*` or `DOM.*`, through the page: + +```ruby +browser.command("Browser.getVersion") + +browser.page.command("Runtime.evaluate", expression: "1 + 1") +browser.on("Page.frameNavigated") { |params| puts params["frame"]["url"] } # same as browser.page.on +``` + ## Thread safety Ferrum is fully thread-safe. You can create one browser or a few as you wish and diff --git a/lib/ferrum/browser.rb b/lib/ferrum/browser.rb index 872e10b7..b091a1f4 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -33,15 +33,15 @@ class Browser wait_for_navigation wait_for_navigation! at_css at_xpath css xpath wait_for_selector current_url current_title url title body doctype content= - headers network console accessibility downloads + headers network console accessibility downloads tracing mouse keyboard - screenshot pdf mhtml viewport_size device_pixel_ratio + screenshot pdf mhtml viewport_size device_pixel_ratio document_size start_screencast stop_screencast frames frame_by main_frame evaluate evaluate_handle execute isolated_world add_script_tag add_style_tag bypass_csp evaluate_on_new_document remove_script_on_new_document - on position position= + on off position position= window_bounds window_bounds= activate playback_rate playback_rate= disable_javascript set_viewport resize] => :page @@ -260,7 +260,9 @@ def close end # - # Sends a CDP command to the browser. + # Sends a CDP command to the browser itself, not to the default page like + # most other `Browser` methods, {#on} included. Use `page.command` for page + # domains such as `Page`, `Runtime` or `DOM`. # # @param [String] method # The CDP method name, e.g. `"Browser.getVersion"`. diff --git a/sig/ferrum/deadline.rbs b/sig/ferrum/deadline.rbs index 6308a46b..4a200eb9 100644 --- a/sig/ferrum/deadline.rbs +++ b/sig/ferrum/deadline.rbs @@ -20,8 +20,6 @@ module Ferrum def expired?: () -> bool - protected - attr_reader at: Float end end diff --git a/sig/ferrum/page.rbs b/sig/ferrum/page.rbs index 249607eb..a8637c8b 100644 --- a/sig/ferrum/page.rbs +++ b/sig/ferrum/page.rbs @@ -90,8 +90,6 @@ module Ferrum def window_id: () -> ::Integer - def set_window_bounds: (Hash[Symbol, (::Integer | String)] bounds) -> Hash[String, untyped] - def command: (String method, ?slowmoable: bool, ?timeout: (::Numeric | Deadline)?, **untyped) -> Hash[String, untyped] def on: (:dialog) { (Dialog) -> void } -> Integer diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index 5650adcd..462d0731 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -1156,4 +1156,49 @@ def proxy_tunnel(socket, request, browser_url) expect(browser.accessibility).to be_a(Ferrum::Accessibility) end end + + describe "#tracing" do + it "returns the tracing of the page" do + expect(browser.tracing).to be(browser.page.tracing) + end + end + + describe "#off" do + it "unsubscribes from the page event" do + removed = [] + kept = [] + id = browser.on("Runtime.consoleAPICalled") { |params| removed << params.dig("args", 0, "value") } + browser.on("Runtime.consoleAPICalled") { |params| kept << params.dig("args", 0, "value") } + browser.evaluate("console.log('first')") + wait_for { kept }.to eq(["first"]) + + browser.off("Runtime.consoleAPICalled", id) + browser.evaluate("console.log('second')") + + wait_for { kept }.to eq(%w[first second]) + expect(removed).to eq(["first"]) + end + end + + describe "#document_size" do + it "returns the scroll size of the page" do + browser.content = %(
) + + expect(browser.document_size).to eq(browser.page.document_size) + end + end + + describe "#window_bounds" do + it "returns the window bounds of the page" do + browser.window_bounds = { width: 800, height: 600 } + + expect(browser.window_bounds).to include("width" => 800, "height" => 600) + end + end + + describe "#activate" do + it "brings the page to the front" do + expect(browser.activate).to be(true) + end + end end