Skip to content

Commit

Permalink
rebase cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Nov 7, 2018
1 parent e95ec9f commit 174b98e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/capybara/selenium/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def native_args(args)

def clear_browser_state
delete_all_cookies
clear_storage
deprecated_clear_storage
rescue Selenium::WebDriver::Error::UnhandledError # rubocop:disable Lint/HandleExceptions
# delete_all_cookies fails when we've previously gone
# to about:blank, so we rescue this error and do nothing
Expand All @@ -259,7 +259,7 @@ def delete_all_cookies
@browser.manage.delete_all_cookies
end

def clear_storage
def deprecated_clear_storage
clear_session_storage if options[:clear_session_storage]
clear_local_storage if options[:clear_local_storage]
end
Expand Down
10 changes: 5 additions & 5 deletions lib/capybara/server/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ def clear_error
def call(env)
if env['PATH_INFO'] == '/__identify__'
[200, {}, [@app.object_id.to_s]]
elsif m = env["PATH_INFO"].match(%r{/__clear_storage__(?:/(local|session))?})
[200, {}, [ <<-HTML
elsif (m = env["PATH_INFO"].match(%r{/__clear_storage__(?:/(local|session))?}))
[200, {}, [<<~HTML
<html>
<head>
<title>Clear Storage</title>
<script>
#{"if (window.localStorage) window.localStorage.clear();" if m[1].nil? || m[1]=='local'}
#{"if (window.sessionStorage) window.sessionStorage.clear();" if m[1].nil? || m[1]=='session'}
#{'if (window.localStorage) window.localStorage.clear();' if m[1].nil? || m[1] == 'local'}
#{'if (window.sessionStorage) window.sessionStorage.clear();' if m[1].nil? || m[1] == 'session'}
</script>
</head>
<body>
Clearing Storage
</body>
</html>
HTML
HTML
]]
else
@counter.increment
Expand Down
14 changes: 5 additions & 9 deletions lib/capybara/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -862,16 +862,12 @@ def computed_uri(visit_uri)
end

def clear_storage(only_clear = nil)
if
begin
driver.clear_storage(only_clear) do
uri = "/__clear_storage__#{ "/#{only_clear}" if !only_clear.nil?}"
driver.visit(computed_uri(uri))
end
rescue => e
warn "Session storage may not have been cleared due to #{e.message}"
end
driver.clear_storage(only_clear) do
uri = "/__clear_storage__#{"/#{only_clear}" unless only_clear.nil?}"
driver.visit(computed_uri(uri))
end
rescue StandardError => e
warn "Session storage may not have been cleared due to #{e.message}"
end

def _find_frame(*args)
Expand Down
6 changes: 4 additions & 2 deletions lib/capybara/session/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class SessionConfig
OPTIONS = %i[always_include_port run_server default_selector default_max_wait_time ignore_hidden_elements
automatic_reload match exact exact_text raise_server_errors visible_text_only
automatic_label_click enable_aria_label save_path asset_host default_host app_host
server_host server_port server_errors default_set_options disable_animation test_id
predicates_wait default_normalize_ws].freeze
server_host server_port server_errors default_set_options disable_animation clear_storage_on_reset
test_id predicates_wait default_normalize_ws].freeze

attr_accessor(*OPTIONS)

Expand Down Expand Up @@ -59,6 +59,8 @@ class SessionConfig
# See {Capybara.configure}
# @!method default_normalize_ws
# See {Capybara.configure}
# @!method clear_storage_on_reset
# See {Capybara.configure}

remove_method :server_host

Expand Down
24 changes: 12 additions & 12 deletions lib/capybara/spec/session/reset_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,39 +150,39 @@
Capybara.clear_storage_on_reset = true
@session.visit("/set_storage")
expect(@session.evaluate_script(
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq ['42', '42']
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq %w[42 42]
@session.reset!
@session.visit("/")
expect(@session.evaluate_script(
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq [nil, nil]
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq [nil, nil]
end

it "resets local storage when :local", requires: [:server] do
Capybara.clear_storage_on_reset = :local
@session.visit("/set_storage")
expect(@session.evaluate_script(
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq ['42', '42']
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq %w[42 42]
@session.reset!
@session.visit("/")
expect(@session.evaluate_script(
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq [nil, '43']
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq [nil, '43']
end

it "resets session storage when :session", requires: [:server] do
Capybara.clear_storage_on_reset = :session
@session.visit("/set_storage")
expect(@session.evaluate_script(
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq ['42', '42']
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq %w[42 42]
@session.reset!
@session.visit("/")
expect(@session.evaluate_script(
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq ['42', nil]
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq ['42', nil]
end
end
end

0 comments on commit 174b98e

Please sign in to comment.