Move normalization of options into selector

This commit is contained in:
Jonas Nicklas 2012-01-02 17:49:54 +01:00
parent 5def28a7b5
commit f07b43b852
2 changed files with 27 additions and 26 deletions

View File

@ -109,8 +109,6 @@ module Capybara
# @return [Array[Capybara::Element]] The found elements
#
def all(*args)
args, options = extract_normalized_options(args)
selector = Capybara::Selector.normalize(*args)
selector.xpaths.
map { |path| find_in_base(selector, path) }.flatten.
@ -140,12 +138,8 @@ module Capybara
protected
def raise_find_error(*args)
args, options = extract_normalized_options(args)
normalized = Capybara::Selector.normalize(*args)
message = options[:message] || "Unable to find #{normalized.name} #{normalized.locator.inspect}"
message = normalized.failure_message.call(self, normalized) if normalized.failure_message
raise Capybara::ElementNotFound, message
raise Capybara::ElementNotFound, normalized.failure_message(self)
end
def find_in_base(selector, xpath)
@ -154,23 +148,6 @@ module Capybara
end
end
def extract_normalized_options(args)
options = if args.last.is_a?(Hash) then args.pop.dup else {} end
if text = options[:text]
options[:text] = Regexp.escape(text) unless text.kind_of?(Regexp)
end
if !options.has_key?(:visible)
options[:visible] = Capybara.ignore_hidden_elements
end
if selected = options[:selected]
options[:selected] = [selected].flatten
end
[args.push(options), options]
end
end
end

View File

@ -5,7 +5,13 @@ module Capybara
class Normalized
attr_accessor :selector, :locator, :options, :xpaths
def failure_message; selector.failure_message; end
def failure_message(node)
message = selector.failure_message.call(node, self) if selector.failure_message
message ||= options[:message]
message ||= "Unable to find #{name} #{locator.inspect}"
message
end
def name; selector.name; end
def matches_filters?(node)
@ -31,9 +37,27 @@ module Capybara
all.delete(name.to_sym)
end
def extract_normalized_options(args)
options = if args.last.is_a?(Hash) then args.pop.dup else {} end
if text = options[:text]
options[:text] = Regexp.escape(text) unless text.kind_of?(Regexp)
end
if !options.has_key?(:visible)
options[:visible] = Capybara.ignore_hidden_elements
end
if selected = options[:selected]
options[:selected] = [selected].flatten
end
options
end
def normalize(*args)
normalized = Normalized.new
normalized.options = if args.last.is_a?(Hash) then args.pop else {} end
normalized.options = extract_normalized_options(args)
if args[1]
normalized.selector = all[args[0]]