2012-01-02 12:11:07 -05:00
|
|
|
module Capybara
|
2014-02-16 12:13:58 -05:00
|
|
|
# @deprecated This class and its methods are not supposed to be used by users of Capybara's public API.
|
|
|
|
# It may be removed in future versions of Capybara.
|
|
|
|
class Query < Queries::BaseQuery
|
2013-02-19 13:56:49 -05:00
|
|
|
attr_accessor :selector, :locator, :options, :expression, :find, :negative
|
2012-01-02 12:11:07 -05:00
|
|
|
|
2013-02-25 14:34:58 -05:00
|
|
|
VALID_KEYS = [:text, :visible, :between, :count, :maximum, :minimum, :exact, :match, :wait]
|
2013-02-24 12:04:53 -05:00
|
|
|
VALID_MATCH = [:first, :smart, :prefer_exact, :one]
|
2012-07-13 09:36:43 -04:00
|
|
|
|
2012-06-08 10:47:01 -04:00
|
|
|
def initialize(*args)
|
2012-01-02 12:11:07 -05:00
|
|
|
@options = if args.last.is_a?(Hash) then args.pop.dup else {} end
|
2012-07-13 09:36:43 -04:00
|
|
|
|
2012-09-09 11:32:03 -04:00
|
|
|
if args[0].is_a?(Symbol)
|
2012-01-02 12:11:07 -05:00
|
|
|
@selector = Selector.all[args[0]]
|
|
|
|
@locator = args[1]
|
|
|
|
else
|
|
|
|
@selector = Selector.all.values.find { |s| s.match?(args[0]) }
|
|
|
|
@locator = args[0]
|
|
|
|
end
|
|
|
|
@selector ||= Selector.all[Capybara.default_selector]
|
|
|
|
|
2013-02-16 04:20:50 -05:00
|
|
|
# for compatibility with Capybara 2.0
|
|
|
|
if Capybara.exact_options and @selector == Selector.all[:option]
|
|
|
|
@options[:exact] = true
|
|
|
|
end
|
|
|
|
|
2013-02-19 13:56:49 -05:00
|
|
|
@expression = @selector.call(@locator)
|
2014-02-16 12:13:58 -05:00
|
|
|
assert_valid_keys
|
2012-01-02 12:11:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def name; selector.name; end
|
2012-06-08 09:53:25 -04:00
|
|
|
def label; selector.label or selector.name; end
|
2012-01-02 12:11:07 -05:00
|
|
|
|
2012-01-02 12:31:50 -05:00
|
|
|
def description
|
2012-06-08 09:53:25 -04:00
|
|
|
@description = "#{label} #{locator.inspect}"
|
2012-01-02 12:31:50 -05:00
|
|
|
@description << " with text #{options[:text].inspect}" if options[:text]
|
2013-09-25 09:07:02 -04:00
|
|
|
@description << " with value #{options[:with].inspect}" if options[:with]
|
2012-01-02 12:31:50 -05:00
|
|
|
@description
|
|
|
|
end
|
|
|
|
|
2012-01-02 12:11:07 -05:00
|
|
|
def matches_filters?(node)
|
2013-03-11 19:11:05 -04:00
|
|
|
if options[:text]
|
2013-03-26 11:52:26 -04:00
|
|
|
regexp = options[:text].is_a?(Regexp) ? options[:text] : Regexp.escape(options[:text].to_s)
|
2013-03-11 19:11:05 -04:00
|
|
|
return false if not node.text(visible).match(regexp)
|
|
|
|
end
|
|
|
|
case visible
|
|
|
|
when :visible then return false unless node.visible?
|
|
|
|
when :hidden then return false if node.visible?
|
|
|
|
end
|
2013-03-15 21:05:36 -04:00
|
|
|
selector.custom_filters.each do |name, filter|
|
|
|
|
if options.has_key?(name)
|
|
|
|
return false unless filter.matches?(node, options[name])
|
|
|
|
elsif filter.default?
|
|
|
|
return false unless filter.matches?(node, filter.default)
|
2012-10-30 06:08:44 -04:00
|
|
|
end
|
2012-01-03 18:19:43 -05:00
|
|
|
end
|
2012-01-02 12:11:07 -05:00
|
|
|
end
|
2012-01-31 09:55:26 -05:00
|
|
|
|
2013-02-24 12:43:01 -05:00
|
|
|
def visible
|
2013-02-17 10:09:14 -05:00
|
|
|
if options.has_key?(:visible)
|
2013-02-24 12:43:01 -05:00
|
|
|
case @options[:visible]
|
|
|
|
when true then :visible
|
|
|
|
when false then :all
|
|
|
|
else @options[:visible]
|
|
|
|
end
|
2013-02-17 10:09:14 -05:00
|
|
|
else
|
2013-02-24 12:43:01 -05:00
|
|
|
if Capybara.ignore_hidden_elements
|
|
|
|
:visible
|
|
|
|
else
|
|
|
|
:all
|
|
|
|
end
|
2013-02-17 10:09:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def exact?
|
|
|
|
if options.has_key?(:exact)
|
|
|
|
@options[:exact]
|
|
|
|
else
|
|
|
|
Capybara.exact
|
|
|
|
end
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def match
|
2013-02-17 10:09:14 -05:00
|
|
|
if options.has_key?(:match)
|
|
|
|
@options[:match]
|
|
|
|
else
|
|
|
|
Capybara.match
|
|
|
|
end
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def xpath(exact=nil)
|
2013-02-17 10:09:14 -05:00
|
|
|
exact = self.exact? if exact == nil
|
2013-02-19 13:56:49 -05:00
|
|
|
if @expression.respond_to?(:to_xpath) and exact
|
|
|
|
@expression.to_xpath(:exact)
|
2013-02-15 14:32:05 -05:00
|
|
|
else
|
2013-02-19 13:56:49 -05:00
|
|
|
@expression.to_s
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-18 19:01:52 -05:00
|
|
|
def css
|
2013-02-19 13:56:49 -05:00
|
|
|
@expression
|
2013-02-18 19:01:52 -05:00
|
|
|
end
|
2013-02-15 14:32:05 -05:00
|
|
|
|
2014-02-16 12:13:58 -05:00
|
|
|
# @api private
|
|
|
|
def resolve_for(node, exact = nil)
|
|
|
|
node.synchronize do
|
|
|
|
children = if selector.format == :css
|
|
|
|
node.find_css(self.css)
|
|
|
|
else
|
|
|
|
node.find_xpath(self.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)
|
2012-07-13 09:36:43 -04:00
|
|
|
end
|
2014-02-16 12:13:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def valid_keys
|
|
|
|
COUNT_KEYS + [:text, :visible, :exact, :match, :wait] + @selector.custom_filters.keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_valid_keys
|
|
|
|
super
|
2013-02-24 12:04:53 -05:00
|
|
|
unless VALID_MATCH.include?(match)
|
|
|
|
raise ArgumentError, "invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(", ")}"
|
|
|
|
end
|
2012-07-13 09:36:43 -04:00
|
|
|
end
|
2012-01-02 12:11:07 -05:00
|
|
|
end
|
|
|
|
end
|