2016-03-08 00:52:19 +00:00
|
|
|
# frozen_string_literal: true
|
2011-02-04 13:47:15 +00:00
|
|
|
module Capybara
|
|
|
|
module RSpecMatchers
|
2013-02-16 09:02:32 +00:00
|
|
|
class Matcher
|
2014-08-14 18:09:21 +00:00
|
|
|
include ::RSpec::Matchers::Composable if defined?(::RSpec::Expectations::Version) && RSpec::Expectations::Version::STRING.to_f >= 3.0
|
2014-02-16 17:13:58 +00:00
|
|
|
|
2013-02-16 09:02:32 +00:00
|
|
|
def wrap(actual)
|
|
|
|
if actual.respond_to?("has_selector?")
|
|
|
|
actual
|
|
|
|
else
|
|
|
|
Capybara.string(actual.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class HaveSelector < Matcher
|
2014-06-25 19:48:44 +00:00
|
|
|
attr_reader :failure_message, :failure_message_when_negated
|
2014-02-16 17:13:58 +00:00
|
|
|
|
2011-02-06 13:36:42 +00:00
|
|
|
def initialize(*args)
|
|
|
|
@args = args
|
|
|
|
end
|
2011-02-04 13:47:15 +00:00
|
|
|
|
2011-02-06 13:36:42 +00:00
|
|
|
def matches?(actual)
|
2012-07-09 11:39:57 +00:00
|
|
|
wrap(actual).assert_selector(*@args)
|
2014-06-11 18:52:29 +00:00
|
|
|
rescue Capybara::ExpectationNotMet => e
|
2014-02-16 17:13:58 +00:00
|
|
|
@failure_message = e.message
|
2014-06-11 18:52:29 +00:00
|
|
|
return false
|
2011-02-06 13:36:42 +00:00
|
|
|
end
|
2011-02-04 13:47:15 +00:00
|
|
|
|
2011-02-06 13:36:42 +00:00
|
|
|
def does_not_match?(actual)
|
2012-07-09 11:39:57 +00:00
|
|
|
wrap(actual).assert_no_selector(*@args)
|
2014-06-11 18:52:29 +00:00
|
|
|
rescue Capybara::ExpectationNotMet => e
|
|
|
|
@failure_message_when_negated = e.message
|
|
|
|
return false
|
2011-02-06 19:07:12 +00:00
|
|
|
end
|
|
|
|
|
2011-04-26 09:56:37 +00:00
|
|
|
def description
|
2012-07-06 00:09:28 +00:00
|
|
|
"have #{query.description}"
|
2011-02-06 13:36:42 +00:00
|
|
|
end
|
2011-02-04 13:47:15 +00:00
|
|
|
|
2012-01-02 17:31:50 +00:00
|
|
|
def query
|
2012-06-08 14:47:01 +00:00
|
|
|
@query ||= Capybara::Query.new(*@args)
|
2011-02-04 13:47:15 +00:00
|
|
|
end
|
2014-02-16 17:13:58 +00:00
|
|
|
|
2014-06-11 18:52:29 +00:00
|
|
|
# RSpec 2 compatibility:
|
|
|
|
alias_method :failure_message_for_should, :failure_message
|
|
|
|
alias_method :failure_message_for_should_not, :failure_message_when_negated
|
2011-02-04 13:47:15 +00:00
|
|
|
end
|
2011-02-06 13:36:42 +00:00
|
|
|
|
2013-02-16 09:02:32 +00:00
|
|
|
class HaveText < Matcher
|
2013-03-03 23:04:23 +00:00
|
|
|
attr_reader :type, :content, :options
|
2011-02-11 12:59:21 +00:00
|
|
|
|
2014-02-16 17:13:58 +00:00
|
|
|
attr_reader :failure_message, :failure_message_when_negated
|
|
|
|
|
2013-03-03 23:04:23 +00:00
|
|
|
def initialize(*args)
|
2014-02-16 17:13:58 +00:00
|
|
|
@args = args.dup
|
|
|
|
|
|
|
|
# are set just for backwards compatability
|
2013-03-10 22:55:32 +00:00
|
|
|
@type = args.shift if args.first.is_a?(Symbol)
|
|
|
|
@content = args.shift
|
|
|
|
@options = (args.first.is_a?(Hash))? args.first : {}
|
2011-03-10 21:39:35 +00:00
|
|
|
end
|
|
|
|
|
2011-02-11 12:59:21 +00:00
|
|
|
def matches?(actual)
|
2014-02-16 17:13:58 +00:00
|
|
|
wrap(actual).assert_text(*@args)
|
|
|
|
rescue Capybara::ExpectationNotMet => e
|
|
|
|
@failure_message = e.message
|
|
|
|
return false
|
2011-02-11 12:59:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
2014-02-16 17:13:58 +00:00
|
|
|
wrap(actual).assert_no_text(*@args)
|
|
|
|
rescue Capybara::ExpectationNotMet => e
|
|
|
|
@failure_message_when_negated = e.message
|
|
|
|
return false
|
2011-02-11 12:59:21 +00:00
|
|
|
end
|
|
|
|
|
2011-04-26 09:56:37 +00:00
|
|
|
def description
|
2013-03-03 23:04:23 +00:00
|
|
|
"text #{format(content)}"
|
2011-02-11 12:59:21 +00:00
|
|
|
end
|
|
|
|
|
2013-03-03 23:04:23 +00:00
|
|
|
def format(content)
|
|
|
|
content = Capybara::Helpers.normalize_whitespace(content) unless content.is_a? Regexp
|
|
|
|
content.inspect
|
2012-08-01 19:56:11 +00:00
|
|
|
end
|
2014-02-16 17:13:58 +00:00
|
|
|
|
|
|
|
# RSpec 2 compatibility:
|
|
|
|
alias_method :failure_message_for_should, :failure_message
|
|
|
|
alias_method :failure_message_for_should_not, :failure_message_when_negated
|
2011-02-11 12:59:21 +00:00
|
|
|
end
|
|
|
|
|
2013-02-16 09:02:32 +00:00
|
|
|
class HaveTitle < Matcher
|
|
|
|
attr_reader :title
|
|
|
|
|
2014-02-16 17:13:58 +00:00
|
|
|
attr_reader :failure_message, :failure_message_when_negated
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
@args = args
|
|
|
|
|
|
|
|
# are set just for backwards compatability
|
|
|
|
@title = args.first
|
2013-02-16 09:02:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(actual)
|
2014-02-16 17:13:58 +00:00
|
|
|
wrap(actual).assert_title(*@args)
|
|
|
|
rescue Capybara::ExpectationNotMet => e
|
|
|
|
@failure_message = e.message
|
|
|
|
return false
|
2013-02-16 09:02:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
2014-02-16 17:13:58 +00:00
|
|
|
wrap(actual).assert_no_title(*@args)
|
|
|
|
rescue Capybara::ExpectationNotMet => e
|
|
|
|
@failure_message_when_negated = e.message
|
|
|
|
return false
|
2013-02-16 09:02:32 +00:00
|
|
|
end
|
|
|
|
|
2014-02-16 17:13:58 +00:00
|
|
|
def description
|
|
|
|
"have title #{title.inspect}"
|
2013-02-16 09:02:32 +00:00
|
|
|
end
|
|
|
|
|
2013-12-31 10:55:35 +00:00
|
|
|
# RSpec 2 compatibility:
|
|
|
|
alias_method :failure_message_for_should, :failure_message
|
|
|
|
alias_method :failure_message_for_should_not, :failure_message_when_negated
|
2013-02-16 09:02:32 +00:00
|
|
|
end
|
|
|
|
|
2015-08-24 05:14:48 +00:00
|
|
|
class HaveCurrentPath < Matcher
|
|
|
|
attr_reader :current_path
|
|
|
|
|
|
|
|
attr_reader :failure_message, :failure_message_when_negated
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
@args = args
|
|
|
|
|
|
|
|
# are set just for backwards compatability
|
|
|
|
@current_path = args.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(actual)
|
|
|
|
wrap(actual).assert_current_path(*@args)
|
|
|
|
rescue Capybara::ExpectationNotMet => e
|
|
|
|
@failure_message = e.message
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
|
|
|
wrap(actual).assert_no_current_path(*@args)
|
|
|
|
rescue Capybara::ExpectationNotMet => e
|
|
|
|
@failure_message_when_negated = e.message
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
"have current path #{current_path.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# RSpec 2 compatibility:
|
|
|
|
alias_method :failure_message_for_should, :failure_message
|
|
|
|
alias_method :failure_message_for_should_not, :failure_message_when_negated
|
|
|
|
end
|
|
|
|
|
2014-04-08 21:28:16 +00:00
|
|
|
class BecomeClosed
|
|
|
|
def initialize(options)
|
|
|
|
@wait_time = Capybara::Query.new(options).wait
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(window)
|
|
|
|
@window = window
|
2015-04-14 21:41:01 +00:00
|
|
|
start_time = Capybara::Helpers.monotonic_time
|
2014-06-26 18:55:45 +00:00
|
|
|
while window.exists?
|
2015-04-14 21:41:01 +00:00
|
|
|
return false if (Capybara::Helpers.monotonic_time - start_time) > @wait_time
|
2014-04-08 21:28:16 +00:00
|
|
|
sleep 0.05
|
|
|
|
end
|
2014-06-26 18:55:45 +00:00
|
|
|
true
|
2014-04-08 21:28:16 +00: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
|
|
|
|
|
|
|
|
# RSpec 2 compatibility:
|
|
|
|
alias_method :failure_message_for_should, :failure_message
|
|
|
|
alias_method :failure_message_for_should_not, :failure_message_when_negated
|
|
|
|
end
|
|
|
|
|
2011-02-06 13:36:42 +00:00
|
|
|
def have_selector(*args)
|
|
|
|
HaveSelector.new(*args)
|
|
|
|
end
|
|
|
|
|
2011-03-25 10:35:59 +00:00
|
|
|
def have_xpath(xpath, options={})
|
2012-06-08 15:07:12 +00:00
|
|
|
HaveSelector.new(:xpath, xpath, options)
|
2011-02-06 13:36:42 +00:00
|
|
|
end
|
|
|
|
|
2013-03-03 23:04:23 +00:00
|
|
|
def have_css(css, options={})
|
2012-06-08 15:07:12 +00:00
|
|
|
HaveSelector.new(:css, css, options)
|
2011-02-06 13:36:42 +00:00
|
|
|
end
|
2011-02-10 16:15:28 +00:00
|
|
|
|
2013-03-03 23:04:23 +00:00
|
|
|
def have_text(*args)
|
|
|
|
HaveText.new(*args)
|
2011-10-15 09:06:44 +00:00
|
|
|
end
|
2013-03-03 23:04:23 +00:00
|
|
|
alias_method :have_content, :have_text
|
2011-02-13 16:04:13 +00:00
|
|
|
|
2014-02-16 17:13:58 +00:00
|
|
|
def have_title(title, options = {})
|
|
|
|
HaveTitle.new(title, options)
|
2013-02-16 09:02:32 +00:00
|
|
|
end
|
|
|
|
|
2015-08-24 05:14:48 +00:00
|
|
|
def have_current_path(path, options = {})
|
|
|
|
HaveCurrentPath.new(path, options)
|
|
|
|
end
|
|
|
|
|
2011-03-25 10:35:59 +00:00
|
|
|
def have_link(locator, options={})
|
2012-06-08 15:07:12 +00:00
|
|
|
HaveSelector.new(:link, locator, options)
|
2011-02-13 16:04:13 +00:00
|
|
|
end
|
2011-03-25 09:54:52 +00:00
|
|
|
|
2013-08-08 23:05:38 +00:00
|
|
|
def have_button(locator, options={})
|
|
|
|
HaveSelector.new(:button, locator, options)
|
2011-03-25 10:35:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def have_field(locator, options={})
|
2012-06-08 15:07:12 +00:00
|
|
|
HaveSelector.new(:field, locator, options)
|
2011-03-25 10:35:59 +00:00
|
|
|
end
|
|
|
|
|
2013-08-09 01:36:33 +00:00
|
|
|
def have_checked_field(locator, options={})
|
|
|
|
HaveSelector.new(:field, locator, options.merge(:checked => true))
|
2011-03-25 10:35:59 +00:00
|
|
|
end
|
|
|
|
|
2013-08-09 01:36:33 +00:00
|
|
|
def have_unchecked_field(locator, options={})
|
|
|
|
HaveSelector.new(:field, locator, options.merge(:unchecked => true))
|
2011-03-25 09:54:52 +00:00
|
|
|
end
|
2011-03-25 10:46:31 +00:00
|
|
|
|
|
|
|
def have_select(locator, options={})
|
2012-06-08 15:07:12 +00:00
|
|
|
HaveSelector.new(:select, locator, options)
|
2011-03-25 10:46:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def have_table(locator, options={})
|
2012-06-08 15:07:12 +00:00
|
|
|
HaveSelector.new(:table, locator, options)
|
2011-03-25 10:46:31 +00:00
|
|
|
end
|
2014-04-08 21:28:16 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Wait for window to become closed.
|
|
|
|
# @example
|
|
|
|
# expect(window).to become_closed(wait: 0.8)
|
|
|
|
# @param options [Hash] optional param
|
2015-04-14 20:56:30 +00:00
|
|
|
# @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum wait time
|
2014-04-08 21:28:16 +00:00
|
|
|
def become_closed(options = {})
|
|
|
|
BecomeClosed.new(options)
|
|
|
|
end
|
2011-02-04 13:47:15 +00:00
|
|
|
end
|
|
|
|
end
|