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

Chrome provides us the ability to clear ALL cookies - so use it

This commit is contained in:
Thomas Walpole 2018-10-02 12:23:19 -07:00
parent 29853e4fad
commit 54a368b847
3 changed files with 40 additions and 2 deletions

View file

@ -263,7 +263,7 @@ private
end
def clear_browser_state
@browser.manage.delete_all_cookies
delete_all_cookies
clear_storage
rescue Selenium::WebDriver::Error::UnhandledError # rubocop:disable Lint/HandleExceptions
# delete_all_cookies fails when we've previously gone
@ -271,6 +271,10 @@ private
# instead.
end
def delete_all_cookies
@browser.manage.delete_all_cookies
end
def clear_storage
clear_session_storage if options[:clear_session_storage]
clear_local_storage if options[:clear_local_storage]

View file

@ -10,7 +10,6 @@ module Capybara::Selenium::Driver::ChromeDriver
rescue NoMethodError => err
raise unless err.message =~ /full_screen_window/
bridge = browser.send(:bridge)
result = bridge.http.call(:post, "session/#{bridge.session_id}/window/fullscreen", {})
result['value']
end
@ -39,7 +38,24 @@ module Capybara::Selenium::Driver::ChromeDriver
private
def delete_all_cookies
execute_cdp('Network.clearBrowserCookies')
rescue Selenium::WebDriver::Error::UnhandledError, Selenium::WebDriver::Error::WebDriverError
# If the CDP clear isn't supported do original limited clear
super
end
def execute_cdp(cmd, params = {})
args = { cmd: cmd, params: params }
result = bridge.http.call(:post, "session/#{bridge.session_id}/goog/cdp/execute", args)
result['value']
end
def build_node(native_node)
::Capybara::Selenium::ChromeNode.new(self, native_node)
end
def bridge
browser.send(:bridge)
end
end

View file

@ -112,4 +112,22 @@ RSpec.describe 'Capybara::Session with chrome' do
expect(Time.parse(extract_results(@session)['datetime'])).to eq datetime
end
end
describe '#reset_session!' do
it 'removes all cookies' do
@session = TestSessions::Chrome
domains = ['localhost', '127.0.0.1']
domains.each do |domain|
@session.visit("http://#{domain}:#{@session.server.port}/set_cookie")
@session.visit("http://#{domain}:#{@session.server.port}/get_cookie")
expect(@session).to have_content('test_cookie')
end
@session.reset_session!
domains.each do |domain|
@session.visit("http://#{domain}:#{@session.server.port}/get_cookie")
expect(@session.body).not_to include('test_cookie')
end
end
end
end