2010-08-27 14:37:39 -04:00
|
|
|
module Capybara
|
|
|
|
class Selector
|
2011-09-28 10:51:31 -04:00
|
|
|
PROPERTY_OPTION_KEYS = [:text, :visible, :with, :checked, :unchecked, :selected]
|
|
|
|
|
2010-10-22 07:29:08 -04:00
|
|
|
attr_reader :name
|
2010-08-27 14:37:39 -04:00
|
|
|
|
2011-02-10 10:59:26 -05:00
|
|
|
class Normalized
|
2011-09-28 10:51:31 -04:00
|
|
|
attr_accessor :selector, :locator, :options, :xpath_options, :property_options, :xpaths
|
2011-02-10 10:59:26 -05:00
|
|
|
|
|
|
|
def failure_message; selector.failure_message; end
|
|
|
|
def name; selector.name; end
|
2011-09-28 10:51:31 -04:00
|
|
|
|
|
|
|
def filter(node)
|
|
|
|
return false if property_options[:text] and not node.text.match(property_options[:text])
|
|
|
|
return false if property_options[:visible] and not node.visible?
|
|
|
|
return false if property_options[:with] and not node.value == property_options[:with]
|
|
|
|
return false if property_options[:checked] and not node.checked?
|
|
|
|
return false if property_options[:unchecked] and node.checked?
|
|
|
|
return false if property_options[:selected] and not has_selected_options?(node, property_options[:selected])
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def has_selected_options?(node, expected)
|
|
|
|
actual = node.all(:xpath, './/option').select { |option| option.selected? }.map { |option| option.text }
|
|
|
|
(expected - actual).empty?
|
|
|
|
end
|
2011-02-10 10:59:26 -05:00
|
|
|
end
|
|
|
|
|
2010-08-27 14:37:39 -04:00
|
|
|
class << self
|
|
|
|
def all
|
|
|
|
@selectors ||= {}
|
|
|
|
end
|
|
|
|
|
2010-10-22 07:29:08 -04:00
|
|
|
def add(name, &block)
|
|
|
|
all[name.to_sym] = Capybara::Selector.new(name.to_sym, &block)
|
2010-08-27 14:37:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove(name)
|
|
|
|
all.delete(name.to_sym)
|
|
|
|
end
|
|
|
|
|
2011-02-06 14:07:12 -05:00
|
|
|
def normalize(*args)
|
2011-02-10 10:59:26 -05:00
|
|
|
normalized = Normalized.new
|
|
|
|
normalized.options = if args.last.is_a?(Hash) then args.pop else {} end
|
2011-09-28 10:51:31 -04:00
|
|
|
normalized.xpath_options, normalized.property_options = split_options(normalized.options)
|
2011-02-04 10:16:46 -05:00
|
|
|
|
2011-02-06 14:07:12 -05:00
|
|
|
if args[1]
|
2011-02-10 10:59:26 -05:00
|
|
|
normalized.selector = all[args[0]]
|
|
|
|
normalized.locator = args[1]
|
2010-08-27 14:37:39 -04:00
|
|
|
else
|
2011-02-10 10:59:26 -05:00
|
|
|
normalized.selector = all.values.find { |s| s.match?(args[0]) }
|
|
|
|
normalized.locator = args[0]
|
2010-08-27 14:37:39 -04:00
|
|
|
end
|
2011-02-10 10:59:26 -05:00
|
|
|
normalized.selector ||= all[Capybara.default_selector]
|
2011-02-04 10:16:46 -05:00
|
|
|
|
2011-09-28 10:51:31 -04:00
|
|
|
xpath = normalized.selector.call(normalized.locator, normalized.xpath_options)
|
2010-10-06 15:07:26 -04:00
|
|
|
if xpath.respond_to?(:to_xpaths)
|
2011-02-10 10:59:26 -05:00
|
|
|
normalized.xpaths = xpath.to_xpaths
|
2010-10-06 15:07:26 -04:00
|
|
|
else
|
2011-02-10 10:59:26 -05:00
|
|
|
normalized.xpaths = [xpath.to_s].flatten
|
2010-10-06 15:07:26 -04:00
|
|
|
end
|
2011-02-10 10:59:26 -05:00
|
|
|
normalized
|
2010-08-27 14:37:39 -04:00
|
|
|
end
|
2011-09-28 10:51:31 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def split_options(options)
|
|
|
|
xpath_options = options.dup
|
|
|
|
property_options = PROPERTY_OPTION_KEYS.inject({}) do |opts, key|
|
|
|
|
opts[key] = xpath_options.delete(key) if xpath_options[key]
|
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
|
|
|
[ xpath_options, property_options ]
|
|
|
|
end
|
2010-08-27 14:37:39 -04:00
|
|
|
end
|
|
|
|
|
2010-10-22 07:29:08 -04:00
|
|
|
def initialize(name, &block)
|
2010-08-27 14:37:39 -04:00
|
|
|
@name = name
|
2010-10-22 07:29:08 -04:00
|
|
|
instance_eval(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def xpath(&block)
|
|
|
|
@xpath = block if block
|
|
|
|
@xpath
|
|
|
|
end
|
|
|
|
|
2011-09-29 16:04:51 -04: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 07:29:08 -04:00
|
|
|
def match(&block)
|
|
|
|
@match = block if block
|
|
|
|
@match
|
2010-08-27 14:37:39 -04:00
|
|
|
end
|
|
|
|
|
2011-02-04 10:16:46 -05:00
|
|
|
def failure_message(&block)
|
|
|
|
@failure_message = block if block
|
|
|
|
@failure_message
|
|
|
|
end
|
|
|
|
|
2011-09-28 10:51:31 -04:00
|
|
|
def call(locator, xpath_options={})
|
|
|
|
@xpath.call(locator, xpath_options)
|
2010-08-27 14:37:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def match?(locator)
|
2010-10-22 07:29:08 -04:00
|
|
|
@match and @match.call(locator)
|
2010-08-27 14:37:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-22 07:29:08 -04:00
|
|
|
Capybara.add_selector(:xpath) do
|
2011-09-05 12:14:10 -04:00
|
|
|
xpath { |xpath| xpath }
|
2010-10-22 07:29:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:css) do
|
2011-09-29 16:17:32 -04:00
|
|
|
css { |css| css }
|
2010-10-22 07:29:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:id) do
|
2011-09-05 12:14:10 -04:00
|
|
|
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
|
2010-10-22 07:29:08 -04:00
|
|
|
match { |value| value.is_a?(Symbol) }
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
|
|
|
|
Capybara.add_selector(:field) do
|
2011-09-05 12:14:10 -04:00
|
|
|
xpath { |locator| XPath::HTML.field(locator) }
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
|
|
|
|
2011-08-27 17:57:12 -04:00
|
|
|
Capybara.add_selector(:fieldset) do
|
2011-09-05 12:14:10 -04:00
|
|
|
xpath { |locator| XPath::HTML.fieldset(locator) }
|
2011-08-27 17:57:12 -04:00
|
|
|
end
|
|
|
|
|
2011-08-26 22:16:15 -04:00
|
|
|
Capybara.add_selector(:link_or_button) do
|
2011-09-05 12:14:10 -04:00
|
|
|
xpath { |locator| XPath::HTML.link_or_button(locator) }
|
2011-08-26 22:16:15 -04:00
|
|
|
failure_message { |node, selector| "no link or button '#{selector.locator}' found" }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:link) do
|
2011-09-28 10:51:31 -04:00
|
|
|
xpath { |locator, xpath_options| XPath::HTML.link(locator, xpath_options) }
|
2011-08-26 22:16:15 -04:00
|
|
|
failure_message { |node, selector| "no link with title, id or text '#{selector.locator}' found" }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:button) do
|
2011-09-05 12:14:10 -04:00
|
|
|
xpath { |locator| XPath::HTML.button(locator) }
|
2011-08-26 22:16:15 -04:00
|
|
|
failure_message { |node, selector| "no button with value or id or text '#{selector.locator}' found" }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:fillable_field) do
|
2011-09-28 10:51:31 -04:00
|
|
|
xpath { |locator, xpath_options| XPath::HTML.fillable_field(locator, xpath_options) }
|
2011-08-26 22:16:15 -04:00
|
|
|
failure_message { |node, selector| "no text field, text area or password field with id, name, or label '#{selector.locator}' found" }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:radio_button) do
|
2011-09-28 10:51:31 -04:00
|
|
|
xpath { |locator, xpath_options| XPath::HTML.radio_button(locator, xpath_options) }
|
2011-08-26 22:16:15 -04:00
|
|
|
failure_message { |node, selector| "no radio button with id, name, or label '#{selector.locator}' found" }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:checkbox) do
|
2011-09-28 10:51:31 -04:00
|
|
|
xpath { |locator, xpath_options| XPath::HTML.checkbox(locator, xpath_options) }
|
2011-08-26 22:16:15 -04:00
|
|
|
failure_message { |node, selector| "no checkbox with id, name, or label '#{selector.locator}' found" }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:select) do
|
2011-09-28 10:51:31 -04:00
|
|
|
xpath { |locator, xpath_options| XPath::HTML.select(locator, xpath_options) }
|
2011-08-26 22:16:15 -04:00
|
|
|
failure_message { |node, selector| "no select box with id, name, or label '#{selector.locator}' found" }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:option) do
|
2011-09-05 12:14:10 -04:00
|
|
|
xpath { |locator| XPath::HTML.option(locator) }
|
2011-08-26 22:16:15 -04:00
|
|
|
failure_message do |node, selector|
|
|
|
|
"no option with text '#{selector.locator}'".tap do |message|
|
|
|
|
message << " in the select box" if node.tag_name == 'select'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:file_field) do
|
2011-09-28 10:51:31 -04:00
|
|
|
xpath { |locator, xpath_options| XPath::HTML.file_field(locator, xpath_options) }
|
2011-08-26 22:16:15 -04:00
|
|
|
failure_message { |node, selector| "no file field with id, name, or label '#{selector.locator}' found" }
|
|
|
|
end
|
2011-08-27 17:54:55 -04:00
|
|
|
|
|
|
|
Capybara.add_selector(:content) do
|
2011-09-05 12:14:10 -04:00
|
|
|
xpath { |content| XPath::HTML.content(content) }
|
2011-08-27 17:54:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:table) do
|
2011-09-28 10:51:31 -04:00
|
|
|
xpath { |locator, xpath_options| XPath::HTML.table(locator, xpath_options) }
|
2011-08-27 17:54:55 -04:00
|
|
|
end
|