2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 15:23:54 -05:00
|
|
|
|
2014-02-16 12:13:58 -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
|
2018-01-13 16:06:03 -05:00
|
|
|
@type = if type.nil?
|
|
|
|
Capybara.ignore_hidden_elements || Capybara.visible_text_only ? :visible : :all
|
|
|
|
else
|
|
|
|
type
|
|
|
|
end
|
2018-03-05 17:57:33 -05:00
|
|
|
|
|
|
|
@expected_text = expected_text.is_a?(Regexp) ? expected_text : expected_text.to_s
|
2016-08-17 19:14:39 -04:00
|
|
|
@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
|
2018-01-13 16:06:03 -05:00
|
|
|
|
2018-02-27 18:46:13 -05:00
|
|
|
@search_regexp = Capybara::Helpers.to_regexp(@expected_text, exact: exact?)
|
2018-01-13 16:06:03 -05:00
|
|
|
|
2014-02-16 12:13:58 -05:00
|
|
|
assert_valid_keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_for(node)
|
2015-11-02 11:04:20 -05:00
|
|
|
@node = node
|
|
|
|
@actual_text = text(node, @type)
|
2014-02-16 12:13:58 -05:00
|
|
|
@count = @actual_text.scan(@search_regexp).size
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message
|
2016-08-30 14:37:54 -04:00
|
|
|
super << build_message(true)
|
2015-11-02 11:04:20 -05:00
|
|
|
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
|
2018-04-27 14:01:47 -04:00
|
|
|
"#{'exact ' if exact?}text #{@expected_text.inspect}"
|
2016-08-30 14:37:54 -04:00
|
|
|
end
|
2015-11-02 11:04:20 -05:00
|
|
|
end
|
|
|
|
|
2018-01-09 17:05:50 -05:00
|
|
|
private
|
2015-11-02 11:04:20 -05:00
|
|
|
|
2016-12-23 15:17:45 -05:00
|
|
|
def exact?
|
2016-12-15 12:04:01 -05:00
|
|
|
options.fetch(:exact, session_options.exact_text)
|
2016-12-23 15:17:45 -05:00
|
|
|
end
|
|
|
|
|
2016-07-21 14:31:59 -04:00
|
|
|
def build_message(report_on_invisible)
|
2018-07-10 17:18:39 -04:00
|
|
|
message = +''
|
2014-02-16 12:13:58 -05:00
|
|
|
unless (COUNT_KEYS & @options.keys).empty?
|
|
|
|
message << " but found #{@count} #{Capybara::Helpers.declension('time', 'times', @count)}"
|
|
|
|
end
|
|
|
|
message << " in #{@actual_text.inspect}"
|
|
|
|
|
2016-07-21 14:31:59 -04:00
|
|
|
details_message = []
|
2018-05-16 15:47:08 -04:00
|
|
|
details_message << case_insensitive_message if @node && check_case_insensitive?
|
2018-05-14 17:30:34 -04:00
|
|
|
details_message << invisible_message if @node && check_visible_text? && report_on_invisible
|
2018-01-11 19:45:50 -05:00
|
|
|
details_message.compact!
|
2016-07-21 14:31:59 -04:00
|
|
|
|
2018-01-11 19:45:50 -05:00
|
|
|
message << ". (However, #{details_message.join(' and ')}.)" unless details_message.empty?
|
|
|
|
message
|
|
|
|
end
|
2016-07-21 14:31:59 -04:00
|
|
|
|
2018-01-11 19:45:50 -05:00
|
|
|
def case_insensitive_message
|
2018-02-27 18:46:13 -05:00
|
|
|
insensitive_regexp = Capybara::Helpers.to_regexp(@expected_text, options: Regexp::IGNORECASE)
|
2018-01-11 19:45:50 -05:00
|
|
|
insensitive_count = @actual_text.scan(insensitive_regexp).size
|
2018-04-27 14:01:47 -04:00
|
|
|
return if insensitive_count == @count
|
|
|
|
"it was found #{insensitive_count} #{Capybara::Helpers.declension('time', 'times', insensitive_count)} using a case insensitive search"
|
2018-01-11 19:45:50 -05:00
|
|
|
end
|
2014-02-16 12:13:58 -05:00
|
|
|
|
2018-01-11 19:45:50 -05:00
|
|
|
def invisible_message
|
|
|
|
invisible_text = text(@node, :all)
|
|
|
|
invisible_count = invisible_text.scan(@search_regexp).size
|
2018-05-16 15:47:08 -04:00
|
|
|
return if invisible_count == @count
|
|
|
|
"it was found #{invisible_count} #{Capybara::Helpers.declension('time', 'times', invisible_count)} including non-visible text"
|
2018-05-14 17:30:34 -04:00
|
|
|
rescue StandardError
|
|
|
|
# An error getting the non-visible text (if element goes out of scope) should not affect the response
|
2018-05-16 15:47:08 -04:00
|
|
|
nil
|
2015-11-02 11:04:20 -05:00
|
|
|
end
|
2014-02-16 12:13:58 -05:00
|
|
|
|
|
|
|
def valid_keys
|
2018-01-08 15:23:54 -05:00
|
|
|
COUNT_KEYS + %i[wait exact]
|
2014-02-16 12:13:58 -05:00
|
|
|
end
|
2015-11-02 11:04:20 -05:00
|
|
|
|
2016-07-21 14:31:59 -04:00
|
|
|
def check_visible_text?
|
|
|
|
@type == :visible
|
2015-11-02 11:04:20 -05:00
|
|
|
end
|
|
|
|
|
2018-05-16 15:47:08 -04:00
|
|
|
def check_case_insensitive?
|
|
|
|
!@expected_text.is_a?(Regexp)
|
|
|
|
end
|
|
|
|
|
2015-11-02 11:04:20 -05:00
|
|
|
def text(node, query_type)
|
2018-03-05 17:57:33 -05:00
|
|
|
node.text(query_type)
|
2015-11-02 11:04:20 -05:00
|
|
|
end
|
2014-02-16 12:13:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|