teamcapybara--capybara/lib/capybara/queries/text_query.rb

117 lines
3.6 KiB
Ruby
Raw Normal View History

2016-03-08 00:52:19 +00:00
# frozen_string_literal: true
2018-01-08 20:23:54 +00:00
module Capybara
# @api private
module Queries
class TextQuery < BaseQuery
2018-01-09 22:05:50 +00:00
def initialize(type = nil, expected_text, session_options:, **options) # rubocop:disable Style/OptionalArguments
@type = type.nil? ? default_type : type
2020-06-22 08:52:41 +00:00
raise ArgumentError, "#{@type} is not a valid type for a text query" unless valid_types.include?(@type)
2020-06-20 23:21:59 +00:00
2016-08-17 23:14:39 +00:00
@options = options
2017-05-28 15:54:55 +00:00
super(@options)
2016-08-17 23:14:39 +00:00
self.session_options = session_options
2018-01-13 21:06:03 +00:00
if expected_text.nil? && !exact?
2022-07-09 18:20:40 +00:00
warn 'Checking for expected text of nil is confusing and/or pointless since it will always match. ' \
"Please specify a string or regexp instead. #{Capybara::Helpers.filter_backtrace(caller)}"
end
@expected_text = expected_text.is_a?(Regexp) ? expected_text : expected_text.to_s
2018-02-27 23:46:13 +00:00
@search_regexp = Capybara::Helpers.to_regexp(@expected_text, exact: exact?)
2018-01-13 21:06:03 +00:00
assert_valid_keys
end
def resolve_for(node)
@node = node
@actual_text = text
@count = @actual_text.scan(@search_regexp).size
end
def failure_message
2016-08-30 18:37:54 +00:00
super << build_message(true)
end
def negative_failure_message
2016-08-30 18:37:54 +00:00
super << build_message(false)
end
def description
if @expected_text.is_a?(Regexp)
"text matching #{@expected_text.inspect}"
else
"#{'exact ' if exact?}text #{@expected_text.inspect}"
2016-08-30 18:37:54 +00:00
end
end
2018-01-09 22:05:50 +00:00
private
2016-12-23 20:17:45 +00:00
def exact?
options.fetch(:exact, session_options.exact_text)
2016-12-23 20:17:45 +00:00
end
def build_message(report_on_invisible)
2018-07-10 21:18:39 +00:00
message = +''
unless (COUNT_KEYS & @options.keys).empty?
message << " but found #{@count} #{Capybara::Helpers.declension('time', 'times', @count)}"
end
message << " in #{@actual_text.inspect}"
details_message = []
2018-05-16 19:47:08 +00:00
details_message << case_insensitive_message if @node && check_case_insensitive?
2018-05-14 21:30:34 +00:00
details_message << invisible_message if @node && check_visible_text? && report_on_invisible
2018-01-12 00:45:50 +00:00
details_message.compact!
2018-01-12 00:45:50 +00:00
message << ". (However, #{details_message.join(' and ')}.)" unless details_message.empty?
message
end
2018-01-12 00:45:50 +00:00
def case_insensitive_message
2018-02-27 23:46:13 +00:00
insensitive_regexp = Capybara::Helpers.to_regexp(@expected_text, options: Regexp::IGNORECASE)
2018-01-12 00:45:50 +00:00
insensitive_count = @actual_text.scan(insensitive_regexp).size
return if insensitive_count == @count
2018-09-24 16:43:46 +00:00
"it was found #{occurrences insensitive_count} using a case insensitive search"
2018-01-12 00:45:50 +00:00
end
2018-01-12 00:45:50 +00:00
def invisible_message
invisible_text = text(query_type: :all)
2018-01-12 00:45:50 +00:00
invisible_count = invisible_text.scan(@search_regexp).size
2018-05-16 19:47:08 +00:00
return if invisible_count == @count
2018-09-24 16:43:46 +00:00
"it was found #{occurrences invisible_count} including non-visible text"
2018-05-14 21:30:34 +00:00
rescue StandardError
# An error getting the non-visible text (if element goes out of scope) should not affect the response
2018-05-16 19:47:08 +00:00
nil
end
def valid_keys
2018-07-30 16:23:29 +00:00
COUNT_KEYS + %i[wait exact normalize_ws]
end
2020-06-20 23:21:59 +00:00
def valid_types
%i[all visible]
end
def check_visible_text?
@type == :visible
end
2018-05-16 19:47:08 +00:00
def check_case_insensitive?
!@expected_text.is_a?(Regexp)
end
def text(node: @node, query_type: @type)
normalize_ws = options.fetch(:normalize_ws, session_options.default_normalize_ws)
node.text(query_type, normalize_ws: normalize_ws)
end
def default_type
Capybara.ignore_hidden_elements || Capybara.visible_text_only ? :visible : :all
end
end
end
end