2016-01-30 00:31:35 +00:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 20:23:54 +00:00
|
|
|
|
2016-01-30 00:31:35 +00:00
|
|
|
module Capybara
|
|
|
|
module Queries
|
|
|
|
class SelectorQuery < Queries::BaseQuery
|
|
|
|
attr_accessor :selector, :locator, :options, :expression, :find, :negative
|
|
|
|
|
2018-01-08 20:23:54 +00: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-30 00:31:35 +00:00
|
|
|
|
2016-08-17 23:14:39 +00:00
|
|
|
def initialize(*args, session_options:, **options, &filter_block)
|
2017-09-21 00:08:02 +00:00
|
|
|
@resolved_node = nil
|
2016-08-17 23:14:39 +00:00
|
|
|
@options = options.dup
|
2017-05-28 15:54:55 +00:00
|
|
|
super(@options)
|
2016-08-17 23:14:39 +00:00
|
|
|
self.session_options = session_options
|
2016-01-30 00:31:35 +00:00
|
|
|
|
2018-01-13 21:06:03 +00:00
|
|
|
@selector = find_selector(args[0].is_a?(Symbol) ? args.shift : args[0])
|
2018-01-08 20:23:54 +00:00
|
|
|
@locator = args.shift
|
2018-01-13 21:06:03 +00:00
|
|
|
@filter_block = filter_block
|
2016-01-30 00:31:35 +00:00
|
|
|
|
2018-02-01 22:02:39 +00:00
|
|
|
raise ArgumentError, "Unused parameters passed to #{self.class.name} : #{args}" unless args.empty?
|
2016-04-04 22:36:43 +00:00
|
|
|
|
2016-12-15 17:04:01 +00:00
|
|
|
@expression = @selector.call(@locator, @options.merge(enable_aria_label: session_options.enable_aria_label))
|
2016-08-24 21:07:05 +00:00
|
|
|
|
2016-08-25 17:07:45 +00:00
|
|
|
warn_exact_usage
|
2016-08-24 21:07:05 +00:00
|
|
|
|
2016-01-30 00:31:35 +00:00
|
|
|
assert_valid_keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def name; selector.name; end
|
2018-01-13 21:06:03 +00:00
|
|
|
def label; selector.label || selector.name; end
|
2016-01-30 00:31:35 +00:00
|
|
|
|
|
|
|
def description
|
2018-05-10 20:20:23 +00:00
|
|
|
@description = +""
|
2017-07-19 22:59:40 +00:00
|
|
|
@description << "visible " if visible == :visible
|
|
|
|
@description << "non-visible " if visible == :hidden
|
2017-07-19 19:37:15 +00:00
|
|
|
@description << "#{label} #{locator.inspect}"
|
2018-04-27 18:01:47 +00:00
|
|
|
@description << " with#{' exact' if exact_text == true} text #{options[:text].inspect}" if options[:text]
|
2018-05-16 19:47:08 +00:00
|
|
|
@description << " with exact text #{exact_text}" if exact_text.is_a?(String)
|
2016-09-22 23:55:54 +00:00
|
|
|
@description << " with id #{options[:id]}" if options[:id]
|
2017-05-18 22:37:48 +00:00
|
|
|
@description << " with classes [#{Array(options[:class]).join(',')}]" if options[:class]
|
2016-01-30 00:31:35 +00:00
|
|
|
@description << selector.description(options)
|
2016-09-23 22:03:36 +00:00
|
|
|
@description << " that also matches the custom filter block" if @filter_block
|
2017-09-21 00:08:02 +00:00
|
|
|
@description << " within #{@resolved_node.inspect}" if describe_within?
|
2016-01-30 00:31:35 +00:00
|
|
|
@description
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches_filters?(node)
|
2018-01-09 22:05:50 +00: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 21:44:33 +00:00
|
|
|
|
2016-01-30 00:31:35 +00:00
|
|
|
case visible
|
2018-01-09 22:05:50 +00:00
|
|
|
when :visible then return false unless node.visible?
|
|
|
|
when :hidden then return false if node.visible?
|
2016-01-30 00:31:35 +00:00
|
|
|
end
|
2016-08-05 21:44:33 +00:00
|
|
|
|
2018-01-12 00:45:50 +00:00
|
|
|
matches_node_filters?(node) && matches_filter_block?(node)
|
2017-05-31 21:07:34 +00:00
|
|
|
rescue *(node.respond_to?(:session) ? node.session.driver.invalid_element_errors : [])
|
2018-05-14 21:30:34 +00:00
|
|
|
false
|
2016-01-30 00:31:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def visible
|
2018-01-09 22:05:50 +00: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-30 00:31:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def exact?
|
2018-01-13 21:06:03 +00:00
|
|
|
supports_exact? ? options.fetch(:exact, session_options.exact) : false
|
2016-01-30 00:31:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def match
|
2016-12-15 17:04:01 +00:00
|
|
|
options.fetch(:match, session_options.match)
|
2016-01-30 00:31:35 +00:00
|
|
|
end
|
|
|
|
|
2018-01-09 22:05:50 +00:00
|
|
|
def xpath(exact = nil)
|
|
|
|
exact = exact? if exact.nil?
|
2016-10-05 22:16:00 +00:00
|
|
|
expr = apply_expression_filters(@expression)
|
2018-01-09 22:05:50 +00:00
|
|
|
expr = exact ? expr.to_xpath(:exact) : expr.to_s if expr.respond_to?(:to_xpath)
|
2016-09-22 23:55:54 +00:00
|
|
|
filtered_xpath(expr)
|
2016-01-30 00:31:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def css
|
2016-10-05 22:16:00 +00:00
|
|
|
filtered_css(apply_expression_filters(@expression))
|
2016-01-30 00:31:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# @api private
|
|
|
|
def resolve_for(node, exact = nil)
|
2017-09-21 00:08:02 +00:00
|
|
|
@resolved_node = node
|
2016-01-30 00:31:35 +00:00
|
|
|
node.synchronize do
|
|
|
|
children = if selector.format == :css
|
2018-01-09 22:05:50 +00:00
|
|
|
node.find_css(css)
|
2016-01-30 00:31:35 +00:00
|
|
|
else
|
2018-01-09 22:05:50 +00:00
|
|
|
node.find_xpath(xpath(exact))
|
2016-01-30 00:31:35 +00: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 21:07:05 +00:00
|
|
|
# @api private
|
|
|
|
def supports_exact?
|
|
|
|
@expression.respond_to? :to_xpath
|
|
|
|
end
|
|
|
|
|
2018-01-09 22:05:50 +00:00
|
|
|
private
|
2016-01-30 00:31:35 +00:00
|
|
|
|
2018-01-13 21:06:03 +00:00
|
|
|
def find_selector(locator)
|
|
|
|
selector = if locator.is_a?(Symbol)
|
|
|
|
Selector.all.fetch(locator) { |sel_type| raise ArgumentError, "Unknown selector type (:#{sel_type})" }
|
|
|
|
else
|
|
|
|
Selector.all.values.find { |s| s.match?(locator) }
|
|
|
|
end
|
|
|
|
selector || Selector.all[session_options.default_selector]
|
|
|
|
end
|
|
|
|
|
2016-01-30 00:31:35 +00:00
|
|
|
def valid_keys
|
2016-09-01 22:19:48 +00:00
|
|
|
VALID_KEYS + custom_keys
|
2015-01-26 22:50:59 +00:00
|
|
|
end
|
|
|
|
|
2018-01-12 00:45:50 +00:00
|
|
|
def matches_node_filters?(node)
|
2018-05-24 22:27:34 +00:00
|
|
|
unapplied_options = options.keys - valid_keys
|
|
|
|
|
|
|
|
node_filters.all? do |filter_name, filter|
|
2018-05-26 17:26:44 +00:00
|
|
|
if filter.matcher?
|
|
|
|
unapplied_options.select { |option_name| filter.handles_option?(option_name) }.all? do |option_name|
|
2018-05-24 22:27:34 +00:00
|
|
|
unapplied_options.delete(option_name)
|
|
|
|
filter.matches?(node, option_name, options[option_name])
|
|
|
|
end
|
|
|
|
elsif options.key?(filter_name)
|
|
|
|
unapplied_options.delete(filter_name)
|
|
|
|
filter.matches?(node, filter_name, options[filter_name])
|
2018-01-12 00:45:50 +00:00
|
|
|
elsif filter.default?
|
2018-05-24 22:27:34 +00:00
|
|
|
filter.matches?(node, filter_name, filter.default)
|
2018-01-12 00:45:50 +00:00
|
|
|
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 22:16:00 +00:00
|
|
|
def node_filters
|
2018-01-09 22:05:50 +00:00
|
|
|
if options.key?(:filter_set)
|
2016-10-05 22:16:00 +00:00
|
|
|
::Capybara::Selector::FilterSet.all[options[:filter_set]].node_filters
|
2015-01-26 22:50:59 +00:00
|
|
|
else
|
2016-10-05 22:16:00 +00:00
|
|
|
@selector.node_filters
|
2015-01-26 22:50:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 22:16:00 +00:00
|
|
|
def expression_filters
|
|
|
|
filters = @selector.expression_filters
|
2018-01-09 22:05:50 +00:00
|
|
|
filters.merge ::Capybara::Selector::FilterSet.all[options[:filter_set]].expression_filters if options.key?(:filter_set)
|
2016-10-05 22:16:00 +00:00
|
|
|
filters
|
|
|
|
end
|
|
|
|
|
2015-01-26 22:50:59 +00:00
|
|
|
def custom_keys
|
2016-10-05 22:16:00 +00:00
|
|
|
@custom_keys ||= node_filters.keys + expression_filters.keys
|
2016-01-30 00:31:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_valid_keys
|
2018-05-26 17:26:44 +00:00
|
|
|
unless VALID_MATCH.include?(match)
|
|
|
|
raise ArgumentError, "invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(', ')}"
|
|
|
|
end
|
|
|
|
unhandled_options = @options.keys - valid_keys
|
|
|
|
unhandled_options -= @options.keys.select do |option_name|
|
|
|
|
expression_filters.any? { |_nmae, ef| ef.handles_option? option_name } ||
|
|
|
|
node_filters.any? { |_name, nf| nf.handles_option? option_name }
|
|
|
|
end
|
|
|
|
|
|
|
|
return if unhandled_options.empty?
|
|
|
|
invalid_names = unhandled_options.map(&:inspect).join(", ")
|
|
|
|
valid_names = valid_keys.map(&:inspect).join(", ")
|
|
|
|
raise ArgumentError, "invalid keys #{invalid_names}, should be one of #{valid_names}"
|
2016-01-30 00:31:35 +00:00
|
|
|
end
|
2016-08-24 21:07:05 +00:00
|
|
|
|
2016-09-22 23:55:54 +00:00
|
|
|
def filtered_xpath(expr)
|
2018-05-26 17:26:44 +00:00
|
|
|
if options.key?(:id) && !custom_keys.include?(:id)
|
|
|
|
expr = if options[:id].is_a? XPath::Expression
|
|
|
|
"(#{expr})[#{XPath.attr(:id)[options[:id]]}]"
|
|
|
|
else
|
|
|
|
"(#{expr})[#{XPath.attr(:id) == options[:id]}]"
|
|
|
|
end
|
|
|
|
end
|
2018-05-16 19:47:08 +00:00
|
|
|
if options.key?(:class) && !custom_keys.include?(:class)
|
2018-05-26 17:26:44 +00:00
|
|
|
class_xpath = if options[:class].is_a?(XPath::Expression)
|
|
|
|
XPath.attr(:class)[options[:class]]
|
|
|
|
else
|
|
|
|
Array(options[:class]).map do |klass|
|
2018-05-29 23:46:04 +00:00
|
|
|
if klass.start_with?('!')
|
|
|
|
!XPath.attr(:class).contains_word(klass.slice(1))
|
|
|
|
else
|
|
|
|
XPath.attr(:class).contains_word(klass)
|
|
|
|
end
|
2018-05-26 17:26:44 +00:00
|
|
|
end.reduce(:&)
|
|
|
|
end
|
2018-05-16 19:47:08 +00:00
|
|
|
expr = "(#{expr})[#{class_xpath}]"
|
2016-09-22 23:55:54 +00:00
|
|
|
end
|
|
|
|
expr
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_css(expr)
|
2018-05-26 17:26:44 +00:00
|
|
|
process_id = options.key?(:id) && !custom_keys.include?(:id)
|
|
|
|
process_class = options.key?(:class) && !custom_keys.include?(:class)
|
|
|
|
|
|
|
|
if process_id && options[:id].is_a?(XPath::Expression)
|
|
|
|
raise ArgumentError, "XPath expressions are not supported for the :id filter with CSS based selectors"
|
|
|
|
end
|
|
|
|
if process_class && options[:class].is_a?(XPath::Expression)
|
|
|
|
raise ArgumentError, "XPath expressions are not supported for the :class filter with CSS based selectors"
|
|
|
|
end
|
|
|
|
|
2018-06-04 21:56:05 +00:00
|
|
|
if process_id || process_class
|
|
|
|
expr = ::Capybara::Selector::CSS.split(expr).map do |sel|
|
|
|
|
sel += "##{::Capybara::Selector::CSS.escape(options[:id])}" if process_id
|
2018-06-04 15:17:30 +00:00
|
|
|
sel += css_from_classes(Array(options[:class])) if process_class
|
|
|
|
sel
|
|
|
|
end.join(", ")
|
|
|
|
end
|
2018-06-04 21:56:05 +00:00
|
|
|
|
2016-09-22 23:55:54 +00:00
|
|
|
expr
|
|
|
|
end
|
|
|
|
|
2018-05-29 23:46:04 +00:00
|
|
|
def css_from_classes(classes)
|
|
|
|
classes = classes.group_by { |c| c.start_with? '!' }
|
|
|
|
(classes[false].to_a.map { |c| ".#{Capybara::Selector::CSS.escape(c)}" } +
|
|
|
|
classes[true].to_a.map { |c| ":not(.#{Capybara::Selector::CSS.escape(c.slice(1))})" }).join
|
|
|
|
end
|
|
|
|
|
2016-10-05 22:16:00 +00:00
|
|
|
def apply_expression_filters(expr)
|
2018-05-24 22:27:34 +00:00
|
|
|
unapplied_options = options.keys - valid_keys
|
2016-10-05 22:16:00 +00:00
|
|
|
expression_filters.inject(expr) do |memo, (name, ef)|
|
2018-05-26 17:26:44 +00:00
|
|
|
if ef.matcher?
|
|
|
|
unapplied_options.select { |option_name| ef.handles_option?(option_name) }.each do |option_name|
|
2018-05-24 22:27:34 +00:00
|
|
|
unapplied_options.delete(option_name)
|
|
|
|
memo = ef.apply_filter(memo, option_name, options[option_name])
|
|
|
|
end
|
|
|
|
memo
|
|
|
|
elsif options.key?(name)
|
|
|
|
unapplied_options.delete(name)
|
|
|
|
ef.apply_filter(memo, name, options[name])
|
2016-10-05 22:16:00 +00:00
|
|
|
elsif ef.default?
|
2018-05-24 22:27:34 +00:00
|
|
|
ef.apply_filter(memo, name, ef.default)
|
2016-10-05 22:16:00 +00:00
|
|
|
else
|
|
|
|
memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-25 17:07:45 +00:00
|
|
|
def warn_exact_usage
|
2018-01-09 22:05:50 +00: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 21:07:05 +00:00
|
|
|
end
|
2016-12-23 20:17:45 +00:00
|
|
|
|
|
|
|
def exact_text
|
2016-12-15 17:04:01 +00:00
|
|
|
options.fetch(:exact_text, session_options.exact_text)
|
2016-12-23 20:17:45 +00:00
|
|
|
end
|
2017-09-21 00:08:02 +00:00
|
|
|
|
|
|
|
def describe_within?
|
2018-05-16 19:47:08 +00:00
|
|
|
@resolved_node && !document?(@resolved_node) && !simple_root?(@resolved_node)
|
2017-09-21 00:08:02 +00:00
|
|
|
end
|
2017-11-13 21:04:47 +00:00
|
|
|
|
2018-05-16 19:47:08 +00:00
|
|
|
def document?(node)
|
|
|
|
node.is_a?(::Capybara::Node::Document)
|
|
|
|
end
|
|
|
|
|
|
|
|
def simple_root?(node)
|
|
|
|
node.is_a?(::Capybara::Node::Simple) && node.path == '/'
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches_text_filter(node, value)
|
|
|
|
return matches_exact_text_filter(node, value) if exact_text == true
|
|
|
|
regexp = value.is_a?(Regexp) ? value : Regexp.escape(value.to_s)
|
|
|
|
matches_text_regexp(node, regexp)
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches_exact_text_filter(node, value)
|
|
|
|
regexp = value.is_a?(Regexp) ? value : /\A#{Regexp.escape(value.to_s)}\z/
|
|
|
|
matches_text_regexp(node, regexp)
|
2017-11-13 21:04:47 +00:00
|
|
|
end
|
|
|
|
|
2018-05-16 19:47:08 +00:00
|
|
|
def matches_text_regexp(node, regexp)
|
2017-11-13 21:04:47 +00:00
|
|
|
text_visible = visible
|
|
|
|
text_visible = :all if text_visible == :hidden
|
|
|
|
node.text(text_visible).match(regexp)
|
|
|
|
end
|
2016-01-30 00:31:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|