Skip to content

Commit

Permalink
allow session OR local clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Apr 6, 2017
1 parent 9bf791f commit d19c053
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class << self
# [automatic_label_click = Boolean] Whether Node#choose, Node#check, Node#uncheck will attempt to click the associated label element if the checkbox/radio button are non-visible (Default: false)
# [enable_aria_label = Boolean] Whether fields, links, and buttons will match against aria-label attribute (Default: false)
# [reuse_server = Boolean] Reuse the server thread between multiple sessions using the same app object (Default: true)
# [clear_storage_on_reset = Boolean] Clear localStorage and sessionStorage when session is reset (Default: false)
# [clear_storage_on_reset = [Boolean, :local, :storage]] Clear localStorage and sessionStorage when session is reset (Default: false)
# === DSL Options
#
# when using capybara/dsl, the following options are also available:
Expand Down
8 changes: 7 additions & 1 deletion lib/capybara/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ def wait?
def reset!
end

def clear_storage
##
#
# Clear local and/or session storage
# @param only_clear [nil, :local, :session]
#
#
def clear_storage(only_clear = nil)
yield
end

Expand Down
7 changes: 3 additions & 4 deletions lib/capybara/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ def pending_requests?
def call(env)
if env["PATH_INFO"] == "/__identify__"
[200, {}, [@app.object_id.to_s]]
elsif
env["PATH_INFO"] == "/__clear_storage__"
elsif m = env["PATH_INFO"].match(%r{/__clear_storage__(?:/(local|session))?})
[200, {}, [ <<-HTML
<html>
<head>
<title>Clear Storage</title>
<script>
window.localStorage.clear();
window.sessionStorage.clear();
#{"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>
Expand Down
13 changes: 9 additions & 4 deletions lib/capybara/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ def driver
#
def reset!
if @touched
clear_storage if @server && Capybara.clear_storage_on_reset
if @server && Capybara.clear_storage_on_reset
what_to_clear = Capybara.clear_storage_on_reset
what_to_clear = nil if what_to_clear == true
clear_storage(what_to_clear)
end
driver.reset!
@touched = false
end
Expand Down Expand Up @@ -848,11 +852,12 @@ def computed_uri(visit_uri)
visit_uri
end

def clear_storage
def clear_storage(only_clear = nil)
if
begin
driver.clear_storage do
driver.visit(computed_uri("/__clear_storage__"))
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}"
Expand Down
28 changes: 27 additions & 1 deletion lib/capybara/spec/session/reset_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
end

context "Capybara.clear_storage_on_reset" do
it "resets storage when true", requires: [:server] do
it "resets all storage when true", requires: [:server] do
Capybara.clear_storage_on_reset = true
@session.visit("/set_storage")
expect(@session.evaluate_script(
Expand All @@ -125,5 +125,31 @@
"[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']
@session.reset!
@session.visit("/")
expect(@session.evaluate_script(
"[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']
@session.reset!
@session.visit("/")
expect(@session.evaluate_script(
"[window.localStorage.getItem('capybara'), window.sessionStorage.getItem('capybara_unload')]"
)).to eq ['42', nil]
end
end
end

0 comments on commit d19c053

Please sign in to comment.