Skip to content

Commit

Permalink
Don't crash when a click closes the window (fixes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
triskweline committed Nov 30, 2023
1 parent eb462be commit dac7c97
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/capybara-lockstep/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara-lockstep/page_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions spec/features/synchronization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
<a href="/start" target="_blank"">open window</a>
<a href="#" onclick="window.close()"">close window</a>
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
Expand Down

0 comments on commit dac7c97

Please sign in to comment.