From dac7c97ee2f2d03f50d131c36dad20dbf872d61d Mon Sep 17 00:00:00 2001 From: Henning Koch Date: Thu, 30 Nov 2023 14:36:29 +0100 Subject: [PATCH] Don't crash when a click closes the window (fixes #10) --- lib/capybara-lockstep/client.rb | 7 ++++++- lib/capybara-lockstep/page_access.rb | 2 +- spec/features/synchronization_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/capybara-lockstep/client.rb b/lib/capybara-lockstep/client.rb index 074267f..d75d89a 100644 --- a/lib/capybara-lockstep/client.rb +++ b/lib/capybara-lockstep/client.rb @@ -7,6 +7,7 @@ class Client ERROR_SNIPPET_MISSING = 'Cannot synchronize: capybara-lockstep JavaScript snippet is missing' ERROR_PAGE_MISSING = 'Cannot synchronize with empty page' ERROR_ALERT_OPEN = 'Cannot synchronize while an alert is open' + ERROR_WINDOW_CLOSED = 'Cannot synchronize with closed window' ERROR_NAVIGATED_AWAY = "Browser navigated away while synchronizing" SYNCHRONIZED_IVAR = :@lockstep_synchronized_client @@ -94,7 +95,11 @@ def synchronize end rescue ::Selenium::WebDriver::Error::UnexpectedAlertOpenError log ERROR_ALERT_OPEN - # Don't raise an error, this will happen in an innocent test. + # Don't raise an error, this will happen in an innocent test where a click opens an alert. + # We will retry on the next Capybara synchronize call. + rescue ::Selenium::WebDriver::Error::NoSuchWindowError + log ERROR_WINDOW_CLOSED + # Don't raise an error, this will happen in an innocent test where a click closes a window. # We will retry on the next Capybara synchronize call. rescue ::Selenium::WebDriver::Error::JavascriptError => e # When the URL changes while a script is running, my current selenium-webdriver diff --git a/lib/capybara-lockstep/page_access.rb b/lib/capybara-lockstep/page_access.rb index f60a22a..547cdfd 100644 --- a/lib/capybara-lockstep/page_access.rb +++ b/lib/capybara-lockstep/page_access.rb @@ -19,7 +19,7 @@ def alert_present? # to its `getLog` API. This causes Selenium to time out with a `Net::ReadTimeout` error page.driver.browser.switch_to.alert true - rescue Capybara::NotSupportedByDriverError, ::Selenium::WebDriver::Error::NoSuchAlertError + rescue Capybara::NotSupportedByDriverError, ::Selenium::WebDriver::Error::NoSuchAlertError, ::Selenium::WebDriver::Error::NoSuchWindowError false end diff --git a/spec/features/synchronization_spec.rb b/spec/features/synchronization_spec.rb index 9de07ee..7919cb7 100644 --- a/spec/features/synchronization_spec.rb +++ b/spec/features/synchronization_spec.rb @@ -92,6 +92,26 @@ page.accept_confirm('OK to proceed?') end + it 'does not crash if the click closes the window' do + App.start_html = <<~HTML + open window + close window + HTML + + visit '/start' + + window = window_opened_by do + find('a', text: 'open window').click + end + + expect do + within_window(window) do + find('a', text: 'close window').click + end + end.to_not raise_error + + end + end describe 'when reading elements' do