Fix headless chrome modals when page changes

This commit is contained in:
Thomas Walpole 2017-06-09 11:28:47 -07:00
parent e656a5dd62
commit a09bb5d40a
4 changed files with 20 additions and 2 deletions

View File

@ -368,6 +368,7 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
prompt: window.prompt,
confirm: window.confirm,
alert: window.alert,
called: false
}
window.capybara.add_handler(modal_handler);
@ -420,15 +421,19 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
ignore: Selenium::WebDriver::Error::NoAlertPresentError)
begin
wait.until do
called, alert_text = evaluate_script('window.capybara.current_modal_status()')
called, alert_text = evaluate_script('window.capybara && window.capybara.current_modal_status()')
if called
execute_script('window.capybara.modal_handlers.shift()')
execute_script('window.capybara && window.capybara.modal_handlers.shift()')
regexp = options[:text].is_a?(Regexp) ? options[:text] : Regexp.escape(options[:text].to_s)
if alert_text.match(regexp)
alert_text
else
raise Capybara::ModalNotFound.new("Unable to find modal dialog#{" with #{options[:text]}" if options[:text]}")
end
elsif called.nil?
# page changed so modal_handler data has gone away
warn "Can't verify modal text when page change occurs - ignoring" if options[:text]
""
else
nil
end

View File

@ -87,6 +87,10 @@ $(function() {
$(link).attr('opened', 'true');
}, 3000);
});
$('#alert-page-change').click(function() {
alert('Page is changing');
return true;
});
$('#open-confirm').click(function() {
if(confirm('Confirm opened')) {
$(this).attr('confirmed', 'true');

View File

@ -47,6 +47,14 @@ Capybara::SpecHelper.spec '#accept_alert', requires: [:modals] do
expect(message).to eq('Alert opened [*Yay?*]')
end
it "should handle the alert if the page changes" do
@session.accept_alert do
@session.click_link('Alert page change')
sleep 1 # ensure page change occurs before the accept_alert block exits
end
expect(@session).to have_current_path('/with_html')
end
context "with an asynchronous alert" do
it "should accept the alert" do
@session.accept_alert do

View File

@ -76,6 +76,7 @@
<p>
<a href="#" id="open-alert">Open alert</a>
<a href="/with_html" id="alert-page-change">Alert page change</a>
</p>
<p>