diff --git a/lib/capybara/searchable.rb b/lib/capybara/searchable.rb index 193b66f5..f5d64dec 100644 --- a/lib/capybara/searchable.rb +++ b/lib/capybara/searchable.rb @@ -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 \ No newline at end of file +end diff --git a/spec/dsl/has_content_spec.rb b/spec/dsl/has_content_spec.rb index 8bb7be93..b0a2db34 100644 --- a/spec/dsl/has_content_spec.rb +++ b/spec/dsl/has_content_spec.rb @@ -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 ullamco 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 \ No newline at end of file diff --git a/spec/dsl/has_css_spec.rb b/spec/dsl/has_css_spec.rb index 2d53ae83..487da468 100644 --- a/spec/dsl/has_css_spec.rb +++ b/spec/dsl/has_css_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/dsl/has_xpath_spec.rb b/spec/dsl/has_xpath_spec.rb index 15a1819a..21c7289d 100644 --- a/spec/dsl/has_xpath_spec.rb +++ b/spec/dsl/has_xpath_spec.rb @@ -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 \ No newline at end of file