diff --git a/lib/capybara/node/matchers.rb b/lib/capybara/node/matchers.rb index a440be1e..7a401330 100644 --- a/lib/capybara/node/matchers.rb +++ b/lib/capybara/node/matchers.rb @@ -196,12 +196,13 @@ module Capybara # This only matches displayable text and specifically excludes text # contained within non-display nodes such as script or head tags. # + # @param [:all, :visible] type Whether to only check for visible or all text # @param [String] content The text to check for # @return [Boolean] Whether it exists # - def has_text?(content) + def has_text?(type=nil, content) synchronize do - unless Capybara::Helpers.normalize_whitespace(text).match(Capybara::Helpers.to_regexp(content)) + unless Capybara::Helpers.normalize_whitespace(text(type)).match(Capybara::Helpers.to_regexp(content)) raise ExpectationNotMet end end @@ -222,9 +223,9 @@ module Capybara # @param [String] content The text to check for # @return [Boolean] Whether it doesn't exist # - def has_no_text?(content) + def has_no_text?(type=nil, content) synchronize do - if Capybara::Helpers.normalize_whitespace(text).match(Capybara::Helpers.to_regexp(content)) + if Capybara::Helpers.normalize_whitespace(text(type)).match(Capybara::Helpers.to_regexp(content)) raise ExpectationNotMet end end diff --git a/lib/capybara/node/simple.rb b/lib/capybara/node/simple.rb index bb5abab6..da4c67f5 100644 --- a/lib/capybara/node/simple.rb +++ b/lib/capybara/node/simple.rb @@ -26,7 +26,7 @@ module Capybara # # @return [String] The text of the element # - def text + def text(type=nil) native.text end diff --git a/lib/capybara/rspec/matchers.rb b/lib/capybara/rspec/matchers.rb index 4ae8721b..1f17b1f5 100644 --- a/lib/capybara/rspec/matchers.rb +++ b/lib/capybara/rspec/matchers.rb @@ -33,28 +33,29 @@ module Capybara end class HaveText < Matcher - attr_reader :text + attr_reader :text, :type - def initialize(text) + def initialize(type, text) + @type = type @text = text end def matches?(actual) @actual = wrap(actual) - @actual.has_text?(text) + @actual.has_text?(type, text) end def does_not_match?(actual) @actual = wrap(actual) - @actual.has_no_text?(text) + @actual.has_no_text?(type, text) end def failure_message_for_should - "expected there to be text #{format(text)} in #{format(@actual.text)}" + "expected there to be text #{format(text)} in #{format(@actual.text(type))}" end def failure_message_for_should_not - "expected there not to be text #{format(text)} in #{format(@actual.text)}" + "expected there not to be text #{format(text)} in #{format(@actual.text(type))}" end def description @@ -109,12 +110,12 @@ module Capybara HaveSelector.new(:css, css, options) end - def have_content(text) - HaveText.new(text) + def have_content(type=nil, text) + HaveText.new(type, text) end - def have_text(text) - HaveText.new(text) + def have_text(type=nil, text) + HaveText.new(type, text) end def have_title(title) diff --git a/lib/capybara/spec/session/has_text_spec.rb b/lib/capybara/spec/session/has_text_spec.rb index e4728231..371af37f 100644 --- a/lib/capybara/spec/session/has_text_spec.rb +++ b/lib/capybara/spec/session/has_text_spec.rb @@ -74,6 +74,17 @@ Capybara::SpecHelper.spec '#has_text?' do @session.should_not have_text('Inside element with hidden ancestor') end + it "should be true if :all given and text is invisible." do + @session.visit('/with_html') + @session.should have_text(:all, 'Some of this text is hidden!') + end + + it "should be true if `Capybara.ignore_hidden_elements = true` and text is invisible." do + Capybara.ignore_hidden_elements = false + @session.visit('/with_html') + @session.should have_text('Some of this text is hidden!') + end + it "should be true if the text in the page matches given regexp" do @session.visit('/with_html') @session.should have_text(/Lorem/) @@ -172,6 +183,17 @@ Capybara::SpecHelper.spec '#has_no_text?' do @session.should have_no_text('Inside element with hidden ancestor') end + it "should be false if :all given and text is invisible." do + @session.visit('/with_html') + @session.should_not have_no_text(:all, 'Some of this text is hidden!') + end + + it "should be false if `Capybara.ignore_hidden_elements = true` and text is invisible." do + Capybara.ignore_hidden_elements = false + @session.visit('/with_html') + @session.should_not have_no_text('Some of this text is hidden!') + end + it "should be true if the text in the page doesn't match given regexp" do @session.visit('/with_html') @session.should have_no_text(/xxxxyzzz/) diff --git a/spec/rspec/matchers_spec.rb b/spec/rspec/matchers_spec.rb index ae091733..7719293a 100644 --- a/spec/rspec/matchers_spec.rb +++ b/spec/rspec/matchers_spec.rb @@ -363,6 +363,15 @@ describe Capybara::RSpecMatchers do page.should have_text(/test/) end + it "can check for all text" do + page.should have_text(:all, 'Some of this text is hidden!') + end + + it "can check for visible text" do + page.should have_text(:visible, 'Some of this text is') + page.should_not have_text(:visible, 'Some of this text is hidden!') + end + it "fails if has_text? returns false" do expect do page.should have_text('No such Text')