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

improve error message when case doesn't match

This commit is contained in:
Thomas Walpole 2016-07-21 11:31:59 -07:00
parent 51c83ed5a9
commit 114ec10d12
4 changed files with 30 additions and 11 deletions

View file

@ -13,6 +13,7 @@ Release date: Unreleased
* Remove need to pass nil locator in most node actions when locator is not needed [Thomas Walpole]
* New frames API for drivers - Issue #1365 [Thomas Walpole]
* Deprecated Element#parent in favor of Element#query_scope to better indicate what it is [Thomas Walpole]
* Improved error messages for have_text matcher [Alex Chaffee, Thomas Walpole]
#Version 2.7.1
Release date: 2016-05-01

View file

@ -28,8 +28,8 @@ module Capybara
# @param [String] text Text to escape
# @return [String] Escaped text
#
def to_regexp(text)
text.is_a?(Regexp) ? text : Regexp.new(Regexp.escape(normalize_whitespace(text)))
def to_regexp(text, options=nil)
text.is_a?(Regexp) ? text : Regexp.new(Regexp.escape(normalize_whitespace(text)), options)
end
##

View file

@ -5,6 +5,7 @@ module Capybara
class TextQuery < BaseQuery
def initialize(*args)
@type = (args.first.is_a?(Symbol) || args.first.nil?) ? args.shift : nil
@type = (Capybara.ignore_hidden_elements or Capybara.visible_text_only) ? :visible : :all if @type.nil?
@expected_text, @options = args
unless @expected_text.is_a?(Regexp)
@expected_text = Capybara::Helpers.normalize_whitespace(@expected_text)
@ -30,7 +31,7 @@ module Capybara
private
def build_message(check_invisible)
def build_message(report_on_invisible)
description =
if @expected_text.is_a?(Regexp)
"text matching #{@expected_text.inspect}"
@ -44,14 +45,26 @@ module Capybara
end
message << " in #{@actual_text.inspect}"
if @node and visible? and check_invisible
details_message = []
if @node and !@expected_text.is_a? Regexp
insensitive_regexp = Regexp.new(@expected_text, Regexp::IGNORECASE)
insensitive_count = @actual_text.scan(insensitive_regexp).size
if insensitive_count != @count
details_message << "it was found #{insensitive_count} #{Capybara::Helpers.declension("time", "times", insensitive_count)} using a case insensitive search"
end
end
if @node and check_visible_text? and report_on_invisible
invisible_text = text(@node, :all)
invisible_count = invisible_text.scan(@search_regexp).size
if invisible_count != @count
message << ". (However, it was found #{invisible_count} time#{'s' if invisible_count != 1} including invisible text.)"
details_message << ". it was found #{invisible_count} #{Capybara::Helpers.declension("time", "times", invisible_count)} including non-visible text"
end
end
message << ". (However, #{details_message.join(' and ')}.)" unless details_message.empty?
message
end
@ -59,15 +72,13 @@ module Capybara
COUNT_KEYS + [:wait]
end
def visible?
@type == :visible or
(@type.nil? and (Capybara.ignore_hidden_elements or Capybara.visible_text_only))
def check_visible_text?
@type == :visible
end
def text(node, query_type)
Capybara::Helpers.normalize_whitespace(node.text(query_type))
end
end
end
end

View file

@ -37,12 +37,19 @@ Capybara::SpecHelper.spec '#assert_text' do
expect(@session.assert_text('Some of this text is hidden!')).to eq(true)
end
it "should raise error with an helpful message if the requested text is present but invisible." do
it "should raise error with a helpful message if the requested text is present but invisible" do
@session.visit('/with_html')
el = @session.find(:css, '#hidden-text')
expect do
el.assert_text(:visible, 'Some of this text is hidden!')
end.to raise_error(Capybara::ExpectationNotMet, /However, it was found 1 time including invisible text/)
end.to raise_error(Capybara::ExpectationNotMet, /it was found 1 time including non-visible text/)
end
it "should raise error with a helpful message if the requested text is present but with incorrect case" do
@session.visit('/with_html')
expect do
@session.assert_text('Text With Whitespace')
end.to raise_error(Capybara::ExpectationNotMet, /it was found 1 time using a case insensitive search/)
end
it "should be true if the text in the page matches given regexp" do