2012-01-02 17:11:07 +00:00
|
|
|
module Capybara
|
|
|
|
class Query
|
2012-01-31 15:43:22 +00:00
|
|
|
attr_accessor :selector, :locator, :options, :xpath
|
2012-01-02 17:11:07 +00:00
|
|
|
|
|
|
|
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[1]
|
|
|
|
@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)
|
|
|
|
if xpath.respond_to?(:to_xpaths)
|
2012-01-31 15:43:22 +00:00
|
|
|
@xpath = xpath.to_xpath(:fuzzy)
|
2012-01-02 17:11:07 +00:00
|
|
|
else
|
2012-01-31 15:43:22 +00:00
|
|
|
@xpath = xpath.to_s
|
2012-01-02 17:11:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-02 17:31:50 +00:00
|
|
|
def failure_message(type, node)
|
2012-01-02 17:11:07 +00:00
|
|
|
message = selector.failure_message.call(node, self) if selector.failure_message
|
|
|
|
message ||= options[:message]
|
2012-01-02 17:31:50 +00:00
|
|
|
if type == :assert
|
|
|
|
message ||= "expected #{description} to return something"
|
|
|
|
else
|
|
|
|
message ||= "Unable to find #{description}"
|
|
|
|
end
|
2012-01-02 17:11:07 +00:00
|
|
|
message
|
|
|
|
end
|
|
|
|
|
2012-01-03 12:20:37 +00:00
|
|
|
def negative_failure_message(type, node)
|
|
|
|
"expected #{description} not to return anything"
|
|
|
|
end
|
|
|
|
|
2012-01-02 17:11:07 +00:00
|
|
|
def name; selector.name; end
|
|
|
|
|
2012-01-02 17:31:50 +00:00
|
|
|
def description
|
|
|
|
@description = "#{name} #{locator.inspect}"
|
|
|
|
@description << " with text #{options[:text].inspect}" if options[:text]
|
|
|
|
@description
|
|
|
|
end
|
|
|
|
|
2012-01-02 17:11:07 +00:00
|
|
|
def matches_filters?(node)
|
2012-01-03 23:19:43 +00:00
|
|
|
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?
|
2012-01-02 17:11:07 +00:00
|
|
|
selector.custom_filters.each do |name, block|
|
|
|
|
return false if options.has_key?(name) and not block.call(node, options[name])
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
2012-01-31 14:55:26 +00:00
|
|
|
|
|
|
|
def matches_count?(nodes)
|
|
|
|
case
|
|
|
|
when nodes.empty?
|
|
|
|
false
|
|
|
|
when options[:between]
|
|
|
|
options[:between] === nodes.size
|
|
|
|
when options[:count]
|
|
|
|
options[:count].to_i == nodes.size
|
|
|
|
when options[:maximum]
|
|
|
|
options[:maximum].to_i >= nodes.size
|
|
|
|
when options[:minimum]
|
|
|
|
options[:minimum].to_i <= nodes.size
|
|
|
|
else
|
|
|
|
nodes.size > 0
|
|
|
|
end
|
|
|
|
end
|
2012-01-02 17:11:07 +00:00
|
|
|
end
|
|
|
|
end
|