2011-02-04 08:47:15 -05:00
|
|
|
module Capybara
|
|
|
|
module RSpecMatchers
|
2011-02-06 08:36:42 -05:00
|
|
|
class HaveSelector
|
|
|
|
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-06-08 10:47:01 -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-06-08 10:47:01 -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-01-02 12:31:50 -05:00
|
|
|
"has #{query.description}"
|
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 wrap(actual)
|
|
|
|
if actual.respond_to?("has_selector?")
|
|
|
|
actual
|
|
|
|
else
|
|
|
|
Capybara.string(actual.to_s)
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
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
|
|
|
|
2011-02-11 07:59:21 -05:00
|
|
|
class HaveMatcher
|
|
|
|
attr_reader :name, :locator, :options, :failure_message, :actual
|
|
|
|
|
|
|
|
def initialize(name, locator, options={}, &block)
|
|
|
|
@name = name
|
|
|
|
@locator = locator
|
|
|
|
@options = options
|
|
|
|
@failure_message = block
|
|
|
|
end
|
|
|
|
|
2011-03-10 16:39:35 -05:00
|
|
|
def arguments
|
|
|
|
if options.empty? then [locator] else [locator, options] end
|
|
|
|
end
|
|
|
|
|
2011-02-11 07:59:21 -05:00
|
|
|
def matches?(actual)
|
|
|
|
@actual = wrap(actual)
|
2011-03-10 16:39:35 -05:00
|
|
|
@actual.send(:"has_#{name}?", *arguments)
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
|
|
|
@actual = wrap(actual)
|
2011-03-10 16:39:35 -05:00
|
|
|
@actual.send(:"has_no_#{name}?", *arguments)
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message_for_should
|
|
|
|
if failure_message
|
|
|
|
failure_message.call(actual, self)
|
2011-12-01 12:14:51 -05:00
|
|
|
elsif(@options[:count])
|
|
|
|
"expected #{selector_name} to be returned #{@options[:count]} times"
|
2011-02-11 07:59:21 -05:00
|
|
|
else
|
|
|
|
"expected #{selector_name} to return something"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message_for_should_not
|
|
|
|
"expected #{selector_name} not to return anything"
|
|
|
|
end
|
|
|
|
|
2011-04-26 05:56:37 -04:00
|
|
|
def description
|
|
|
|
"has #{selector_name}"
|
|
|
|
end
|
|
|
|
|
2011-02-11 07:59:21 -05:00
|
|
|
def selector_name
|
|
|
|
selector_name = "#{name} #{locator.inspect}"
|
|
|
|
selector_name << " with text #{options[:text].inspect}" if options[:text]
|
|
|
|
selector_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def wrap(actual)
|
|
|
|
if actual.respond_to?("has_selector?")
|
|
|
|
actual
|
|
|
|
else
|
|
|
|
Capybara.string(actual.to_s)
|
|
|
|
end
|
|
|
|
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={})
|
|
|
|
HaveMatcher.new(:xpath, xpath, options)
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
|
|
|
|
2011-02-11 07:59:21 -05:00
|
|
|
def have_css(css, options={})
|
|
|
|
HaveMatcher.new(:css, css, options)
|
2011-02-06 08:36:42 -05:00
|
|
|
end
|
2011-02-10 11:15:28 -05:00
|
|
|
|
|
|
|
def have_content(text)
|
2011-02-11 07:59:21 -05:00
|
|
|
HaveMatcher.new(:content, text.to_s) do |page, matcher|
|
|
|
|
%(expected there to be content #{matcher.locator.inspect} in #{page.text.inspect})
|
|
|
|
end
|
2011-02-10 11:15:28 -05:00
|
|
|
end
|
2012-01-02 12:31:50 -05:00
|
|
|
|
2011-10-15 05:06:44 -04:00
|
|
|
def have_text(text)
|
|
|
|
HaveMatcher.new(:text, text.to_s) do |page, matcher|
|
|
|
|
%(expected there to be text #{matcher.locator.inspect} in #{page.text.inspect})
|
|
|
|
end
|
|
|
|
end
|
2011-02-13 11:04:13 -05:00
|
|
|
|
2011-03-25 06:35:59 -04:00
|
|
|
def have_link(locator, options={})
|
|
|
|
HaveMatcher.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)
|
|
|
|
HaveMatcher.new(:button, locator)
|
|
|
|
end
|
|
|
|
|
|
|
|
def have_field(locator, options={})
|
|
|
|
HaveMatcher.new(:field, locator, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def have_checked_field(locator)
|
|
|
|
HaveMatcher.new(:checked_field, locator)
|
|
|
|
end
|
|
|
|
|
|
|
|
def have_unchecked_field(locator)
|
|
|
|
HaveMatcher.new(:unchecked_field, locator)
|
2011-03-25 05:54:52 -04:00
|
|
|
end
|
2011-03-25 06:46:31 -04:00
|
|
|
|
|
|
|
def have_select(locator, options={})
|
|
|
|
HaveMatcher.new(:select, locator, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def have_table(locator, options={})
|
|
|
|
HaveMatcher.new(:table, locator, options)
|
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
|
|
|
end
|