mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Add have_content matcher
This commit is contained in:
parent
764f022973
commit
d0985f7f78
4 changed files with 79 additions and 1 deletions
|
@ -12,6 +12,14 @@ module Capybara
|
||||||
def inspect
|
def inspect
|
||||||
%(#<Capybara::Document>)
|
%(#<Capybara::Document>)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
#
|
||||||
|
# @return [String] The text of the document
|
||||||
|
#
|
||||||
|
def text
|
||||||
|
find('.').text
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,7 +17,7 @@ module Capybara
|
||||||
|
|
||||||
def failure_message_for_should
|
def failure_message_for_should
|
||||||
if normalized.failure_message
|
if normalized.failure_message
|
||||||
normalized.failure_message.call(@actual)
|
normalized.failure_message.call(@actual, normalized)
|
||||||
else
|
else
|
||||||
"expected #{selector_name} to return something"
|
"expected #{selector_name} to return something"
|
||||||
end
|
end
|
||||||
|
@ -57,5 +57,9 @@ module Capybara
|
||||||
def have_css(*args)
|
def have_css(*args)
|
||||||
HaveSelector.new(:css, *args)
|
HaveSelector.new(:css, *args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def have_content(text)
|
||||||
|
HaveSelector.new(:content, text.to_s)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -87,3 +87,11 @@ Capybara.add_selector(:id) do
|
||||||
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
|
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
|
||||||
match { |value| value.is_a?(Symbol) }
|
match { |value| value.is_a?(Symbol) }
|
||||||
end
|
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
|
||||||
|
|
||||||
|
|
|
@ -207,5 +207,63 @@ describe Capybara::RSpecMatchers do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "have_content matcher" do
|
||||||
|
context "on a string" do
|
||||||
|
context "with should" do
|
||||||
|
it "passes if has_css? returns true" do
|
||||||
|
"<h1>Text</h1>".should have_content('Text')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "fails if has_css? returns false" do
|
||||||
|
expect do
|
||||||
|
"<h1>Text</h1>".should have_content('No such Text')
|
||||||
|
end.to raise_error(/expected there to be content "No such Text" in "Text"/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with should_not" do
|
||||||
|
it "passes if has_no_css? returns true" do
|
||||||
|
"<h1>Text</h1>".should_not have_content('No such Text')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "fails if has_no_css? returns false" do
|
||||||
|
expect do
|
||||||
|
"<h1>Text</h1>".should_not have_content('Text')
|
||||||
|
end.to raise_error(/expected content "Text" not to return anything/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "on a page or node" do
|
||||||
|
before do
|
||||||
|
visit('/with_html')
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with should" do
|
||||||
|
it "passes if has_css? returns true" do
|
||||||
|
page.should have_content('This is a test')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "fails if has_css? returns false" do
|
||||||
|
expect do
|
||||||
|
page.should have_content('No such Text')
|
||||||
|
end.to raise_error(/expected there to be content "No such Text" in "(.*)This is a test(.*)"/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with should_not" do
|
||||||
|
it "passes if has_no_css? returns true" do
|
||||||
|
page.should_not have_content('No such Text')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "fails if has_no_css? returns false" do
|
||||||
|
expect do
|
||||||
|
page.should_not have_content('This is a test')
|
||||||
|
end.to raise_error(/expected content "This is a test" not to return anything/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue