1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Added inverse of has_*? methods: has_no_*?

This commit is contained in:
Jonas Nicklas 2009-12-30 17:37:56 +01:00
parent 741c153cb4
commit af81f7444f
4 changed files with 181 additions and 10 deletions

View file

@ -4,9 +4,13 @@ module Capybara
has_xpath?(XPath.content(content).to_s)
end
def has_no_content?(content)
!has_content?(content)
end
def has_xpath?(path, options={})
results = all(path, options)
if options[:count]
results.size == options[:count]
else
@ -14,14 +18,22 @@ module Capybara
end
end
def has_no_xpath?(path, options={})
!has_xpath?(path, options)
end
def has_css?(path, options={})
has_xpath?(XPath.from_css(path), options)
end
def has_no_css?(path, options={})
!has_css?(path, options)
end
def find(locator, options = {})
all(locator, options).first
end
def find_field(locator)
find(XPath.field(locator))
end
@ -34,30 +46,30 @@ module Capybara
def find_button(locator)
find(XPath.button(locator))
end
def find_by_id(id)
def find_by_id(id)
find(Xpath.for_css("##{id}"))
end
def all(locator, options = {})
results = all_unfiltered(locator)
if options[:text]
results = results.select { |n| n.text.match(options[:text]) }
end
if options[:visible] == true
results.reject! { |n| !n.visible? }
end
results
end
private
def all_unfiltered(locator)
raise "Must be overridden"
end
end
end
end

View file

@ -49,5 +49,55 @@ module HasContentSpec
@session.should have_content(%q{"you can't do that."})
end
end
describe '#has_no_content?' do
it "should be false if the given content is on the page at least once" do
@session.visit('/with_html')
@session.should_not have_no_content('est')
@session.should_not have_no_content('Lorem')
@session.should_not have_no_content('Redirect')
end
it "should be false if scoped to an element which has the content" do
@session.visit('/with_html')
@session.within("//a[@title='awesome title']") do
@session.should_not have_no_content('labore')
end
end
it "should be true if scoped to an element which does not have the content" do
@session.visit('/with_html')
@session.within("//a[@title='awesome title']") do
@session.should have_no_content('monkey')
end
end
it "should ignore tags" do
@session.visit('/with_html')
@session.should have_no_content('exercitation <a href="/foo" id="foo">ullamco</a> laboris')
@session.should_not have_no_content('exercitation ullamco laboris')
end
it "should be true if the given content is not on the page" do
@session.visit('/with_html')
@session.should have_no_content('xxxxyzzz')
@session.should have_no_content('monkey')
end
it 'should handle single quotes in the content' do
@session.visit('/with-quotes')
@session.should_not have_no_content("can't")
end
it 'should handle double quotes in the content' do
@session.visit('/with-quotes')
@session.should_not have_no_content(%q{"No," he said})
end
it 'should handle mixed single and double quotes in the content' do
@session.visit('/with-quotes')
@session.should_not have_no_content(%q{"you can't do that."})
end
end
end
end

View file

@ -52,5 +52,58 @@ module HasCssSpec
end
end
end
describe '#has_no_css?' do
before do
@session.visit('/with_html')
end
it "should be false if the given selector is on the page" do
@session.should_not have_no_css("p")
@session.should_not have_no_css("p a#foo")
end
it "should be true if the given selector is not on the page" do
@session.should have_no_css("abbr")
@session.should have_no_css("p a#doesnotexist")
@session.should have_no_css("p.nosuchclass")
end
it "should respect scopes" do
@session.within "//p[@id='first']" do
@session.should_not have_no_css("a#foo")
@session.should have_no_css("a#red")
end
end
context "with count" do
it "should be false if the content is on the page the given number of times" do
@session.should_not have_no_css("p", :count => 3)
@session.should_not have_no_css("p a#foo", :count => 1)
end
it "should be true if the content is on the page the given number of times" do
@session.should have_no_css("p", :count => 6)
@session.should have_no_css("p a#foo", :count => 2)
end
it "should be true if the content isn't on the page at all" do
@session.should have_no_css("abbr", :count => 2)
@session.should have_no_css("p a.doesnotexist", :count => 1)
end
end
context "with text" do
it "should discard all matches where the given string is not contained" do
@session.should_not have_no_css("p a", :text => "Redirect", :count => 1)
@session.should have_no_css("p a", :text => "Doesnotexist")
end
it "should discard all matches where the given regexp is not matched" do
@session.should_not have_no_css("p a", :text => /re[dab]i/i, :count => 1)
@session.should have_no_css("p a", :text => /Red$/)
end
end
end
end
end

View file

@ -55,5 +55,61 @@ module HasXpathSpec
end
end
end
describe '#has_no_xpath?' do
before do
@session.visit('/with_html')
end
it "should be false if the given selector is on the page" do
@session.should_not have_no_xpath("//p")
@session.should_not have_no_xpath("//p//a[@id='foo']")
@session.should_not have_no_xpath("//p[contains(.,'est')]")
end
it "should be true if the given selector is not on the page" do
@session.should have_no_xpath("//abbr")
@session.should have_no_xpath("//p//a[@id='doesnotexist']")
@session.should have_no_xpath("//p[contains(.,'thisstringisnotonpage')]")
end
it "should respect scopes" do
@session.within "//p[@id='first']" do
@session.should_not have_no_xpath("//a[@id='foo']")
@session.should have_no_xpath("//a[@id='red']")
end
end
context "with count" do
it "should be false if the content is on the page the given number of times" do
@session.should_not have_no_xpath("//p", :count => 3)
@session.should_not have_no_xpath("//p//a[@id='foo']", :count => 1)
@session.should_not have_no_xpath("//p[contains(.,'est')]", :count => 1)
end
it "should be true if the content is on the page the wrong number of times" do
@session.should have_no_xpath("//p", :count => 6)
@session.should have_no_xpath("//p//a[@id='foo']", :count => 2)
@session.should have_no_xpath("//p[contains(.,'est')]", :count => 5)
end
it "should be true if the content isn't on the page at all" do
@session.should have_no_xpath("//abbr", :count => 2)
@session.should have_no_xpath("//p//a[@id='doesnotexist']", :count => 1)
end
end
context "with text" do
it "should discard all matches where the given string is contained" do
@session.should_not have_no_xpath("//p//a", :text => "Redirect", :count => 1)
@session.should have_no_xpath("//p", :text => "Doesnotexist")
end
it "should discard all matches where the given regexp is matched" do
@session.should_not have_no_xpath("//p//a", :text => /re[dab]i/i, :count => 1)
@session.should have_no_xpath("//p//a", :text => /Red$/)
end
end
end
end
end