teamcapybara--capybara/lib/capybara/rspec/matchers.rb

120 lines
2.4 KiB
Ruby
Raw Normal View History

module Capybara
module RSpecMatchers
class HaveSelector
def initialize(*args)
@args = args
end
def matches?(actual)
2012-07-09 11:39:57 +00:00
wrap(actual).assert_selector(*@args)
end
def does_not_match?(actual)
2012-07-09 11:39:57 +00:00
wrap(actual).assert_no_selector(*@args)
2011-02-06 19:07:12 +00:00
end
def description
"have #{query.description}"
end
def wrap(actual)
if actual.respond_to?("has_selector?")
actual
else
Capybara.string(actual.to_s)
end
end
def query
@query ||= Capybara::Query.new(*@args)
end
end
class HaveText
attr_reader :text
def initialize(text)
@text = text
end
def matches?(actual)
@actual = wrap(actual)
@actual.has_text?(text)
end
def does_not_match?(actual)
@actual = wrap(actual)
@actual.has_no_text?(text)
end
def failure_message_for_should
"expected there to be text #{text.inspect} in #{@actual.text.inspect}"
end
def failure_message_for_should_not
"expected there not to be text #{text.inspect} in #{@actual.text.inspect}"
end
def description
"have text #{text.inspect}"
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(xpath, options={})
HaveSelector.new(:xpath, xpath, options)
end
def have_css(css, options={})
HaveSelector.new(:css, css, options)
end
2011-02-10 16:15:28 +00:00
def have_content(text)
HaveText.new(text)
2011-02-10 16:15:28 +00:00
end
def have_text(text)
HaveText.new(text)
end
2011-02-13 16:04:13 +00:00
def have_link(locator, options={})
HaveSelector.new(:link, locator, options)
2011-02-13 16:04:13 +00:00
end
2011-03-25 09:54:52 +00:00
def have_button(locator)
HaveSelector.new(:button, locator)
end
def have_field(locator, options={})
HaveSelector.new(:field, locator, options)
end
def have_checked_field(locator)
HaveSelector.new(:field, locator, :checked => true)
end
def have_unchecked_field(locator)
HaveSelector.new(:field, locator, :unchecked => true)
2011-03-25 09:54:52 +00:00
end
def have_select(locator, options={})
HaveSelector.new(:select, locator, options)
end
def have_table(locator, options={})
HaveSelector.new(:table, locator, options)
end
end
end