From 6b1e42dd3195cc6589fbacbac69a5aca05c0d2e7 Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Sun, 20 Oct 2013 19:29:22 +0200 Subject: [PATCH] Improve resetting of page, closes #1035 Navigate to an empty HTML instead of navigating to about:blank. Hopefully this should work better than before. --- lib/capybara.rb | 6 ++++-- lib/capybara/empty.html | 4 ++++ lib/capybara/selenium/driver.rb | 6 +++++- lib/capybara/session.rb | 7 +++++-- .../spec/session/reset_session_spec.rb | 19 +++++++++++++++++-- 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 lib/capybara/empty.html diff --git a/lib/capybara.rb b/lib/capybara.rb index 51536983c..50446f6f3 100644 --- a/lib/capybara.rb +++ b/lib/capybara.rb @@ -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 @@ -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. @@ -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 diff --git a/lib/capybara/empty.html b/lib/capybara/empty.html new file mode 100644 index 000000000..715726911 --- /dev/null +++ b/lib/capybara/empty.html @@ -0,0 +1,4 @@ + + + + diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index 2345db0b0..727d26af2 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -1,3 +1,5 @@ +require "uri" + class Capybara::Selenium::Driver < Capybara::Driver::Base DEFAULT_OPTIONS = { :browser => :firefox @@ -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 diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 317be89cc..7803eed1a 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -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/*" + end raise @server.error if Capybara.raise_server_errors and @server and @server.error ensure @server.reset_error! if @server diff --git a/lib/capybara/spec/session/reset_session_spec.rb b/lib/capybara/spec/session/reset_session_spec.rb index 95bd67221..f8ae4e0f3 100644 --- a/lib/capybara/spec/session/reset_session_spec.rb +++ b/lib/capybara/spec/session/reset_session_spec.rb @@ -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 @@ -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