From fb88d5739f0effbed03d1dc7e75efc3920b5c832 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Thu, 19 Nov 2026 16:35:20 +0300 Subject: [PATCH 1/3] chore(deps): use ferrum main until the next release --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 6dbf95d..63d7431 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source "https://rubygems.org" gem "byebug", "~> 11.1", platforms: %i[mri windows] +gem "ferrum", github: "rubycdp/ferrum", branch: "main" gem "launchy", "~> 2.5" gem "logger" gem "ostruct" From f25d2ca9eb93c069cb41b12e4aaf989520ef3c46 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Thu, 19 Nov 2026 16:35:20 +0300 Subject: [PATCH 2/3] fix(page): handle dialogs with on(:dialog) so Ferrum's default handler stays out Ferrum 0.21 runs on(:dialog) callbacks as a chain and accepts a dialog nobody handled with a warning. Cuprite listened to the raw Page.javascriptDialogOpening event, so Ferrum accepted every dialog first, dismissals and prompt responses were lost, and a test stubbing warn killed the dispatcher thread. --- CHANGELOG.md | 2 ++ lib/capybara/cuprite/page/modals.rb | 43 +++++++++++++++-------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 814dbd0..2c409ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ - `:pending_connection_errors` is handled by Cuprite instead of being passed to Ferrum, which deprecates it. `visit` raises `Capybara::Cuprite::PendingConnectionsError`, a subclass of Ferrum's, and rescuing Ferrum's warns [rubycdp/ferrum#689] +- Dialogs are handled with Ferrum's `on(:dialog)` instead of the raw CDP event, so Ferrum's default handler no longer + accepts them first and loses dismissals and prompt responses [rubycdp/ferrum#703] ### Fixed - A click on a node the page removes before the hit test raises `ObsoleteNode`, which Capybara retries, instead of diff --git a/lib/capybara/cuprite/page/modals.rb b/lib/capybara/cuprite/page/modals.rb index 020d9d2..3b46ff6 100644 --- a/lib/capybara/cuprite/page/modals.rb +++ b/lib/capybara/cuprite/page/modals.rb @@ -14,13 +14,14 @@ def initialize(...) super end - # The `Page.javascriptDialogOpening` event is handled on Ferrum's - # background CDP dispatcher thread, so raising there wouldn't reach - # the caller and would permanently kill that thread instead. The - # dialog is always accepted immediately from that thread, and the - # error (if any) is stashed here to be raised from the main thread - # the next time it makes a command round trip. + # Dialogs are handled on Ferrum's background dispatcher thread, so + # raising there wouldn't reach the caller. The dialog is answered + # from that thread, skipping the pending error check, and the error + # (if any) is stashed here to be raised from the calling thread the + # next time it makes a command round trip. def command(...) + return super if Thread.current[:cuprite_handling_dialog] + raise_pending_unhandled_modal_error! { super } end @@ -78,39 +79,41 @@ def raise_pending_unhandled_modal_error! def prepare_page super - on("Page.javascriptDialogOpening") { |params| handle_javascript_dialog(params) } + on(:dialog) { |dialog| handle_dialog(dialog) } end - def handle_javascript_dialog(params) + def handle_dialog(dialog) accept_modal = @accept_modal.last - unhandled_modal_error = nil if [true, false].include?(accept_modal) @accept_modal.pop - @modal_messages << params["message"] - options = { accept: accept_modal } - response = @modal_response || params["defaultPrompt"] + @modal_messages << dialog.message + accept = accept_modal + response = @modal_response else - with_text = params["message"] ? "with text `#{params['message']}` " : "" + with_text = dialog.message ? "with text `#{dialog.message}` " : "" message = "Modal window #{with_text}has been opened, but you didn't wrap " \ "your code into (`accept_prompt` | `dismiss_prompt` | " \ "`accept_confirm` | `dismiss_confirm` | `accept_alert`), " \ "accepting by default" if @options.raise_on_unhandled_modal - unhandled_modal_error = UnhandledModalError.new(message) + @unhandled_modal_error = UnhandledModalError.new(message) else warn message end - options = { accept: true } - response = params["defaultPrompt"] + accept = true end - options.merge!(promptText: response) if response - # Skips #command so a pending error isn't raised on the dispatcher thread. - client.command("Page.handleJavaScriptDialog", timeout: timeout, **options) - @unhandled_modal_error = unhandled_modal_error if unhandled_modal_error + answer_dialog(dialog, accept, response) + end + + def answer_dialog(dialog, accept, response) + Thread.current[:cuprite_handling_dialog] = true + accept ? dialog.accept(response) : dialog.dismiss + ensure + Thread.current[:cuprite_handling_dialog] = nil end end end From 25b22d3ed7d43a4344810a5b79c46e8e3b5b6029 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Thu, 19 Nov 2026 16:43:23 +0300 Subject: [PATCH 3/3] fix(browser): remove a closed window from the default context instead of a copy of its targets Ferrum 0.21 returns a copy from Context#targets, so deleting from it kept a closed window listed until Chrome reported it destroyed: exists? stayed true and switching to it raised BrowserError instead of NoSuchPageError. --- CHANGELOG.md | 2 ++ lib/capybara/cuprite/browser.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c409ea..5a8600d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ [rubycdp/ferrum#689] - Dialogs are handled with Ferrum's `on(:dialog)` instead of the raw CDP event, so Ferrum's default handler no longer accepts them first and loses dismissals and prompt responses [rubycdp/ferrum#703] +- A closed window is removed from Ferrum's context, which returns a copy of its targets, so `close_window` no longer + leaves it listed until Chrome reports it destroyed [rubycdp/ferrum#707] ### Fixed - A click on a node the page removes before the hit test raises `ObsoleteNode`, which Capybara retries, instead of diff --git a/lib/capybara/cuprite/browser.rb b/lib/capybara/cuprite/browser.rb index 6d8db60..a53fd97 100644 --- a/lib/capybara/cuprite/browser.rb +++ b/lib/capybara/cuprite/browser.rb @@ -77,7 +77,7 @@ def close_window(target_id) @page_closed = true end target.page.close - targets.delete(target_id) # page.close is async, delete target asap + default_context.delete_target(target_id) # page.close is async, delete target asap end private