Skip to content
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 22 additions & 17 deletions lib/capybara/cuprite/page/modals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -78,20 +79,20 @@ 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`), " \
Expand All @@ -103,15 +104,19 @@ def handle_javascript_dialog(params)
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)

answer_dialog(dialog, accept, response)
@unhandled_modal_error = unhandled_modal_error if unhandled_modal_error
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
end
Expand Down
Loading