1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Make Selenium driver#reset! synchronous and test with latest firefox

This commit is contained in:
Thomas Walpole 2016-03-30 14:55:29 -07:00
parent 8d5a7e18ad
commit 88eb97aaa4
5 changed files with 60 additions and 7 deletions

View file

@ -29,6 +29,7 @@ env:
- NOKOGIRI_USE_SYSTEM_LIBRARIES=true
addons:
firefox: latest
apt:
sources:
- google-chrome

View file

@ -92,20 +92,35 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
def reset!
# Use instance variable directly so we avoid starting the browser just to reset the session
if @browser
navigated = false
start_time = Capybara::Helpers.monotonic_time
begin
begin @browser.manage.delete_all_cookies
rescue Selenium::WebDriver::Error::UnhandledError
# delete_all_cookies fails when we've previously gone
# to about:blank, so we rescue this error and do nothing
# instead.
if !navigated
# Only trigger a navigation if we haven't done it already, otherwise it
# can trigger an endless series of unload modals
begin
@browser.manage.delete_all_cookies
rescue Selenium::WebDriver::Error::UnhandledError
# delete_all_cookies fails when we've previously gone
# to about:blank, so we rescue this error and do nothing
# instead.
end
@browser.navigate.to("about:blank")
end
navigated = true
#Ensure the page is empty and trigger an UnhandledAlertError for any modals that appear during unload
until find_xpath("/html/body/*").empty? do
raise Capybara::ExpectationNotMet.new('Timed out waiting for Selenium session reset') if (Capybara::Helpers.monotonic_time - start_time) >= 10
sleep 0.05
end
@browser.navigate.to("about:blank")
rescue Selenium::WebDriver::Error::UnhandledAlertError
# This error is thrown if an unhandled alert is on the page
# Firefox appears to automatically dismiss this alert, chrome does not
# We'll try to accept it
begin
@browser.switch_to.alert.accept
sleep 0.25 # allow time for the modal to be handled
rescue Selenium::WebDriver::Error::NoAlertPresentError
# The alert is now gone - nothing to do
end

View file

@ -33,11 +33,19 @@ Capybara::SpecHelper.spec '#reset_session!' do
end
it "is synchronous" do
@session.visit("/with_html")
@session.visit("/with_slow_unload")
expect(@session).to have_selector(:css, 'div')
@session.reset_session!
expect(@session).to have_no_selector :xpath, "/html/body/*", wait: false
end
it "handles modals during unload" do
@session.visit('/with_unload_alert')
expect(@session).to have_selector(:css, 'div')
expect { @session.reset_session! }.not_to raise_error
expect(@session).to have_no_selector :xpath, "/html/body/*", wait: false
end
it "raises any standard errors caught inside the server", :requires => [:server] do
quietly { @session.visit("/error") }
expect do

View file

@ -0,0 +1,17 @@
<html>
<head>
<script>
function delay_unload() {
var start = new Date();
var i = 0;
while ((new Date()) - start < 1000){ i = i + 1; };
return null;
}
window.onbeforeunload = delay_unload;
window.onunload = delay_unload;
</script>
</head>
<body>
<div>This delays unload by 2 seconds</div>
</body>
</html>

View file

@ -0,0 +1,12 @@
<html>
<head>
<script>
window.onbeforeunload = function(e) {
return 'Unload?';
};
</script>
</head>
<body>
<div>This triggers an alert on unload</div>
</body>
</html>