2010-08-27 18:37:39 +00:00
|
|
|
module Capybara
|
|
|
|
class Selector
|
2010-10-22 11:29:08 +00:00
|
|
|
attr_reader :name
|
2010-08-27 18:37:39 +00:00
|
|
|
|
2011-02-10 15:59:26 +00:00
|
|
|
class Normalized
|
|
|
|
attr_accessor :selector, :locator, :options, :xpaths
|
|
|
|
|
|
|
|
def failure_message; selector.failure_message; end
|
|
|
|
def name; selector.name; end
|
|
|
|
end
|
|
|
|
|
2010-08-27 18:37:39 +00:00
|
|
|
class << self
|
|
|
|
def all
|
|
|
|
@selectors ||= {}
|
|
|
|
end
|
|
|
|
|
2010-10-22 11:29:08 +00:00
|
|
|
def add(name, &block)
|
|
|
|
all[name.to_sym] = Capybara::Selector.new(name.to_sym, &block)
|
2010-08-27 18:37:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove(name)
|
|
|
|
all.delete(name.to_sym)
|
|
|
|
end
|
|
|
|
|
2011-02-06 19:07:12 +00:00
|
|
|
def normalize(*args)
|
2011-02-10 15:59:26 +00:00
|
|
|
normalized = Normalized.new
|
|
|
|
normalized.options = if args.last.is_a?(Hash) then args.pop else {} end
|
2011-02-04 15:16:46 +00:00
|
|
|
|
2011-02-06 19:07:12 +00:00
|
|
|
if args[1]
|
2011-02-10 15:59:26 +00:00
|
|
|
normalized.selector = all[args[0]]
|
|
|
|
normalized.locator = args[1]
|
2010-08-27 18:37:39 +00:00
|
|
|
else
|
2011-02-10 15:59:26 +00:00
|
|
|
normalized.selector = all.values.find { |s| s.match?(args[0]) }
|
|
|
|
normalized.locator = args[0]
|
2010-08-27 18:37:39 +00:00
|
|
|
end
|
2011-02-10 15:59:26 +00:00
|
|
|
normalized.selector ||= all[Capybara.default_selector]
|
2011-02-04 15:16:46 +00:00
|
|
|
|
2011-02-10 15:59:26 +00:00
|
|
|
xpath = normalized.selector.call(normalized.locator)
|
2010-10-06 19:07:26 +00:00
|
|
|
if xpath.respond_to?(:to_xpaths)
|
2011-02-10 15:59:26 +00:00
|
|
|
normalized.xpaths = xpath.to_xpaths
|
2010-10-06 19:07:26 +00:00
|
|
|
else
|
2011-02-10 15:59:26 +00:00
|
|
|
normalized.xpaths = [xpath.to_s].flatten
|
2010-10-06 19:07:26 +00:00
|
|
|
end
|
2011-02-10 15:59:26 +00:00
|
|
|
normalized
|
2010-08-27 18:37:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-22 11:29:08 +00:00
|
|
|
def initialize(name, &block)
|
2010-08-27 18:37:39 +00:00
|
|
|
@name = name
|
2010-10-22 11:29:08 +00:00
|
|
|
instance_eval(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def xpath(&block)
|
|
|
|
@xpath = block if block
|
|
|
|
@xpath
|
|
|
|
end
|
|
|
|
|
2011-09-29 20:04:51 +00:00
|
|
|
# Same as xpath, but wrap in XPath.css().
|
|
|
|
def css(&block)
|
|
|
|
if block
|
|
|
|
@xpath = xpath { |*args| XPath.css(block.call(*args)) }
|
|
|
|
end
|
|
|
|
@xpath
|
|
|
|
end
|
|
|
|
|
2010-10-22 11:29:08 +00:00
|
|
|
def match(&block)
|
|
|
|
@match = block if block
|
|
|
|
@match
|
2010-08-27 18:37:39 +00:00
|
|
|
end
|
|
|
|
|
2011-02-04 15:16:46 +00:00
|
|
|
def failure_message(&block)
|
|
|
|
@failure_message = block if block
|
|
|
|
@failure_message
|
|
|
|
end
|
|
|
|
|
2010-08-27 18:37:39 +00:00
|
|
|
def call(locator)
|
2010-10-22 11:29:08 +00:00
|
|
|
@xpath.call(locator)
|
2010-08-27 18:37:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def match?(locator)
|
2010-10-22 11:29:08 +00:00
|
|
|
@match and @match.call(locator)
|
2010-08-27 18:37:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-22 11:29:08 +00:00
|
|
|
Capybara.add_selector(:xpath) do
|
|
|
|
xpath { |xpath| xpath }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:css) do
|
2011-09-29 20:17:32 +00:00
|
|
|
css { |css| css }
|
2010-10-22 11:29:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:id) do
|
|
|
|
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
|
|
|
|
match { |value| value.is_a?(Symbol) }
|
|
|
|
end
|