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

154 lines
3.4 KiB
Ruby
Raw Normal View History

module Capybara
module RSpecMatchers
2013-02-16 09:02:32 +00: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
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 query
@query ||= Capybara::Query.new(*@args)
end
end
2013-02-16 09:02:32 +00:00
class HaveText < Matcher
2013-03-03 23:04:23 +00:00
attr_reader :type, :content, :options
2013-03-03 23:04:23 +00:00
def initialize(*args)
@type = args.shift if args.first.is_a?(Symbol)
@content = args.shift
@options = (args.first.is_a?(Hash))? args.first : {}
end
def matches?(actual)
@actual = wrap(actual)
2013-03-03 23:04:23 +00:00
@actual.has_text?(type, content, options)
end
def does_not_match?(actual)
@actual = wrap(actual)
2013-03-03 23:04:23 +00:00
@actual.has_no_text?(type, content, options)
end
def failure_message_for_should
2013-03-17 14:48:04 +00:00
message = Capybara::Helpers.failure_message(description, options)
2013-03-03 23:04:23 +00:00
message << " in #{format(@actual.text(type))}"
message
end
def failure_message_for_should_not
2013-03-03 23:04:23 +00:00
failure_message_for_should.sub(/(to find)/, 'not \1')
end
def description
2013-03-03 23:04:23 +00:00
"text #{format(content)}"
end
2013-03-03 23:04:23 +00:00
def format(content)
content = Capybara::Helpers.normalize_whitespace(content) unless content.is_a? Regexp
content.inspect
end
end
2013-02-16 09:02:32 +00: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
def have_selector(*args)
HaveSelector.new(*args)
end
def have_xpath(xpath, options={})
HaveSelector.new(:xpath, xpath, options)
end
2013-03-03 23:04:23 +00:00
def have_css(css, options={})
HaveSelector.new(:css, css, options)
end
2011-02-10 16:15:28 +00:00
2013-03-03 23:04:23 +00:00
def have_text(*args)
HaveText.new(*args)
end
2013-03-03 23:04:23 +00:00
alias_method :have_content, :have_text
2011-02-13 16:04:13 +00:00
2013-02-16 09:02:32 +00:00
def have_title(title)
HaveTitle.new(title)
end
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, options={})
HaveSelector.new(:button, locator, options)
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