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