2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2014-02-16 12:13:58 -05:00
|
|
|
module Capybara
|
|
|
|
# @api private
|
|
|
|
module Queries
|
|
|
|
class TextQuery < BaseQuery
|
|
|
|
def initialize(*args)
|
2015-07-21 04:26:34 -04:00
|
|
|
@type = (args.first.is_a?(Symbol) || args.first.nil?) ? args.shift : nil
|
2016-12-15 12:04:01 -05:00
|
|
|
# @type = (Capybara.ignore_hidden_elements or Capybara.visible_text_only) ? :visible : :all if @type.nil?
|
|
|
|
@options = if args.last.is_a?(Hash) then args.pop.dup else {} end
|
2017-05-28 11:54:55 -04:00
|
|
|
super(@options)
|
2016-12-15 12:04:01 -05:00
|
|
|
|
|
|
|
@type = (session_options.ignore_hidden_elements or session_options.visible_text_only) ? :visible : :all if @type.nil?
|
|
|
|
|
|
|
|
@expected_text = args.shift
|
2014-02-16 12:13:58 -05:00
|
|
|
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?)
|
2017-10-10 12:49:51 -04:00
|
|
|
warn "Unused parameters passed to #{self.class.name} : #{args}" unless args.empty?
|
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
|
2016-12-23 15:17:45 -05: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
|
|
|
|
|
|
|
|
private
|
|
|
|
|
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)
|
2016-08-30 14:37:54 -04:00
|
|
|
message = String.new()
|
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 = []
|
|
|
|
|
|
|
|
if @node and !@expected_text.is_a? Regexp
|
2016-08-18 13:05:10 -04:00
|
|
|
insensitive_regexp = Capybara::Helpers.to_regexp(@expected_text, Regexp::IGNORECASE)
|
2016-07-21 14:31:59 -04:00
|
|
|
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
|
2017-01-18 21:14:19 -05:00
|
|
|
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
|
|
|
|
rescue
|
|
|
|
# An error getting the non-visible text (if element goes out of scope) should not affect the response
|
2015-11-02 11:04:20 -05:00
|
|
|
end
|
|
|
|
end
|
2014-02-16 12:13:58 -05:00
|
|
|
|
2016-07-21 14:31:59 -04:00
|
|
|
message << ". (However, #{details_message.join(' and ')}.)" unless details_message.empty?
|
|
|
|
|
2015-11-02 11:04:20 -05:00
|
|
|
message
|
|
|
|
end
|
2014-02-16 12:13:58 -05:00
|
|
|
|
|
|
|
def valid_keys
|
2016-12-23 15:17:45 -05:00
|
|
|
COUNT_KEYS + [: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
|
|
|
|
|
|
|
|
def text(node, query_type)
|
|
|
|
Capybara::Helpers.normalize_whitespace(node.text(query_type))
|
|
|
|
end
|
2014-02-16 12:13:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|