2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2018-01-08 15:23:54 -05:00
2015-08-24 01:14:48 -04:00
module Capybara
module SessionMatchers
##
# Asserts that the page has the given path.
2017-06-20 18:14:32 -04:00
# 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
2018-10-27 13:03:37 -04:00
# the comparison will depend on the :url option (path+query by default)
2015-08-24 01:14:48 -04:00
#
# @!macro current_path_query_params
2018-05-17 17:45:53 -04:00
# @overload $0(string, **options)
2015-08-24 01:14:48 -04:00
# @param string [String] The string that the current 'path' should equal
2018-05-17 17:45:53 -04:00
# @overload $0(regexp, **options)
2015-08-24 01:14:48 -04:00
# @param regexp [Regexp] The regexp that the current 'path' should match to
2017-10-08 17:02:48 -04:00
# @option options [Boolean] :url (true if `string` ia a full url, otherwise false) Whether the compare should be done against the full current url or just the path
2017-06-20 18:14:32 -04:00
# @option options [Boolean] :ignore_query (false) Whether the query portion of the current url/path should be ignored
2017-10-08 17:02:48 -04:00
# @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
2015-08-24 01:14:48 -04:00
# @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
# @return [true]
#
2016-08-17 19:14:39 -04:00
def assert_current_path ( path , ** options )
2018-11-12 13:34:15 -05:00
_verify_current_path ( path , options ) do | query |
raise Capybara :: ExpectationNotMet , query . failure_message unless query . resolves_for? ( self )
end
2015-08-24 01:14:48 -04:00
end
##
# Asserts that the page doesn't have the given path.
2017-06-20 18:14:32 -04:00
# 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
2015-08-24 01:14:48 -04:00
#
# @macro current_path_query_params
# @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time
# @return [true]
#
2016-08-17 19:14:39 -04:00
def assert_no_current_path ( path , ** options )
2018-11-12 13:34:15 -05:00
_verify_current_path ( path , options ) do | query |
raise Capybara :: ExpectationNotMet , query . negative_failure_message if query . resolves_for? ( self )
end
2015-08-24 01:14:48 -04:00
end
##
# Checks if the page has the given path.
2017-06-20 18:14:32 -04:00
# 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
2015-08-24 01:14:48 -04:00
#
# @macro current_path_query_params
# @return [Boolean]
#
2016-08-17 19:14:39 -04:00
def has_current_path? ( path , ** options )
2018-07-23 17:57:22 -04:00
make_predicate ( options ) { assert_current_path ( path , options ) }
2015-08-24 01:14:48 -04:00
end
##
# Checks if the page doesn't have the given path.
2017-06-20 18:14:32 -04:00
# 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
2015-08-24 01:14:48 -04:00
#
# @macro current_path_query_params
# @return [Boolean]
#
2016-08-17 19:14:39 -04:00
def has_no_current_path? ( path , ** options )
2018-07-23 17:57:22 -04:00
make_predicate ( options ) { assert_no_current_path ( path , options ) }
2015-08-24 01:14:48 -04:00
end
2016-11-21 19:28:45 -05:00
2018-01-09 17:05:50 -05:00
private
2016-11-21 19:28:45 -05:00
def _verify_current_path ( path , options )
query = Capybara :: Queries :: CurrentPathQuery . new ( path , options )
document . synchronize ( query . wait ) do
yield ( query )
end
2018-05-14 17:30:34 -04:00
true
2016-11-21 19:28:45 -05:00
end
2018-07-23 17:57:22 -04:00
def make_predicate ( options )
options [ :wait ] = 0 unless options . key? ( :wait ) || config . predicates_wait
yield
rescue Capybara :: ExpectationNotMet
false
end
2015-08-24 01:14:48 -04:00
end
2016-03-07 19:52:19 -05:00
end