2011-02-04 08:47:15 -05:00
|
|
|
module Capybara
|
|
|
|
module RSpecMatchers
|
2013-02-16 04:02:32 -05:00
|
|
|
class Matcher
|
|
|
|
def wrap(actual)
|
|
|
|
if actual.respond_to?("has_selector?")
|
|
|
|
actual
|
|
|
|
else
|
|
|
|
Capybara.string(actual.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class HaveSelector < Matcher
|
2011-02-06 08:36:42 -05:00
|
|
|
def initialize(*args)
|
|
|
|
@args = args
|
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def matches?(actual)
|
2012-07-09 07:39:57 -04:00
|
|
|
wrap(actual).assert_selector(*@args)
|
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)
|
2012-07-09 07:39:57 -04:00
|
|
|
wrap(actual).assert_no_selector(*@args)
|
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
|
2012-06-08 10:47:01 -04:00
|
|
|
@query ||= Capybara::Query.new(*@args)
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
|
|
|
end
|
2011-02-06 08:36:42 -05:00
|
|
|
|
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)
|
|
|
|
@options = (args.last.is_a?(Hash))? args.pop : {}
|
|
|
|
@content = args.pop
|
|
|
|
@type = args.first
|
2011-03-10 16:39:35 -05:00
|
|
|
end
|
|
|
|
|
2011-02-11 07:59:21 -05:00
|
|
|
def matches?(actual)
|
|
|
|
@actual = wrap(actual)
|
2013-03-03 18:04:23 -05:00
|
|
|
@actual.has_text?(type, content, options)
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
|
|
|
@actual = wrap(actual)
|
2013-03-03 18:04:23 -05:00
|
|
|
@actual.has_no_text?(type, content, options)
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message_for_should
|
2013-03-03 18:04:23 -05:00
|
|
|
message = Capybara::CountHelpers.failure_message(description, options)
|
|
|
|
message << " in #{format(@actual.text(type))}"
|
|
|
|
message
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message_for_should_not
|
2013-03-03 18:04:23 -05:00
|
|
|
failure_message_for_should.sub(/(to find)/, 'not \1')
|
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
|
|
|
|
|
|
|
|
def initialize(title)
|
|
|
|
@title = title
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(actual)
|
|
|
|
@actual = wrap(actual)
|
|
|
|
@actual.has_title?(title)
|
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
|
|
|
@actual = wrap(actual)
|
|
|
|
@actual.has_no_title?(title)
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message_for_should
|
|
|
|
"expected there to be title #{title.inspect} in #{@actual.title.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message_for_should_not
|
|
|
|
"expected there not to be title #{title.inspect} in #{@actual.title.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
"have title #{title.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def have_selector(*args)
|
|
|
|
HaveSelector.new(*args)
|
|
|
|
end
|
|
|
|
|
2011-03-25 06:35:59 -04:00
|
|
|
def have_xpath(xpath, options={})
|
2012-06-08 11:07:12 -04:00
|
|
|
HaveSelector.new(:xpath, xpath, options)
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
|
|
|
|
2013-03-03 18:04:23 -05:00
|
|
|
def have_css(css, options={})
|
2012-06-08 11:07:12 -04:00
|
|
|
HaveSelector.new(:css, css, options)
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
2011-02-10 11:15:28 -05:00
|
|
|
|
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
|
|
|
|
2013-02-16 04:02:32 -05:00
|
|
|
def have_title(title)
|
|
|
|
HaveTitle.new(title)
|
|
|
|
end
|
|
|
|
|
2011-03-25 06:35:59 -04:00
|
|
|
def have_link(locator, options={})
|
2012-06-08 11:07:12 -04:00
|
|
|
HaveSelector.new(:link, locator, options)
|
2011-02-13 11:04:13 -05:00
|
|
|
end
|
2011-03-25 05:54:52 -04:00
|
|
|
|
2011-03-25 06:35:59 -04:00
|
|
|
def have_button(locator)
|
2012-06-08 11:07:12 -04:00
|
|
|
HaveSelector.new(:button, locator)
|
2011-03-25 06:35:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def have_field(locator, options={})
|
2012-06-08 11:07:12 -04:00
|
|
|
HaveSelector.new(:field, locator, options)
|
2011-03-25 06:35:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def have_checked_field(locator)
|
2012-06-08 11:07:12 -04:00
|
|
|
HaveSelector.new(:field, locator, :checked => true)
|
2011-03-25 06:35:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def have_unchecked_field(locator)
|
2012-06-08 11:07:12 -04:00
|
|
|
HaveSelector.new(:field, locator, :unchecked => true)
|
2011-03-25 05:54:52 -04:00
|
|
|
end
|
2011-03-25 06:46:31 -04:00
|
|
|
|
|
|
|
def have_select(locator, options={})
|
2012-06-08 11:07:12 -04:00
|
|
|
HaveSelector.new(:select, locator, options)
|
2011-03-25 06:46:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def have_table(locator, options={})
|
2012-06-08 11:07:12 -04:00
|
|
|
HaveSelector.new(:table, locator, options)
|
2011-03-25 06:46:31 -04:00
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
|
|
|
end
|