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)
|
|
|
|
@actual = wrap(actual)
|
|
|
|
@actual.has_selector?(*@args)
|
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def does_not_match?(actual)
|
|
|
|
@actual = wrap(actual)
|
|
|
|
@actual.has_no_selector?(*@args)
|
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def failure_message_for_should
|
2011-02-10 10:59:26 -05:00
|
|
|
if normalized.failure_message
|
2011-02-10 11:15:28 -05:00
|
|
|
normalized.failure_message.call(@actual, normalized)
|
2011-02-06 08:36:42 -05:00
|
|
|
else
|
2011-02-06 14:07:12 -05:00
|
|
|
"expected #{selector_name} to return something"
|
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
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def failure_message_for_should_not
|
2011-02-06 14:07:12 -05:00
|
|
|
"expected #{selector_name} not to return anything"
|
|
|
|
end
|
|
|
|
|
|
|
|
def selector_name
|
2011-02-10 10:59:26 -05:00
|
|
|
name = "#{normalized.name} #{normalized.locator.inspect}"
|
|
|
|
name << " with text #{normalized.options[:text].inspect}" if normalized.options[:text]
|
2011-02-06 14:07:12 -05:00
|
|
|
name
|
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
|
|
|
|
2011-02-06 08:36:42 -05:00
|
|
|
def normalized
|
|
|
|
@normalized ||= Capybara::Selector.normalize(*@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
|
|
|
|
|
|
|
|
def matches?(actual)
|
|
|
|
@actual = wrap(actual)
|
2011-03-10 12:58:33 -05:00
|
|
|
# Note: Do not pass options if none given (has_content for instance
|
|
|
|
# expects no options hash).
|
|
|
|
@actual.send(:"has_#{name}?", locator, *([options] unless options.empty?))
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def does_not_match?(actual)
|
|
|
|
@actual = wrap(actual)
|
2011-03-10 12:58:33 -05:00
|
|
|
@actual.send(:"has_no_#{name}?", locator, *([options] unless options.empty?))
|
2011-02-11 07:59:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message_for_should
|
|
|
|
if failure_message
|
|
|
|
failure_message.call(actual, self)
|
|
|
|
else
|
|
|
|
"expected #{selector_name} to return something"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message_for_should_not
|
|
|
|
"expected #{selector_name} not to return anything"
|
|
|
|
end
|
|
|
|
|
|
|
|
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-02-11 07:59:21 -05:00
|
|
|
def have_xpath(path, options={})
|
|
|
|
HaveMatcher.new(:xpath, path, 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
|
2011-02-13 11:04:13 -05:00
|
|
|
|
|
|
|
def have_button(button, options={})
|
|
|
|
HaveMatcher.new(:button, button, options) do |page, matcher|
|
2011-02-13 11:46:22 -05:00
|
|
|
buttons = page.all(:xpath, './/button | .//input[(@type="submit") or (@type="image") or (@type="button")]')
|
|
|
|
labels = buttons.map { |button| %("#{button[:value] or button.text}") }.join(', ')
|
2011-02-13 11:04:13 -05:00
|
|
|
%(expected there to be a button #{matcher.locator.inspect}, other buttons: #{labels})
|
|
|
|
end
|
|
|
|
end
|
2011-02-04 08:47:15 -05:00
|
|
|
end
|
|
|
|
end
|