Skip to content

Commit

Permalink
Improve resetting of page, closes #1035
Browse files Browse the repository at this point in the history
Navigate to an empty HTML instead of navigating to about:blank. Hopefully this should work better than before.
  • Loading branch information
jnicklas committed Oct 20, 2013
1 parent 823c18b commit 6b1e42d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/capybara.rb
Expand Up @@ -14,6 +14,8 @@ class UnselectNotAllowed < CapybaraError; end
class NotSupportedByDriverError < CapybaraError; end
class InfiniteRedirectError < CapybaraError; end

EMPTY_HTML_FILE_PATH = File.expand_path('./capybara/empty.html', File.dirname(__FILE__))

class << self
attr_accessor :asset_host, :app_host, :run_server, :default_host, :always_include_port
attr_accessor :server_port, :exact, :match, :exact_options, :visible_text_only
Expand Down Expand Up @@ -274,7 +276,7 @@ def using_session(name)
ensure
self.session_name = :default
end

##
#
# Parse raw html into a document using Nokogiri, and adjust textarea contents as defined by the spec.
Expand All @@ -284,7 +286,7 @@ def using_session(name)
#
def HTML(html)
Nokogiri::HTML(html).tap do |document|
document.xpath('//textarea').each do |textarea|
document.xpath('//textarea').each do |textarea|
textarea.content=textarea.content.sub(/\A\n/,'')
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/empty.html
@@ -0,0 +1,4 @@
<!doctype>
<html>
<!-- an empty HTML document, used for resetting the page content -->
</html>
6 changes: 5 additions & 1 deletion lib/capybara/selenium/driver.rb
@@ -1,3 +1,5 @@
require "uri"

class Capybara::Selenium::Driver < Capybara::Driver::Base
DEFAULT_OPTIONS = {
:browser => :firefox
Expand Down Expand Up @@ -87,7 +89,9 @@ def reset!
# to about:blank, so we rescue this error and do nothing
# instead.
end
@browser.navigate.to('about:blank')
uri = URI(Capybara::EMPTY_HTML_FILE_PATH)
uri.scheme = "file"
@browser.navigate.to(uri.to_s)
end
end

Expand Down
7 changes: 5 additions & 2 deletions lib/capybara/session.rb
Expand Up @@ -75,8 +75,11 @@ def driver
# Reset the session, removing all cookies.
#
def reset!
driver.reset! if @touched
@touched = false
if @touched
driver.reset!
@touched = false
assert_no_selector :xpath, "/html/body/*"

This comment has been minimized.

Copy link
@abotalov

abotalov Feb 18, 2014

Collaborator

What is the purpose of this line? Looks like it just tests that driver resets page correctly.

This comment has been minimized.

Copy link
@jnicklas

jnicklas Feb 18, 2014

Author Collaborator

In a sense yes. It makes sure that reset! is synchronous. It felt easier adding this check here than adding the synchronization to all drivers individually.

end
raise @server.error if Capybara.raise_server_errors and @server and @server.error
ensure
@server.reset_error! if @server
Expand Down
19 changes: 17 additions & 2 deletions lib/capybara/spec/session/reset_session_spec.rb
Expand Up @@ -16,9 +16,18 @@
@session.current_path.should == '/foo'

@session.reset_session!
[nil, '', 'about:blank'].should include @session.current_url
[
->(v) { v == nil },
->(v) { v == '' },
->(v) { v == 'about:blank' },
->(v) { v.end_with? Capybara::EMPTY_HTML_FILE_PATH } # allow file:// protocol
].any? { |p| p.(@session.current_url) }.should be_true
[
->(v) { v == '' },
->(v) { v == nil },
->(v) { v == Capybara::EMPTY_HTML_FILE_PATH }
].any? { |p| p.(@session.current_path) }.should be_true
@session.current_host.should be_nil
@session.current_path.should be_nil
end

it "resets page body" do
Expand All @@ -31,6 +40,12 @@
@session.should have_no_selector('.//h1')
end

it "is synchronous" do
@session.visit("/with_html")
@session.reset_session!
@session.should have_no_selector :xpath, "/html/body/*", wait: false
end

it "raises any errors caught inside the server", :requires => [:server] do
quietly { @session.visit("/error") }
expect do
Expand Down

0 comments on commit 6b1e42d

Please sign in to comment.