Add type to matchers
This commit is contained in:
parent
2697a5bd8c
commit
10499c05b7
|
@ -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
|
||||
|
|
|
@ -26,7 +26,7 @@ module Capybara
|
|||
#
|
||||
# @return [String] The text of the element
|
||||
#
|
||||
def text
|
||||
def text(type=nil)
|
||||
native.text
|
||||
end
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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/)
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue