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

Selenium driver clear session and local storage on reset by default

This commit is contained in:
Thomas Walpole 2018-11-20 09:44:52 -08:00
parent 8d9b245ada
commit 321e903c4a
3 changed files with 31 additions and 33 deletions

View file

@ -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

View file

@ -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

View file

@ -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