2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2011-02-04 08:47:15 -05:00
|
|
|
module Capybara
|
|
|
|
module RSpecMatchers
|
2013-02-16 04:02:32 -05:00
|
|
|
class Matcher
|
2016-01-29 19:31:35 -05:00
|
|
|
include ::RSpec::Matchers::Composable if defined?(::RSpec::Expectations::Version) && (Gem::Version.new(RSpec::Expectations::Version::STRING) >= Gem::Version.new('3.0'))
|
2014-02-16 12:13:58 -05:00
|
|
|
|
2016-11-21 16:58:12 -05:00
|
|
|
attr_reader :failure_message, :failure_message_when_negated
|
|
|
|
|
2013-02-16 04:02:32 -05:00
|
|
|
def wrap(actual)
|
2016-12-15 12:04:01 -05:00
|
|
|
@context_el = if actual.respond_to?("has_selector?")
|
2013-02-16 04:02:32 -05:00
|
|
|
actual
|
|
|
|
else
|
|
|
|
Capybara.string(actual.to_s)
|
|
|
|
end
|
|
|
|
end
|
2016-11-18 13:56:47 -05:00
|
|
|
|
2016-11-21 16:58:12 -05:00
|
|
|
# RSpec 2 compatibility:
|
|
|
|
def failure_message_for_should; failure_message end
|
|
|
|
def failure_message_for_should_not; failure_message_when_negated end
|
|
|
|
|
2016-11-18 18:22:04 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def wrap_matches?(actual)
|
2016-11-18 13:56:47 -05:00
|
|
|
yield(wrap(actual))
|
|
|
|
rescue Capybara::ExpectationNotMet => e
|
|
|
|
@failure_message = e.message
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2016-11-18 18:22:04 -05:00
|
|
|
def wrap_does_not_match?(actual)
|
2016-11-18 13:56:47 -05:00
|
|
|
yield(wrap(actual))
|
|
|
|
rescue Capybara::ExpectationNotMet => e
|
|
|
|
@failure_message_when_negated = e.message
|
|
|
|
return false
|
|
|
|
end
|
2016-12-15 12:04:01 -05:00
|
|
|
|
|
|
|
def session_query_args
|
|
|
|
if @args.last.is_a? Hash
|
|
|
|
@args.last[:session_options] = session_options
|
|
|
|
else
|
|
|
|
@args.push(session_options: session_options)
|
|
|
|
end
|
|
|
|
@args
|
|
|
|
end
|
|
|
|
|
|
|
|
def session_options
|
|
|
|
@context_el ? @context_el.session_options : Capybara.session_options
|
|
|
|
end
|
2013-02-16 04:02:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class HaveSelector < Matcher
|
2014-02-16 12:13:58 -05:00
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def initialize(*args, &filter_block)
|
2011-02-06 08:36:42 -05:00
|
|
|
@args = args
|
2016-09-23 18:03:36 -04:00
|
|
|
@filter_block = filter_block
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def matches?(actual)
|
2016-11-18 18:22:04 -05:00
|
|
|
wrap_matches?(actual){ |el| el.assert_selector(*@args, &@filter_block) }
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def does_not_match?(actual)
|
2016-11-18 18:22:04 -05:00
|
|
|
wrap_does_not_match?(actual){ |el| el.assert_no_selector(*@args, &@filter_block) }
|
2011-02-06 14:07:12 -05:00
|
|
|
end
|
|
|
|
|
2011-04-26 05:56:37 -04:00
|
|
|
def description
|
2012-07-05 20:09:28 -04:00
|
|
|
"have #{query.description}"
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2012-01-02 12:31:50 -05:00
|
|
|
def query
|
2016-12-15 12:04:01 -05:00
|
|
|
@query ||= Capybara::Queries::SelectorQuery.new(*session_query_args, &@filter_block)
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
|
|
|
end
|
2011-02-06 08:36:42 -05:00
|
|
|
|
2016-11-18 18:22:04 -05:00
|
|
|
class MatchSelector < HaveSelector
|
|
|
|
def matches?(actual)
|
|
|
|
wrap_matches?(actual) { |el| el.assert_matches_selector(*@args, &@filter_block) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
|
|
|
wrap_does_not_match?(actual) { |el| el.assert_not_matches_selector(*@args, &@filter_block) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
"match #{query.description}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def query
|
2016-12-15 12:04:01 -05:00
|
|
|
@query ||= Capybara::Queries::MatchQuery.new(*session_query_args, &@filter_block)
|
2016-11-18 18:22:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-16 04:02:32 -05:00
|
|
|
class HaveText < Matcher
|
2013-03-03 18:04:23 -05:00
|
|
|
attr_reader :type, :content, :options
|
2011-02-11 07:59:21 -05:00
|
|
|
|
2013-03-03 18:04:23 -05:00
|
|
|
def initialize(*args)
|
2014-02-16 12:13:58 -05:00
|
|
|
@args = args.dup
|
|
|
|
|
|
|
|
# are set just for backwards compatability
|
2013-03-10 18:55:32 -04:00
|
|
|
@type = args.shift if args.first.is_a?(Symbol)
|
|
|
|
@content = args.shift
|
|
|
|
@options = (args.first.is_a?(Hash))? args.first : {}
|
2011-03-10 16:39:35 -05:00
|
|
|
end
|
|
|
|
|
2011-02-11 07:59:21 -05:00
|
|
|
def matches?(actual)
|
2016-11-18 18:22:04 -05:00
|
|
|
wrap_matches?(actual) { |el| el.assert_text(*@args) }
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
2016-11-18 18:22:04 -05:00
|
|
|
wrap_does_not_match?(actual) { |el| el.assert_no_text(*@args) }
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
2011-04-26 05:56:37 -04:00
|
|
|
def description
|
2013-03-03 18:04:23 -05:00
|
|
|
"text #{format(content)}"
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
2013-03-03 18:04:23 -05:00
|
|
|
def format(content)
|
|
|
|
content = Capybara::Helpers.normalize_whitespace(content) unless content.is_a? Regexp
|
|
|
|
content.inspect
|
2012-08-01 15:56:11 -04:00
|
|
|
end
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
2013-02-16 04:02:32 -05:00
|
|
|
class HaveTitle < Matcher
|
|
|
|
attr_reader :title
|
|
|
|
|
2014-02-16 12:13:58 -05:00
|
|
|
def initialize(*args)
|
|
|
|
@args = args
|
|
|
|
|
|
|
|
# are set just for backwards compatability
|
|
|
|
@title = args.first
|
2013-02-16 04:02:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(actual)
|
2016-11-18 18:22:04 -05:00
|
|
|
wrap_matches?(actual) { |el| el.assert_title(*@args) }
|
2013-02-16 04:02:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
2016-11-18 18:22:04 -05:00
|
|
|
wrap_does_not_match?(actual) { |el| el.assert_no_title(*@args) }
|
2013-02-16 04:02:32 -05:00
|
|
|
end
|
|
|
|
|
2014-02-16 12:13:58 -05:00
|
|
|
def description
|
|
|
|
"have title #{title.inspect}"
|
2013-02-16 04:02:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-24 01:14:48 -04:00
|
|
|
class HaveCurrentPath < Matcher
|
|
|
|
attr_reader :current_path
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
@args = args
|
|
|
|
|
|
|
|
# are set just for backwards compatability
|
|
|
|
@current_path = args.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(actual)
|
2016-11-18 18:22:04 -05:00
|
|
|
wrap_matches?(actual) { |el| el.assert_current_path(*@args) }
|
2015-08-24 01:14:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
2016-11-18 18:22:04 -05:00
|
|
|
wrap_does_not_match?(actual) { |el| el.assert_no_current_path(*@args) }
|
2015-08-24 01:14:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
"have current path #{current_path.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-08 17:28:16 -04:00
|
|
|
class BecomeClosed
|
|
|
|
def initialize(options)
|
2016-12-15 12:04:01 -05:00
|
|
|
@options = options
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(window)
|
|
|
|
@window = window
|
2016-12-15 12:04:01 -05:00
|
|
|
@wait_time = Capybara::Queries::BaseQuery.wait(@options, window.session.config.default_max_wait_time)
|
2015-04-14 17:41:01 -04:00
|
|
|
start_time = Capybara::Helpers.monotonic_time
|
2014-06-26 14:55:45 -04:00
|
|
|
while window.exists?
|
2015-04-14 17:41:01 -04:00
|
|
|
return false if (Capybara::Helpers.monotonic_time - start_time) > @wait_time
|
2014-04-08 17:28:16 -04:00
|
|
|
sleep 0.05
|
|
|
|
end
|
2014-06-26 14:55:45 -04:00
|
|
|
true
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message
|
|
|
|
"expected #{@window.inspect} to become closed after #{@wait_time} seconds"
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message_when_negated
|
|
|
|
"expected #{@window.inspect} not to become closed after #{@wait_time} seconds"
|
|
|
|
end
|
2016-11-21 18:11:49 -05:00
|
|
|
|
|
|
|
# RSpec 2 compatibility:
|
|
|
|
alias_method :failure_message_for_should, :failure_message
|
|
|
|
alias_method :failure_message_for_should_not, :failure_message_when_negated
|
2014-04-08 17:28:16 -04:00
|
|
|
end
|
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def have_selector(*args, &optional_filter_block)
|
|
|
|
HaveSelector.new(*args, &optional_filter_block)
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
|
|
|
|
2016-11-18 18:22:04 -05:00
|
|
|
def match_selector(*args, &optional_filter_block)
|
|
|
|
MatchSelector.new(*args, &optional_filter_block)
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
# defined_negated_matcher was added in RSpec 3.1 - it's syntactic sugar only since a user can do
|
|
|
|
# expect(page).not_to match_selector, so not sure we really need to support not_match_selector for prior to RSpec 3.1
|
|
|
|
::RSpec::Matchers.define_negated_matcher :not_match_selector, :match_selector if defined?(::RSpec::Expectations::Version) && (Gem::Version.new(RSpec::Expectations::Version::STRING) >= Gem::Version.new('3.1'))
|
|
|
|
|
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def have_xpath(xpath, options={}, &optional_filter_block)
|
|
|
|
HaveSelector.new(:xpath, xpath, options, &optional_filter_block)
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
|
|
|
|
2016-11-18 18:22:04 -05:00
|
|
|
def match_xpath(xpath, options={}, &optional_filter_block)
|
|
|
|
MatchSelector.new(:xpath, xpath, options, &optional_filter_block)
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def have_css(css, options={}, &optional_filter_block)
|
|
|
|
HaveSelector.new(:css, css, options, &optional_filter_block)
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
2011-02-10 11:15:28 -05:00
|
|
|
|
2016-11-18 18:22:04 -05:00
|
|
|
def match_css(css, options={}, &optional_filter_block)
|
|
|
|
MatchSelector.new(:css, css, options, &optional_filter_block)
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
|
2013-03-03 18:04:23 -05:00
|
|
|
def have_text(*args)
|
|
|
|
HaveText.new(*args)
|
2011-10-15 05:06:44 -04:00
|
|
|
end
|
2013-03-03 18:04:23 -05:00
|
|
|
alias_method :have_content, :have_text
|
2011-02-13 11:04:13 -05:00
|
|
|
|
2014-02-16 12:13:58 -05:00
|
|
|
def have_title(title, options = {})
|
|
|
|
HaveTitle.new(title, options)
|
2013-02-16 04:02:32 -05:00
|
|
|
end
|
|
|
|
|
2015-08-24 01:14:48 -04:00
|
|
|
def have_current_path(path, options = {})
|
|
|
|
HaveCurrentPath.new(path, options)
|
|
|
|
end
|
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def have_link(locator=nil, options={}, &optional_filter_block)
|
2016-09-28 16:54:20 -04:00
|
|
|
locator, options = nil, locator if locator.is_a? Hash
|
2016-09-23 18:03:36 -04:00
|
|
|
HaveSelector.new(:link, locator, options, &optional_filter_block)
|
2011-02-13 11:04:13 -05:00
|
|
|
end
|
2011-03-25 05:54:52 -04:00
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def have_button(locator=nil, options={}, &optional_filter_block)
|
2016-09-28 16:54:20 -04:00
|
|
|
locator, options = nil, locator if locator.is_a? Hash
|
2016-09-23 18:03:36 -04:00
|
|
|
HaveSelector.new(:button, locator, options, &optional_filter_block)
|
2011-03-25 06:35:59 -04:00
|
|
|
end
|
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def have_field(locator=nil, options={}, &optional_filter_block)
|
2016-09-28 16:54:20 -04:00
|
|
|
locator, options = nil, locator if locator.is_a? Hash
|
2016-09-23 18:03:36 -04:00
|
|
|
HaveSelector.new(:field, locator, options, &optional_filter_block)
|
2011-03-25 06:35:59 -04:00
|
|
|
end
|
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def have_checked_field(locator=nil, options={}, &optional_filter_block)
|
2016-09-28 16:54:20 -04:00
|
|
|
locator, options = nil, locator if locator.is_a? Hash
|
2016-10-04 14:10:29 -04:00
|
|
|
HaveSelector.new(:field, locator, options.merge(checked: true), &optional_filter_block)
|
2011-03-25 06:35:59 -04:00
|
|
|
end
|
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def have_unchecked_field(locator=nil, options={}, &optional_filter_block)
|
2016-09-28 16:54:20 -04:00
|
|
|
locator, options = nil, locator if locator.is_a? Hash
|
2016-10-04 14:10:29 -04:00
|
|
|
HaveSelector.new(:field, locator, options.merge(unchecked: true), &optional_filter_block)
|
2011-03-25 05:54:52 -04:00
|
|
|
end
|
2011-03-25 06:46:31 -04:00
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def have_select(locator=nil, options={}, &optional_filter_block)
|
2016-09-28 16:54:20 -04:00
|
|
|
locator, options = nil, locator if locator.is_a? Hash
|
2016-09-23 18:03:36 -04:00
|
|
|
HaveSelector.new(:select, locator, options, &optional_filter_block)
|
2011-03-25 06:46:31 -04:00
|
|
|
end
|
|
|
|
|
2016-09-23 18:03:36 -04:00
|
|
|
def have_table(locator=nil, options={}, &optional_filter_block)
|
2016-09-28 16:54:20 -04:00
|
|
|
locator, options = nil, locator if locator.is_a? Hash
|
2016-09-23 18:03:36 -04:00
|
|
|
HaveSelector.new(:table, locator, options, &optional_filter_block)
|
2011-03-25 06:46:31 -04:00
|
|
|
end
|
2014-04-08 17:28:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Wait for window to become closed.
|
|
|
|
# @example
|
|
|
|
# expect(window).to become_closed(wait: 0.8)
|
|
|
|
# @param options [Hash] optional param
|
2015-04-14 16:56:30 -04:00
|
|
|
# @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum wait time
|
2014-04-08 17:28:16 -04:00
|
|
|
def become_closed(options = {})
|
|
|
|
BecomeClosed.new(options)
|
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
2017-03-28 13:41:49 -04:00
|
|
|
end
|