mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
e39c026abe
With this change, any call to `find`, `all`, etc where the first argument is a Symbol, will assume that that Symbol is the name of the selector. This makes the `match` option on the `id` selector impossible, so `find(:foo)` will no longer find elements by id. This change is in preparation for allowing selectors without locators, as discussed in #783, which we will probably hold off with until Capybara 2.1, because it is too much work. This commit ensures we won't have to break the API to make implementing #783 possible.
78 lines
2.2 KiB
Ruby
78 lines
2.2 KiB
Ruby
module Capybara
|
|
class Query
|
|
attr_accessor :selector, :locator, :options, :xpath, :find, :negative
|
|
|
|
VALID_KEYS = [:text, :visible, :between, :count, :maximum, :minimum]
|
|
|
|
def initialize(*args)
|
|
@options = if args.last.is_a?(Hash) then args.pop.dup else {} end
|
|
|
|
unless options.has_key?(:visible)
|
|
@options[:visible] = Capybara.ignore_hidden_elements
|
|
end
|
|
|
|
if args[0].is_a?(Symbol)
|
|
@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]
|
|
|
|
@xpath = @selector.call(@locator).to_s
|
|
|
|
assert_valid_keys!
|
|
end
|
|
|
|
def name; selector.name; end
|
|
def label; selector.label or selector.name; end
|
|
|
|
def description
|
|
@description = "#{label} #{locator.inspect}"
|
|
@description << " with text #{options[:text].inspect}" if options[:text]
|
|
@description
|
|
end
|
|
|
|
def matches_filters?(node)
|
|
if options[:text]
|
|
regexp = options[:text].is_a?(Regexp) ? options[:text] : Regexp.escape(options[:text])
|
|
return false if not node.text.match(regexp)
|
|
end
|
|
return false if options[:visible] and not node.visible?
|
|
selector.custom_filters.each do |name, block|
|
|
return false if options.has_key?(name) and not block.call(node, options[name])
|
|
end
|
|
true
|
|
end
|
|
|
|
def matches_count?(count)
|
|
case
|
|
when count.zero?
|
|
false
|
|
when options[:between]
|
|
options[:between] === count
|
|
when options[:count]
|
|
options[:count].to_i == count
|
|
when options[:maximum]
|
|
options[:maximum].to_i >= count
|
|
when options[:minimum]
|
|
options[:minimum].to_i <= count
|
|
else
|
|
count > 0
|
|
end
|
|
end
|
|
|
|
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
|
|
end
|
|
end
|