Not everything can or should be a selector ;)

This commit is contained in:
Jonas Nicklas and Nicklas Ramhöj 2011-02-11 13:59:21 +01:00
parent 36df16e5e4
commit 70c4f196c2
3 changed files with 62 additions and 13 deletions

View File

@ -46,20 +46,69 @@ module Capybara
end
end
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)
@actual.send(:"has_#{name}?", locator, *options)
end
def does_not_match?(actual)
@actual = wrap(actual)
@actual.send(:"has_no_#{name}?", locator, *options)
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
def have_selector(*args)
HaveSelector.new(*args)
end
def have_xpath(*args)
HaveSelector.new(:xpath, *args)
def have_xpath(path, options={})
HaveMatcher.new(:xpath, path, options)
end
def have_css(*args)
HaveSelector.new(:css, *args)
def have_css(css, options={})
HaveMatcher.new(:css, css, options)
end
def have_content(text)
HaveSelector.new(:content, text.to_s)
HaveMatcher.new(:content, text.to_s) do |page, matcher|
%(expected there to be content #{matcher.locator.inspect} in #{page.text.inspect})
end
end
end
end

View File

@ -87,11 +87,3 @@ Capybara.add_selector(:id) do
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
match { |value| value.is_a?(Symbol) }
end
Capybara.add_selector(:content) do
xpath { |text| XPath::HTML.content(text) }
failure_message do |page, selector|
%(expected there to be content #{selector.locator.inspect} in #{page.text.inspect})
end
end

View File

@ -265,5 +265,13 @@ describe Capybara::RSpecMatchers do
end
end
end
describe "have_link matcher"
describe "have_button matcher"
describe "have_no_button matcher"
describe "have_field matcher"
describe "have_checked_field matcher"
describe "have_unchecked_field matcher"
describe "have_select matcher"
describe "have_table matcher"
end