1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/queries/selector_query.rb

244 lines
8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-01-08 15:23:54 -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-08-17 19:14:39 -04:00
def initialize(*args, session_options:, **options, &filter_block)
@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
@filter_block = filter_block
2018-01-08 15:23:54 -05:00
@selector = if args[0].is_a?(Symbol)
Selector.all.fetch(args.shift) do |selector_type|
raise ArgumentError, "Unknown selector type (:#{selector_type})"
2016-09-26 14:20:15 -04:00
end
else
2018-01-08 15:23:54 -05:00
Selector.all.values.find { |s| s.match?(args[0]) }
end
2018-01-08 15:23:54 -05:00
@locator = args.shift
@selector ||= Selector.all[session_options.default_selector]
warn "Unused parameters passed to #{self.class.name} : #{args}" unless args.empty?
@expression = @selector.call(@locator, @options.merge(enable_aria_label: session_options.enable_aria_label))
2016-08-25 13:07:45 -04:00
warn_exact_usage
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
@description << "visible " if visible == :visible
@description << "non-visible " if visible == :hidden
@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)
@description << " with id #{options[:id]}" if options[:id]
@description << " with classes [#{Array(options[:class]).join(',')}]" if options[:class]
@description << selector.description(options)
@description << " that also matches the custom filter block" if @filter_block
@description << " within #{@resolved_node.inspect}" if describe_within?
@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)
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?
end
res = node_filters.all? do |name, filter|
2018-01-09 17:05:50 -05:00
if options.key?(name)
filter.matches?(node, options[name])
elsif filter.default?
filter.matches?(node, filter.default)
else
true
end
end
2018-01-09 17:05:50 -05:00
if @filter_block
res &&= if node.respond_to?(:session)
node.session.using_wait_time(0) { @filter_block.call(node) }
else
@filter_block.call(node)
end
end
res
rescue *(node.respond_to?(:session) ? node.session.driver.invalid_element_errors : [])
return false
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
end
end
def exact?
2018-01-09 17:05:50 -05:00
return false unless supports_exact?
options.fetch(:exact, session_options.exact)
end
def match
options.fetch(:match, session_options.match)
end
2018-01-09 17:05:50 -05:00
def xpath(exact = nil)
exact = exact? if exact.nil?
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)
filtered_xpath(expr)
end
def css
filtered_css(apply_expression_filters(@expression))
end
# @api private
def resolve_for(node, exact = nil)
@resolved_node = node
node.synchronize do
children = if selector.format == :css
2018-01-09 17:05:50 -05:00
node.find_css(css)
else
2018-01-09 17:05:50 -05:00
node.find_xpath(xpath(exact))
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
# @api private
def supports_exact?
@expression.respond_to? :to_xpath
end
2018-01-09 17:05:50 -05:00
private
def valid_keys
2016-09-01 18:19:48 -04:00
VALID_KEYS + custom_keys
2015-01-26 17:50:59 -05:00
end
def node_filters
2018-01-09 17:05:50 -05:00
if options.key?(:filter_set)
::Capybara::Selector::FilterSet.all[options[:filter_set]].node_filters
2015-01-26 17:50:59 -05:00
else
@selector.node_filters
2015-01-26 17:50:59 -05:00
end
end
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)
filters
end
2015-01-26 17:50:59 -05:00
def custom_keys
@custom_keys ||= node_filters.keys + expression_filters.keys
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
def filtered_xpath(expr)
2018-01-09 17:05:50 -05:00
if options.key?(:id) || options.key?(:class)
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)
class_xpath = Array(options[:class]).map do |klass|
XPath.attr(:class).contains_word(klass)
end.reduce(:&)
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)
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
end.join(", ")
end
expr
end
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)
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."
end
2016-12-23 15:17:45 -05:00
def exact_text
options.fetch(:exact_text, session_options.exact_text)
2016-12-23 15:17:45 -05:00
end
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
end
end
end