diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index 77177380..083f33ff 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -6,8 +6,8 @@ require 'English' class Capybara::Selenium::Driver < Capybara::Driver::Base DEFAULT_OPTIONS = { browser: :firefox, - clear_local_storage: false, - clear_session_storage: false + clear_local_storage: nil, + clear_session_storage: nil }.freeze SPECIAL_OPTIONS = %i[browser clear_local_storage clear_session_storage].freeze attr_reader :app, :options @@ -260,15 +260,17 @@ private end def clear_storage - clear_session_storage if options[:clear_session_storage] - clear_local_storage if options[:clear_local_storage] + clear_session_storage unless options[:clear_session_storage] == false + clear_local_storage unless options[:clear_local_storage] == false + rescue Selenium::WebDriver::Error::JavascriptError # rubocop:disable Lint/HandleExceptions + # session/local storage may not be available if on non-http pages (e.g. about:blank) end def clear_session_storage if @browser.respond_to? :session_storage @browser.session_storage.clear else - warn 'sessionStorage clear requested but is not available for this driver' + warn 'sessionStorage clear requested but is not supported by this driver' unless options[:clear_session_storage].nil? end end @@ -276,7 +278,7 @@ private if @browser.respond_to? :local_storage @browser.local_storage.clear else - warn 'localStorage clear requested but is not available for this driver' + warn 'localStorage clear requested but is not supported by this driver' unless options[:clear_local_storage].nil? end end diff --git a/spec/selenium_spec_chrome.rb b/spec/selenium_spec_chrome.rb index 0f0d350b..4959124e 100644 --- a/spec/selenium_spec_chrome.rb +++ b/spec/selenium_spec_chrome.rb @@ -17,12 +17,12 @@ Capybara.register_driver :selenium_chrome do |app| end end -Capybara.register_driver :selenium_chrome_clear_storage do |app| +Capybara.register_driver :selenium_chrome_not_clear_storage do |app| chrome_options = { browser: :chrome, options: browser_options } - Capybara::Selenium::Driver.new(app, chrome_options.merge(clear_local_storage: true, clear_session_storage: true)) + Capybara::Selenium::Driver.new(app, chrome_options.merge(clear_local_storage: false, clear_session_storage: false)) end module TestSessions @@ -49,28 +49,24 @@ RSpec.describe 'Capybara::Session with chrome' do context 'storage' do describe '#reset!' do - it 'does not clear either storage by default' do + it 'clears storage by default' do @session = TestSessions::Chrome @session.visit('/with_js') @session.find(:css, '#set-storage').click @session.reset! @session.visit('/with_js') - # expect(@session.driver.browser.local_storage.keys).not_to be_empty - # expect(@session.driver.browser.session_storage.keys).not_to be_empty - expect(@session.evaluate_script('Object.keys(localStorage)')).not_to be_empty - expect(@session.evaluate_script('Object.keys(sessionStorage)')).not_to be_empty + expect(@session.evaluate_script('Object.keys(localStorage)')).to be_empty + expect(@session.evaluate_script('Object.keys(sessionStorage)')).to be_empty end - it 'clears storage when set' do - @session = Capybara::Session.new(:selenium_chrome_clear_storage, TestApp) + it 'does not clear storage when false' do + @session = Capybara::Session.new(:selenium_chrome_not_clear_storage, TestApp) @session.visit('/with_js') @session.find(:css, '#set-storage').click @session.reset! @session.visit('/with_js') - # expect(@session.driver.browser.local_storage.keys).to be_empty - # expect(@session.driver.browser.session_storage.keys).to be_empty - expect(@session.evaluate_script('Object.keys(localStorage)')).to be_empty - expect(@session.evaluate_script('Object.keys(sessionStorage)')).to be_empty + expect(@session.evaluate_script('Object.keys(localStorage)')).not_to be_empty + expect(@session.evaluate_script('Object.keys(sessionStorage)')).not_to be_empty end end end diff --git a/spec/selenium_spec_firefox.rb b/spec/selenium_spec_firefox.rb index 99d4468d..7d363064 100644 --- a/spec/selenium_spec_firefox.rb +++ b/spec/selenium_spec_firefox.rb @@ -26,12 +26,12 @@ Capybara.register_driver :selenium_firefox do |app| ) end -Capybara.register_driver :selenium_firefox_clear_storage do |app| +Capybara.register_driver :selenium_firefox_not_clear_storage do |app| Capybara::Selenium::Driver.new( app, browser: :firefox, - clear_local_storage: true, - clear_session_storage: true, + clear_local_storage: false, + clear_session_storage: false, options: browser_options ) end @@ -125,25 +125,25 @@ RSpec.describe Capybara::Selenium::Driver do context 'storage' do describe '#reset!' do - it 'does not clear either storage by default' do + it 'clears storage by default' do @session = TestSessions::SeleniumFirefox @session.visit('/with_js') @session.find(:css, '#set-storage').click @session.reset! @session.visit('/with_js') - expect(@session.driver.browser.local_storage.keys).not_to be_empty - expect(@session.driver.browser.session_storage.keys).not_to be_empty - end - - it 'clears storage when set' do - @session = Capybara::Session.new(:selenium_firefox_clear_storage, TestApp) - @session.visit('/with_js') - @session.find(:css, '#set-storage').click - @session.reset! - @session.visit('/with_js') expect(@session.driver.browser.local_storage.keys).to be_empty expect(@session.driver.browser.session_storage.keys).to be_empty end + + it 'does not clear storage when false' do + @session = Capybara::Session.new(:selenium_firefox_not_clear_storage, TestApp) + @session.visit('/with_js') + @session.find(:css, '#set-storage').click + @session.reset! + @session.visit('/with_js') + expect(@session.driver.browser.local_storage.keys).not_to be_empty + expect(@session.driver.browser.session_storage.keys).not_to be_empty + end end end end