rough implementation of has_text matchers

This commit is contained in:
Mark Dodwell 2011-10-15 02:06:44 -07:00
parent acbb81cb90
commit 0c3f377fc4
5 changed files with 60 additions and 1 deletions

View File

@ -196,6 +196,42 @@ module Capybara
has_no_xpath?(XPath::HTML.content(content))
end
##
#
# Checks if the page or current node has the given text content,
# ignoring any HTML tags and normalizing whitespace.
#
# Unlike has_content this only matches text displayed on the page
# and specifically excludes text contained within script nodes.
#
# @param [String] content The text to check for
# @return [Boolean] Whether it exists
#
def has_text?(content)
literal = XPath::Expression::StringLiteral.new(content).to_xpath
expression = "./descendant-or-self::*[text()[contains(normalize-space(.), #{literal})] and not(ancestor-or-self::script)]"
has_xpath?(expression)
end
##
#
# Checks if the page or current node does not have the given text
# content, ignoring any HTML tags and normalizing whitespace.
#
# Unlike has_content this only matches text displayed on the page
# and specifically excludes text contained within script nodes.
#
# @param [String] content The text to check for
# @return [Boolean] Whether it exists
#
def has_no_text?(content)
literal = XPath::Expression::StringLiteral.new(content).to_xpath
expression = "./descendant-or-self::*[text()[contains(normalize-space(.), #{literal})] and not(ancestor-or-self::script)]"
has_no_xpath?(expression)
end
##
#
# Checks if the page or current node has a link with the given

View File

@ -122,6 +122,12 @@ module Capybara
%(expected there to be content #{matcher.locator.inspect} in #{page.text.inspect})
end
end
def have_text(text)
HaveMatcher.new(:text, text.to_s) do |page, matcher|
%(expected there to be text #{matcher.locator.inspect} in #{page.text.inspect})
end
end
def have_link(locator, options={})
HaveMatcher.new(:link, locator, options)

View File

@ -30,7 +30,8 @@ module Capybara
:all, :first, :attach_file, :text, :check, :choose,
:click_link_or_button, :click_button, :click_link, :field_labeled,
:fill_in, :find, :find_button, :find_by_id, :find_field, :find_link,
:has_content?, :has_css?, :has_no_content?, :has_no_css?, :has_no_xpath?,
:has_content?, :has_text?, :has_css?, :has_no_content?, :has_no_text?,
:has_no_css?, :has_no_xpath?,
:has_xpath?, :select, :uncheck, :has_link?, :has_no_link?, :has_button?,
:has_no_button?, :has_field?, :has_no_field?, :has_checked_field?,
:has_unchecked_field?, :has_no_table?, :has_table?, :unselect,

View File

@ -98,6 +98,7 @@ shared_examples_for "session" do
it_should_behave_like "find_by_id"
it_should_behave_like "find"
it_should_behave_like "has_content"
it_should_behave_like "has_text"
it_should_behave_like "has_css"
it_should_behave_like "has_css"
it_should_behave_like "has_selector"

View File

@ -0,0 +1,15 @@
shared_examples_for 'has_text' do
describe '#has_text?' do
it 'works' do
@session.visit('/with_html')
@session.should have_text('Lorem')
end
end
describe '#has_no_text?' do
it 'works' do
@session.visit('/with_html')
@session.should have_no_text('Merol')
end
end
end