Improve resetting of page, closes #1035

Navigate to an empty HTML instead of navigating to about:blank. Hopefully this should work better than before.
This commit is contained in:
Jonas Nicklas 2013-10-20 19:29:22 +02:00
parent 823c18bfc3
commit 6b1e42dd31
5 changed files with 35 additions and 7 deletions

View File

@ -14,6 +14,8 @@ module Capybara
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
@ -274,7 +276,7 @@ module Capybara
ensure
self.session_name = :default
end
##
#
# Parse raw html into a document using Nokogiri, and adjust textarea contents as defined by the spec.
@ -284,7 +286,7 @@ module Capybara
#
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

4
lib/capybara/empty.html Normal file
View File

@ -0,0 +1,4 @@
<!doctype>
<html>
<!-- an empty HTML document, used for resetting the page content -->
</html>

View File

@ -1,3 +1,5 @@
require "uri"
class Capybara::Selenium::Driver < Capybara::Driver::Base
DEFAULT_OPTIONS = {
:browser => :firefox
@ -87,7 +89,9 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
# 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

View File

@ -75,8 +75,11 @@ module Capybara
# 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/*"
end
raise @server.error if Capybara.raise_server_errors and @server and @server.error
ensure
@server.reset_error! if @server

View File

@ -16,9 +16,18 @@ Capybara::SpecHelper.spec '#reset_session!' do
@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
@ -31,6 +40,12 @@ Capybara::SpecHelper.spec '#reset_session!' do
@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