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

96 lines
3 KiB
Ruby
Raw Normal View History

2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2018-01-08 15:23:54 -05:00
module Capybara
# @api private
module Queries
class TextQuery < BaseQuery
2018-01-09 17:05:50 -05:00
def initialize(type = nil, expected_text, session_options:, **options) # rubocop:disable Style/OptionalArguments
2016-08-17 19:14:39 -04:00
@type = type
2018-01-09 17:05:50 -05:00
@type = Capybara.ignore_hidden_elements || Capybara.visible_text_only ? :visible : :all if @type.nil?
2016-08-17 19:14:39 -04:00
@expected_text = expected_text
@options = options
2017-05-28 11:54:55 -04:00
super(@options)
2016-08-17 19:14:39 -04:00
self.session_options = session_options
unless @expected_text.is_a?(Regexp)
@expected_text = Capybara::Helpers.normalize_whitespace(@expected_text)
end
2016-12-23 15:17:45 -05:00
@search_regexp = Capybara::Helpers.to_regexp(@expected_text, nil, exact?)
assert_valid_keys
end
def resolve_for(node)
@node = node
@actual_text = text(node, @type)
@count = @actual_text.scan(@search_regexp).size
end
def failure_message
2016-08-30 14:37:54 -04:00
super << build_message(true)
end
def negative_failure_message
2016-08-30 14:37:54 -04:00
super << build_message(false)
end
def description
if @expected_text.is_a?(Regexp)
"text matching #{@expected_text.inspect}"
else
2016-12-23 15:17:45 -05:00
"#{"exact " if exact?}text #{@expected_text.inspect}"
2016-08-30 14:37:54 -04:00
end
end
2018-01-09 17:05:50 -05:00
private
2016-12-23 15:17:45 -05:00
def exact?
options.fetch(:exact, session_options.exact_text)
2016-12-23 15:17:45 -05:00
end
def build_message(report_on_invisible)
2018-01-09 17:05:50 -05:00
message = "".dup
unless (COUNT_KEYS & @options.keys).empty?
message << " but found #{@count} #{Capybara::Helpers.declension('time', 'times', @count)}"
end
message << " in #{@actual_text.inspect}"
details_message = []
if @node and !@expected_text.is_a? Regexp
insensitive_regexp = Capybara::Helpers.to_regexp(@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
begin
invisible_text = text(@node, :all)
invisible_count = invisible_text.scan(@search_regexp).size
if invisible_count != @count
details_message << "it was found #{invisible_count} #{Capybara::Helpers.declension("time", "times", invisible_count)} including non-visible text"
end
2018-01-09 17:05:50 -05:00
rescue # An error getting the non-visible text (if element goes out of scope) should not affect the response
end
end
message << ". (However, #{details_message.join(' and ')}.)" unless details_message.empty?
message
end
def valid_keys
2018-01-08 15:23:54 -05:00
COUNT_KEYS + %i[wait exact]
end
def check_visible_text?
@type == :visible
end
def text(node, query_type)
Capybara::Helpers.normalize_whitespace(node.text(query_type))
end
end
end
end