2016-01-29 19:31:35 -05:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 15:23:54 -05:00
|
|
|
|
2016-01-29 19:31:35 -05:00
|
|
|
module Capybara
|
|
|
|
module Queries
|
|
|
|
class SelectorQuery < Queries::BaseQuery
|
|
|
|
attr_accessor :selector, :locator, :options, :expression, :find, :negative
|
|
|
|
|
2018-01-08 15:23:54 -05:00
|
|
|
VALID_KEYS = COUNT_KEYS + %i[text id class visible exact exact_text match wait filter_set]
|
|
|
|
VALID_MATCH = %i[first smart prefer_exact one].freeze
|
2016-01-29 19:31:35 -05:00
|
|
|
|
2016-08-17 19:14:39 -04:00
|
|
|
def initialize(*args, session_options:, **options, &filter_block)
|
2017-09-20 20:08:02 -04:00
|
|
|
@resolved_node = nil
|
2016-08-17 19:14:39 -04:00
|
|
|
@options = options.dup
|
2017-05-28 11:54:55 -04:00
|
|
|
super(@options)
|
2016-08-17 19:14:39 -04:00
|
|
|
self.session_options = session_options
|
2016-09-23 18:03:36 -04:00
|
|
|
@filter_block = filter_block
|
2016-01-29 19:31:35 -05:00
|
|
|
|
2018-01-08 15:23:54 -05:00
|
|
|
@selector = if args[0].is_a?(Symbol)
|
|
|
|
Selector.all.fetch(args.shift) do |selector_type|
|
2016-12-15 12:04:01 -05:00
|
|
|
raise ArgumentError, "Unknown selector type (:#{selector_type})"
|
2016-09-26 14:20:15 -04:00
|
|
|
end
|
2016-01-29 19:31:35 -05:00
|
|
|
else
|
2018-01-08 15:23:54 -05:00
|
|
|
Selector.all.values.find { |s| s.match?(args[0]) }
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
2018-01-08 15:23:54 -05:00
|
|
|
@locator = args.shift
|
2016-12-15 12:04:01 -05:00
|
|
|
@selector ||= Selector.all[session_options.default_selector]
|
2016-01-29 19:31:35 -05:00
|
|
|
|
2017-10-10 12:49:51 -04:00
|
|
|
warn "Unused parameters passed to #{self.class.name} : #{args}" unless args.empty?
|
2016-04-04 18:36:43 -04:00
|
|
|
|
2016-12-15 12:04:01 -05:00
|
|
|
@expression = @selector.call(@locator, @options.merge(enable_aria_label: session_options.enable_aria_label))
|
2016-08-24 17:07:05 -04:00
|
|
|
|
2016-08-25 13:07:45 -04:00
|
|
|
warn_exact_usage
|
2016-08-24 17:07:05 -04:00
|
|
|
|
2016-01-29 19:31:35 -05:00
|
|
|
assert_valid_keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def name; selector.name; end
|
|
|
|
def label; selector.label or selector.name; end
|
|
|
|
|
|
|
|
def description
|
2018-01-09 17:05:50 -05:00
|
|
|
@description = "".dup
|
2017-07-19 18:59:40 -04:00
|
|
|
@description << "visible " if visible == :visible
|
|
|
|
@description << "non-visible " if visible == :hidden
|
2017-07-19 15:37:15 -04:00
|
|
|
@description << "#{label} #{locator.inspect}"
|
2017-01-26 19:10:51 -05:00
|
|
|
@description << " with#{" exact" if exact_text == true} text #{options[:text].inspect}" if options[:text]
|
2016-12-23 15:17:45 -05:00
|
|
|
@description << " with exact text #{options[:exact_text]}" if options[:exact_text].is_a?(String)
|
2016-09-22 19:55:54 -04:00
|
|
|
@description << " with id #{options[:id]}" if options[:id]
|
2017-05-18 18:37:48 -04:00
|
|
|
@description << " with classes [#{Array(options[:class]).join(',')}]" if options[:class]
|
2016-01-29 19:31:35 -05:00
|
|
|
@description << selector.description(options)
|
2016-09-23 18:03:36 -04:00
|
|
|
@description << " that also matches the custom filter block" if @filter_block
|
2017-09-20 20:08:02 -04:00
|
|
|
@description << " within #{@resolved_node.inspect}" if describe_within?
|
2016-01-29 19:31:35 -05:00
|
|
|
@description
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches_filters?(node)
|
2018-01-09 17:05:50 -05:00
|
|
|
return false if options[:text] && !matches_text_filter(node, options[:text])
|
|
|
|
return false if exact_text.is_a?(String) && !matches_exact_text_filter(node, exact_text)
|
2016-08-05 17:44:33 -04:00
|
|
|
|
2016-01-29 19:31:35 -05:00
|
|
|
case visible
|
2018-01-09 17:05:50 -05:00
|
|
|
when :visible then return false unless node.visible?
|
|
|
|
when :hidden then return false if node.visible?
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
2016-08-05 17:44:33 -04:00
|
|
|
|
2018-01-11 19:45:50 -05:00
|
|
|
matches_node_filters?(node) && matches_filter_block?(node)
|
2017-05-31 17:07:34 -04:00
|
|
|
rescue *(node.respond_to?(:session) ? node.session.driver.invalid_element_errors : [])
|
2017-05-31 14:50:32 -04:00
|
|
|
return false
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def visible
|
2018-01-09 17:05:50 -05:00
|
|
|
case (vis = options.fetch(:visible) { @selector.default_visibility(session_options.ignore_hidden_elements) })
|
|
|
|
when true then :visible
|
|
|
|
when false then :all
|
|
|
|
else vis
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def exact?
|
2018-01-09 17:05:50 -05:00
|
|
|
return false unless supports_exact?
|
2016-12-15 12:04:01 -05:00
|
|
|
options.fetch(:exact, session_options.exact)
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def match
|
2016-12-15 12:04:01 -05:00
|
|
|
options.fetch(:match, session_options.match)
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
|
2018-01-09 17:05:50 -05:00
|
|
|
def xpath(exact = nil)
|
|
|
|
exact = exact? if exact.nil?
|
2016-10-05 18:16:00 -04:00
|
|
|
expr = apply_expression_filters(@expression)
|
2018-01-09 17:05:50 -05:00
|
|
|
expr = exact ? expr.to_xpath(:exact) : expr.to_s if expr.respond_to?(:to_xpath)
|
2016-09-22 19:55:54 -04:00
|
|
|
filtered_xpath(expr)
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def css
|
2016-10-05 18:16:00 -04:00
|
|
|
filtered_css(apply_expression_filters(@expression))
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# @api private
|
|
|
|
def resolve_for(node, exact = nil)
|
2017-09-20 20:08:02 -04:00
|
|
|
@resolved_node = node
|
2016-01-29 19:31:35 -05:00
|
|
|
node.synchronize do
|
|
|
|
children = if selector.format == :css
|
2018-01-09 17:05:50 -05:00
|
|
|
node.find_css(css)
|
2016-01-29 19:31:35 -05:00
|
|
|
else
|
2018-01-09 17:05:50 -05:00
|
|
|
node.find_xpath(xpath(exact))
|
2016-01-29 19:31:35 -05:00
|
|
|
end.map do |child|
|
|
|
|
if node.is_a?(Capybara::Node::Base)
|
|
|
|
Capybara::Node::Element.new(node.session, child, node, self)
|
|
|
|
else
|
|
|
|
Capybara::Node::Simple.new(child)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Capybara::Result.new(children, self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-24 17:07:05 -04:00
|
|
|
# @api private
|
|
|
|
def supports_exact?
|
|
|
|
@expression.respond_to? :to_xpath
|
|
|
|
end
|
|
|
|
|
2018-01-09 17:05:50 -05:00
|
|
|
private
|
2016-01-29 19:31:35 -05:00
|
|
|
|
|
|
|
def valid_keys
|
2016-09-01 18:19:48 -04:00
|
|
|
VALID_KEYS + custom_keys
|
2015-01-26 17:50:59 -05:00
|
|
|
end
|
|
|
|
|
2018-01-11 19:45:50 -05:00
|
|
|
def matches_node_filters?(node)
|
|
|
|
node_filters.all? do |name, filter|
|
|
|
|
if options.key?(name)
|
|
|
|
filter.matches?(node, options[name])
|
|
|
|
elsif filter.default?
|
|
|
|
filter.matches?(node, filter.default)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches_filter_block?(node)
|
|
|
|
return true unless @filter_block
|
|
|
|
|
|
|
|
if node.respond_to?(:session)
|
|
|
|
node.session.using_wait_time(0) { @filter_block.call(node) }
|
|
|
|
else
|
|
|
|
@filter_block.call(node)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 18:16:00 -04:00
|
|
|
def node_filters
|
2018-01-09 17:05:50 -05:00
|
|
|
if options.key?(:filter_set)
|
2016-10-05 18:16:00 -04:00
|
|
|
::Capybara::Selector::FilterSet.all[options[:filter_set]].node_filters
|
2015-01-26 17:50:59 -05:00
|
|
|
else
|
2016-10-05 18:16:00 -04:00
|
|
|
@selector.node_filters
|
2015-01-26 17:50:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 18:16:00 -04:00
|
|
|
def expression_filters
|
|
|
|
filters = @selector.expression_filters
|
2018-01-09 17:05:50 -05:00
|
|
|
filters.merge ::Capybara::Selector::FilterSet.all[options[:filter_set]].expression_filters if options.key?(:filter_set)
|
2016-10-05 18:16:00 -04:00
|
|
|
filters
|
|
|
|
end
|
|
|
|
|
2015-01-26 17:50:59 -05:00
|
|
|
def custom_keys
|
2016-10-05 18:16:00 -04:00
|
|
|
@custom_keys ||= node_filters.keys + expression_filters.keys
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_valid_keys
|
|
|
|
super
|
|
|
|
unless VALID_MATCH.include?(match)
|
|
|
|
raise ArgumentError, "invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(", ")}"
|
|
|
|
end
|
|
|
|
end
|
2016-08-24 17:07:05 -04:00
|
|
|
|
2016-09-22 19:55:54 -04:00
|
|
|
def filtered_xpath(expr)
|
2018-01-09 17:05:50 -05:00
|
|
|
if options.key?(:id) || options.key?(:class)
|
2016-09-22 19:55:54 -04:00
|
|
|
expr = "(#{expr})"
|
2018-01-09 17:05:50 -05:00
|
|
|
expr = "#{expr}[#{XPath.attr(:id) == options[:id]}]" if options.key?(:id) && !custom_keys.include?(:id)
|
|
|
|
if options.key?(:class) && !custom_keys.include?(:class)
|
2016-09-22 19:55:54 -04:00
|
|
|
class_xpath = Array(options[:class]).map do |klass|
|
2018-01-02 20:46:41 -05:00
|
|
|
XPath.attr(:class).contains_word(klass)
|
|
|
|
end.reduce(:&)
|
2016-09-22 19:55:54 -04:00
|
|
|
expr = "#{expr}[#{class_xpath}]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
expr
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_css(expr)
|
2018-01-09 17:05:50 -05:00
|
|
|
if options.key?(:id) || options.key?(:class)
|
2016-09-22 19:55:54 -04:00
|
|
|
css_selectors = expr.split(',').map(&:rstrip)
|
|
|
|
expr = css_selectors.map do |sel|
|
2018-01-09 17:05:50 -05:00
|
|
|
sel += "##{Capybara::Selector::CSS.escape(options[:id])}" if options.key?(:id) && !custom_keys.include?(:id)
|
|
|
|
sel += Array(options[:class]).map { |k| ".#{Capybara::Selector::CSS.escape(k)}" }.join if options.key?(:class) && !custom_keys.include?(:class)
|
|
|
|
sel
|
2016-09-22 19:55:54 -04:00
|
|
|
end.join(", ")
|
|
|
|
end
|
|
|
|
expr
|
|
|
|
end
|
|
|
|
|
2016-10-05 18:16:00 -04:00
|
|
|
def apply_expression_filters(expr)
|
|
|
|
expression_filters.inject(expr) do |memo, (name, ef)|
|
2018-01-09 17:05:50 -05:00
|
|
|
if options.key?(name)
|
2016-10-05 18:16:00 -04:00
|
|
|
ef.apply_filter(memo, options[name])
|
|
|
|
elsif ef.default?
|
|
|
|
ef.apply_filter(memo, ef.default)
|
|
|
|
else
|
|
|
|
memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-25 13:07:45 -04:00
|
|
|
def warn_exact_usage
|
2018-01-09 17:05:50 -05:00
|
|
|
return unless options.key?(:exact) && !supports_exact?
|
|
|
|
warn "The :exact option only has an effect on queries using the XPath#is method. Using it with the query \"#{expression}\" has no effect."
|
2016-08-24 17:07:05 -04:00
|
|
|
end
|
2016-12-23 15:17:45 -05:00
|
|
|
|
|
|
|
def exact_text
|
2016-12-15 12:04:01 -05:00
|
|
|
options.fetch(:exact_text, session_options.exact_text)
|
2016-12-23 15:17:45 -05:00
|
|
|
end
|
2017-09-20 20:08:02 -04:00
|
|
|
|
|
|
|
def describe_within?
|
|
|
|
@resolved_node && !(@resolved_node.is_a?(::Capybara::Node::Document) ||
|
|
|
|
(@resolved_node.is_a?(::Capybara::Node::Simple) && @resolved_node.path == '/'))
|
|
|
|
end
|
2017-11-13 16:04:47 -05:00
|
|
|
|
|
|
|
def matches_text_filter(node, text_option)
|
|
|
|
regexp = if text_option.is_a?(Regexp)
|
|
|
|
text_option
|
2018-01-08 15:23:54 -05:00
|
|
|
elsif exact_text == true
|
|
|
|
/\A#{Regexp.escape(text_option.to_s)}\z/
|
2017-11-13 16:04:47 -05:00
|
|
|
else
|
2018-01-08 15:23:54 -05:00
|
|
|
Regexp.escape(text_option.to_s)
|
2017-11-13 16:04:47 -05:00
|
|
|
end
|
|
|
|
text_visible = visible
|
|
|
|
text_visible = :all if text_visible == :hidden
|
|
|
|
node.text(text_visible).match(regexp)
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches_exact_text_filter(node, exact_text_option)
|
|
|
|
regexp = /\A#{Regexp.escape(exact_text_option)}\z/
|
|
|
|
text_visible = visible
|
|
|
|
text_visible = :all if text_visible == :hidden
|
|
|
|
node.text(text_visible).match(regexp)
|
|
|
|
end
|
2016-01-29 19:31:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|