Deprecate only_path in favor of ignore_query and auto-detection of url/path matching

This commit is contained in:
Thomas Walpole 2017-06-20 15:14:32 -07:00
parent 3a42f29c01
commit 4f766066a3
3 changed files with 50 additions and 18 deletions

View File

@ -8,22 +8,25 @@ module Capybara
def initialize(expected_path, options = {})
super(options)
@expected_path = expected_path
warn "DEPRECATED: The :only_path option is deprecated in favor of the :ignore_query option" if options.has_key?(:only_path)
@options = {
url: false,
only_path: false }.merge(options)
url: !@expected_path.is_a?(Regexp) && !::Addressable::URI.parse(@expected_path || "").hostname.nil?,
only_path: false,
ignore_query: false }.merge(options)
assert_valid_keys
end
def resolves_for?(session)
uri = ::Addressable::URI.parse(session.current_url)
uri.query = nil if uri && options[:ignore_query]
@actual_path = if options[:url]
session.current_url
uri.to_s
else
uri = ::Addressable::URI.parse(session.current_url)
if options[:only_path]
uri.path unless uri.nil? # Ensure the parsed url isn't nil.
uri && uri.path
else
uri.request_uri unless uri.nil? # Ensure the parsed url isn't nil.
uri && uri.request_uri
end
end
@ -50,7 +53,7 @@ module Capybara
end
def valid_keys
[:wait, :url, :only_path]
[:wait, :url, :only_path, :ignore_query]
end
def assert_valid_keys

View File

@ -3,16 +3,18 @@ module Capybara
module SessionMatchers
##
# Asserts that the page has the given path.
# By default this will compare against the path+query portion of the full url
# By default, if passed a full url this will compare against the full url,
# if passed a path only the path+query portion will be compared, if passed a regexp
# the comparison will depend on the :url option
#
# @!macro current_path_query_params
# @overload $0(string, options = {})
# @param string [String] The string that the current 'path' should equal
# @overload $0(regexp, options = {})
# @param regexp [Regexp] The regexp that the current 'path' should match to
# @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for the current path to eq/match given string/regexp argument
# @option options [Boolean] :url (false) Whether the compare should be done against the full url
# @option options [Boolean] :only_path (false) Whether the compare should be done against just the path protion of the url
# @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for the current url/path to eq/match given string/regexp argument
# @option options [Boolean] :url (true if a full url is passed in, otherwise false) Whether the compare should be done against the full current url or just the path
# @option options [Boolean] :ignore_query (false) Whether the query portion of the current url/path should be ignored
# @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
# @return [true]
#
@ -22,7 +24,9 @@ module Capybara
##
# Asserts that the page doesn't have the given path.
# By default this will compare against the path+query portion of the full url
# By default, if passed a full url this will compare against the full url,
# if passed a path only the path+query portion will be compared, if passed a regexp
# the comparison will depend on the :url option
#
# @macro current_path_query_params
# @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
@ -34,7 +38,9 @@ module Capybara
##
# Checks if the page has the given path.
# By default this will compare against the path+query portion of the full url
# By default, if passed a full url this will compare against the full url,
# if passed a path only the path+query portion will be compared, if passed a regexp
# the comparison will depend on the :url option
#
# @macro current_path_query_params
# @return [Boolean]
@ -47,7 +53,9 @@ module Capybara
##
# Checks if the page doesn't have the given path.
# By default this will compare against the path+query portion of the full url
# By default, if passed a full url this will compare against the full url,
# if passed a path only the path+query portion will be compared, if passed a regexp
# the comparison will depend on the :url option
#
# @macro current_path_query_params
# @return [Boolean]

View File

@ -57,16 +57,27 @@ Capybara::SpecHelper.spec '#has_current_path?' do
expect(@session).to have_current_path('/with_js', url: false)
end
it "should default to full url if value is a url" do
url = @session.current_url
expect(url).to match /with_js$/
expect(@session).to have_current_path(url)
expect(@session).not_to have_current_path("http://www.not_example.com/with_js")
end
it "should ignore the query" do
@session.visit('/with_js?test=test')
expect(@session).to have_current_path('/with_js?test=test')
expect(@session).to have_current_path('/with_js', only_path: true)
expect(@session).to have_current_path('/with_js', ignore_query: true)
uri = ::Addressable::URI.parse(@session.current_url)
uri.query = nil
expect(@session).to have_current_path(uri.to_s, ignore_query: true)
end
it "should not allow url and only_path at the same time" do
expect {
expect(@session).to have_current_path('/with_js', url: true, only_path: true)
}. to raise_error ArgumentError
}.to raise_error ArgumentError
end
it "should not raise an exception if the current_url is nil" do
@ -75,12 +86,17 @@ Capybara::SpecHelper.spec '#has_current_path?' do
# Without only_path option
expect {
expect(@session).to have_current_path(nil)
}. not_to raise_exception
}.not_to raise_exception
# With only_path option
expect {
expect(@session).to have_current_path(nil, only_path: true)
}. not_to raise_exception
}.not_to raise_exception
# With ignore_query option
expect {
expect(@session).to have_current_path(nil, ignore_query: true)
}.not_to raise_exception
end
end
@ -121,5 +137,10 @@ Capybara::SpecHelper.spec '#has_no_current_path?' do
expect {
expect(@session).not_to have_current_path('/with_js', only_path: true)
}. not_to raise_exception
# With ignore_query option
expect {
expect(@session).not_to have_current_path('/with_js', ignore_query: true)
}. not_to raise_exception
end
end