teamcapybara--capybara/lib/capybara/query.rb

126 lines
3.4 KiB
Ruby
Raw Normal View History

2012-01-02 17:11:07 +00:00
module Capybara
class Query
attr_accessor :selector, :locator, :options, :expression, :find, :negative
2012-01-02 17:11:07 +00:00
VALID_KEYS = [:text, :visible, :between, :count, :maximum, :minimum, :exact, :match, :wait]
VALID_MATCH = [:first, :smart, :prefer_exact, :one]
2012-07-13 13:36:43 +00:00
def initialize(*args)
2012-01-02 17:11:07 +00:00
@options = if args.last.is_a?(Hash) then args.pop.dup else {} end
2012-07-13 13:36:43 +00:00
if args[0].is_a?(Symbol)
2012-01-02 17:11:07 +00: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]
# for compatibility with Capybara 2.0
if Capybara.exact_options and @selector == Selector.all[:option]
@options[:exact] = true
end
@expression = @selector.call(@locator)
2012-07-13 13:36:43 +00:00
assert_valid_keys!
2012-01-02 17:11:07 +00:00
end
def name; selector.name; end
def label; selector.label or selector.name; end
2012-01-02 17:11:07 +00:00
def description
@description = "#{label} #{locator.inspect}"
@description << " with text #{options[:text].inspect}" if options[:text]
2013-09-25 13:07:02 +00:00
@description << " with value #{options[:with].inspect}" if options[:with]
@description
end
2012-01-02 17:11:07 +00:00
def matches_filters?(node)
if options[:text]
regexp = options[:text].is_a?(Regexp) ? options[:text] : Regexp.escape(options[:text].to_s)
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
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)
end
end
2012-01-02 17:11:07 +00:00
end
2012-01-31 14:55:26 +00:00
def visible
if options.has_key?(:visible)
case @options[:visible]
when true then :visible
when false then :all
else @options[:visible]
end
else
if Capybara.ignore_hidden_elements
:visible
else
:all
end
end
end
def wait
if options.has_key?(:wait)
@options[:wait] or 0
else
Capybara.default_wait_time
end
end
def exact?
if options.has_key?(:exact)
@options[:exact]
else
Capybara.exact
end
2013-02-15 19:32:05 +00:00
end
def match
if options.has_key?(:match)
@options[:match]
else
Capybara.match
end
2013-02-15 19:32:05 +00:00
end
def xpath(exact=nil)
exact = self.exact? if exact == nil
if @expression.respond_to?(:to_xpath) and exact
@expression.to_xpath(:exact)
2013-02-15 19:32:05 +00:00
else
@expression.to_s
2013-02-15 19:32:05 +00:00
end
end
2013-02-19 00:01:52 +00:00
def css
@expression
2013-02-19 00:01:52 +00:00
end
2013-02-15 19:32:05 +00:00
2012-07-13 13:36:43 +00: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
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 13:36:43 +00:00
end
2012-01-02 17:11:07 +00:00
end
end