Skip to content

Commit a9fffad

Browse files
committed
Introduce raise_on_unhandled_modal browser configuration
The problem --- When executing a system test suite that relies on `confirm`, `prompt`, or other browser-level modals, the default behavior to ignore modally presented dialogs can cause false negatives. For example, a change to the implementation might accidentally introduce a perpetually prompting confirmation modal. While the test suite outputs "Modal window … has been opened" warnings, the underlying test still passes. The proposal --- This commit proposes a new Cuprite-level `:raise_on_unhandled_modal` option to control whether an unhandled modal warns, or raises. When set to `true`, then false negative test would fail, rather than pass.
1 parent b64f03d commit a9fffad

5 files changed

Lines changed: 22 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ end
6161
`Cuprite`-specific options are:
6262

6363
* options `Hash`
64+
* `:raise_on_unhandled_modal` (Boolean) - When set to `false`, output a warning. When set to `true`, raise an exception
6465
* `:url_blacklist` (Array) - array of regexes to match against requested URLs
6566
* `:url_whitelist` (Array) - array of regexes to match against requested URLs
6667

lib/capybara/cuprite/browser.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ module Cuprite
77
class Browser < Ferrum::Browser
88
extend Forwardable
99

10+
attr_accessor :raise_on_unhandled_modal
11+
1012
delegate %i[send_keys select set hover trigger before_click switch_to_frame
1113
find_modal accept_confirm dismiss_confirm accept_prompt
1214
dismiss_prompt reset_modals] => :page
1315

1416
def initialize(options = nil)
17+
@raise_on_unhandled_modal = options&.delete(:raise_on_unhandled_modal)
18+
1519
super
1620

1721
@options.url_blacklist = prepare_wildcards(options&.dig(:url_blacklist))

lib/capybara/cuprite/driver.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def reset!
137137
@paper_size = nil
138138
browser.url_blacklist = @options[:url_blacklist]
139139
browser.url_whitelist = @options[:url_whitelist]
140+
browser.raise_on_unhandled_modal = @options.fetch(:raise_on_unhandled_modal, false)
140141
browser.reset
141142
@started = false
142143
end

lib/capybara/cuprite/page.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,13 @@ def prepare_page
162162
response = @modal_response || params["defaultPrompt"]
163163
else
164164
with_text = params["message"] ? "with text `#{params['message']}` " : ""
165-
warn "Modal window #{with_text}has been opened, but you didn't wrap " \
165+
message = "Modal window #{with_text}has been opened, but you didn't wrap " \
166166
"your code into (`accept_prompt` | `dismiss_prompt` | " \
167167
"`accept_confirm` | `dismiss_confirm` | `accept_alert`), " \
168168
"accepting by default"
169+
170+
browser.raise_on_unhandled_modal ? raise(message) : warn(message)
171+
169172
options = { accept: true }
170173
response = params["defaultPrompt"]
171174
end

spec/features/session_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,18 @@
11101110
expect(@session).to have_xpath("//a[@id='open-match' and @confirmed='true']")
11111111
end
11121112

1113+
it "configured to raise warning" do
1114+
@session.driver.browser.raise_on_unhandled_modal = true
1115+
1116+
@session.visit "/cuprite/with_js"
1117+
1118+
expect { @session.click_link("Open for match") }.to raise_error(
1119+
"Modal window with text `{T}ext \\w|th [reg.exp] (chara©+er$)?` has been opened, " \
1120+
"but you didn't wrap your code into (`accept_prompt` | `dismiss_prompt` | `accept_confirm` " \
1121+
"| `dismiss_confirm` | `accept_alert`), accepting by default"
1122+
)
1123+
end
1124+
11131125
it "matches on partial strings" do
11141126
@session.visit "/cuprite/with_js"
11151127
expect do

0 commit comments

Comments
 (0)