123456789_123456789_123456789_123456789_123456789_

Class: Capybara::Queries::TextQuery Private

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, BaseQuery
Instance Chain:
self, BaseQuery
Inherits: Capybara::Queries::BaseQuery
Defined in: lib/capybara/queries/text_query.rb

Constant Summary

BaseQuery - Inherited

COUNT_KEYS

Class Method Summary

Instance Attribute Summary

BaseQuery - Inherited

#expects_none?

Checks if a count of 0 is valid for the query Returns false if query does not have any count options specified.

#options, #session_options, #session_options=, #count_specified?

Instance Method Summary

BaseQuery - Inherited

#failure_message

Generates a failure message from the query description and count options.

#matches_count?

Checks if the given count matches the query count options.

#negative_failure_message, #wait, #assert_valid_keys, #count_message, #occurrences

Constructor Details

.new(type = nil, expected_text, session_options:, **options) ⇒ TextQuery

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 7

def initialize(type = nil, expected_text, session_options:, **options) # rubocop:disable Style/OptionalArguments
  @type = type.nil? ? default_type : type
  raise ArgumentError, "#{@type} is not a valid type for a text query" unless valid_types.include?(@type)

  @options = options
  super(@options)
  self.session_options = session_options

  if expected_text.nil? && !exact?
    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

  @search_regexp = Capybara::Helpers.to_regexp(@expected_text, exact: exact?)

  assert_valid_keys
end

Instance Attribute Details

#check_case_insensitive?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 102

def check_case_insensitive?
  !@expected_text.is_a?(Regexp)
end

#check_visible_text?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 98

def check_visible_text?
  @type == :visible
end

#exact?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 51

def exact?
  options.fetch(:exact, session_options.exact_text)
end

Instance Method Details

#build_message(report_on_invisible) (private)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 55

def build_message(report_on_invisible)
  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 = []
  details_message << case_insensitive_message if @node && check_case_insensitive?
  details_message << invisible_message if @node && check_visible_text? && report_on_invisible
  details_message.compact!

  message << ". (However, #{details_message.join(' and ')}.)" unless details_message.empty?
  message
end

#case_insensitive_message (private)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 71

def case_insensitive_message
  insensitive_regexp = Capybara::Helpers.to_regexp(@expected_text, options: Regexp::IGNORECASE)
  insensitive_count = @actual_text.scan(insensitive_regexp).size
  return if insensitive_count == @count

  "it was found #{occurrences insensitive_count} using a case insensitive search"
end

#default_type (private)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 111

def default_type
  Capybara.ignore_hidden_elements || Capybara.visible_text_only ? :visible : :all
end

#description

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 41

def description
  if @expected_text.is_a?(Regexp)
    "text matching #{@expected_text.inspect}"
  else
    "#{'exact ' if exact?}text #{@expected_text.inspect}"
  end
end

#failure_message

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 33

def failure_message
  super << build_message(true)
end

#invisible_message (private)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 79

def invisible_message
  invisible_text = text(query_type: :all)
  invisible_count = invisible_text.scan(@search_regexp).size
  return if invisible_count == @count

  "it was found #{occurrences invisible_count} including non-visible text"
rescue StandardError
  # An error getting the non-visible text (if element goes out of scope) should not affect the response
  nil
end

#negative_failure_message

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 37

def negative_failure_message
  super << build_message(false)
end

#resolve_for(node)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 27

def resolve_for(node)
  @node = node
  @actual_text = text
  @count = @actual_text.scan(@search_regexp).size
end

#text(node: @node, query_type: @type) (private)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 106

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

#valid_keys (private)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 90

def valid_keys
  COUNT_KEYS + %i[wait exact normalize_ws]
end

#valid_types (private)

[ GitHub ]

  
# File 'lib/capybara/queries/text_query.rb', line 94

def valid_types
  %i[all visible]
end