2012-01-02 12:11:07 -05:00
|
|
|
module Capybara
|
|
|
|
class Query
|
2012-06-08 10:47:01 -04:00
|
|
|
attr_accessor :selector, :locator, :options, :xpath, :find, :negative
|
2012-01-02 12:11:07 -05:00
|
|
|
|
2013-02-15 14:32:05 -05:00
|
|
|
VALID_KEYS = [:text, :visible, :between, :count, :maximum, :minimum, :exact, :match]
|
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-15 13:59:51 -05:00
|
|
|
@xpath = @selector.call(@locator)
|
2012-07-13 09:36:43 -04: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]
|
|
|
|
@description
|
|
|
|
end
|
|
|
|
|
2012-01-02 12:11:07 -05:00
|
|
|
def matches_filters?(node)
|
2012-10-30 06:08:44 -04:00
|
|
|
node.unsynchronized do
|
|
|
|
if options[:text]
|
|
|
|
regexp = options[:text].is_a?(Regexp) ? options[:text] : Regexp.escape(options[:text])
|
2013-02-17 10:09:14 -05:00
|
|
|
type = if @options.has_key?(:visible) and not @options[:visible] then :all else nil end
|
|
|
|
return false if not node.text(type).match(regexp)
|
2012-10-30 06:08:44 -04:00
|
|
|
end
|
2013-02-17 10:09:14 -05:00
|
|
|
return false if visible? and not node.visible?
|
2012-10-30 06:08:44 -04:00
|
|
|
selector.custom_filters.each do |name, block|
|
|
|
|
return false if options.has_key?(name) and not block.call(node, options[name])
|
|
|
|
end
|
|
|
|
true
|
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
|
|
|
|
2012-06-08 10:47:01 -04:00
|
|
|
def matches_count?(count)
|
2012-01-31 09:55:26 -05:00
|
|
|
case
|
|
|
|
when options[:between]
|
2012-06-08 10:47:01 -04:00
|
|
|
options[:between] === count
|
2012-01-31 09:55:26 -05:00
|
|
|
when options[:count]
|
2012-06-08 10:47:01 -04:00
|
|
|
options[:count].to_i == count
|
2012-01-31 09:55:26 -05:00
|
|
|
when options[:maximum]
|
2012-06-08 10:47:01 -04:00
|
|
|
options[:maximum].to_i >= count
|
2012-01-31 09:55:26 -05:00
|
|
|
when options[:minimum]
|
2012-06-08 10:47:01 -04:00
|
|
|
options[:minimum].to_i <= count
|
2012-01-31 09:55:26 -05:00
|
|
|
else
|
2012-06-08 10:47:01 -04:00
|
|
|
count > 0
|
2012-01-31 09:55:26 -05:00
|
|
|
end
|
|
|
|
end
|
2012-07-13 09:36:43 -04:00
|
|
|
|
2013-02-17 10:09:14 -05:00
|
|
|
def visible?
|
|
|
|
if options.has_key?(:visible)
|
|
|
|
@options[:visible]
|
|
|
|
else
|
|
|
|
Capybara.ignore_hidden_elements
|
|
|
|
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-15 14:32:05 -05:00
|
|
|
|
|
|
|
if @xpath.respond_to?(:to_xpath) and exact
|
|
|
|
@xpath.to_xpath(:exact)
|
|
|
|
else
|
|
|
|
@xpath.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-13 09:36:43 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def assert_valid_keys!
|
|
|
|
valid_keys = VALID_KEYS + @selector.custom_filters.keys
|
|
|
|
invalid_keys = @options.keys - valid_keys
|
|
|
|
unless invalid_keys.empty?
|
|
|
|
invalid_names = invalid_keys.map(&:inspect).join(", ")
|
|
|
|
valid_names = valid_keys.map(&:inspect).join(", ")
|
|
|
|
raise ArgumentError, "invalid keys #{invalid_names}, should be one of #{valid_names}"
|
|
|
|
end
|
|
|
|
end
|
2012-01-02 12:11:07 -05:00
|
|
|
end
|
|
|
|
end
|