teamcapybara--capybara/lib/capybara/selector.rb

214 lines
6.4 KiB
Ruby
Raw Normal View History

module Capybara
class Selector
2012-01-02 11:46:04 +00:00
attr_reader :name, :custom_filters
2011-02-10 15:59:26 +00:00
class Normalized
attr_accessor :selector, :locator, :options, :xpaths
2011-02-10 15:59:26 +00:00
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
2011-02-10 15:59:26 +00:00
def name; selector.name; end
2012-01-02 16:32:03 +00:00
def matches_filters?(node)
return false if options[:text] and not node.text.match(options[:text])
return false if options[:visible] and not node.visible?
2012-01-02 11:46:04 +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
2011-02-10 15:59:26 +00:00
end
class << self
def all
@selectors ||= {}
end
def add(name, &block)
all[name.to_sym] = Capybara::Selector.new(name.to_sym, &block)
end
def remove(name)
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
2011-02-06 19:07:12 +00:00
def normalize(*args)
2011-02-10 15:59:26 +00:00
normalized = Normalized.new
normalized.options = extract_normalized_options(args)
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]
else
2011-02-10 15:59:26 +00:00
normalized.selector = all.values.find { |s| s.match?(args[0]) }
normalized.locator = args[0]
end
2011-02-10 15:59:26 +00:00
normalized.selector ||= all[Capybara.default_selector]
2011-02-04 15:16:46 +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
end
end
def initialize(name, &block)
@name = name
2012-01-02 11:46:04 +00:00
@custom_filters = {}
instance_eval(&block)
end
def xpath(&block)
@xpath = block if block
@xpath
end
# Same as xpath, but wrap in XPath.css().
def css(&block)
if block
@xpath = xpath { |*args| XPath.css(block.call(*args)) }
end
@xpath
end
def match(&block)
@match = block if block
@match
end
2011-02-04 15:16:46 +00:00
def failure_message(&block)
@failure_message = block if block
@failure_message
end
def call(locator, xpath_options={})
@xpath.call(locator, xpath_options)
end
def match?(locator)
@match and @match.call(locator)
end
2012-01-02 11:46:04 +00:00
def filter(name, &block)
@custom_filters[name] = block
end
end
end
Capybara.add_selector(:xpath) do
xpath { |xpath| xpath }
end
Capybara.add_selector(:css) do
css { |css| css }
end
Capybara.add_selector(:id) do
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
match { |value| value.is_a?(Symbol) }
end
Capybara.add_selector(:field) do
xpath { |locator| XPath::HTML.field(locator) }
filter(:checked) { |node, value| not(value ^ node.checked?) }
filter(:unchecked) { |node, value| (value ^ node.checked?) }
2012-01-02 16:27:26 +00:00
filter(:with) { |node, with| node.value == with }
end
Capybara.add_selector(:fieldset) do
xpath { |locator| XPath::HTML.fieldset(locator) }
end
Capybara.add_selector(:link_or_button) do
xpath { |locator| XPath::HTML.link_or_button(locator) }
failure_message { |node, selector| "no link or button '#{selector.locator}' found" }
end
Capybara.add_selector(:link) do
xpath { |locator, xpath_options| XPath::HTML.link(locator, xpath_options) }
failure_message { |node, selector| "no link with title, id or text '#{selector.locator}' found" }
filter(:href) { |node, href| node[:href] == href }
end
Capybara.add_selector(:button) do
xpath { |locator| XPath::HTML.button(locator) }
failure_message { |node, selector| "no button with value or id or text '#{selector.locator}' found" }
end
Capybara.add_selector(:fillable_field) do
xpath { |locator, xpath_options| XPath::HTML.fillable_field(locator, xpath_options) }
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
xpath { |locator, xpath_options| XPath::HTML.radio_button(locator, xpath_options) }
failure_message { |node, selector| "no radio button with id, name, or label '#{selector.locator}' found" }
filter(:checked) { |node, value| not(value ^ node.checked?) }
filter(:unchecked) { |node, value| (value ^ node.checked?) }
end
Capybara.add_selector(:checkbox) do
xpath { |locator, xpath_options| XPath::HTML.checkbox(locator, xpath_options) }
failure_message { |node, selector| "no checkbox with id, name, or label '#{selector.locator}' found" }
filter(:checked) { |node, value| not(value ^ node.checked?) }
filter(:unchecked) { |node, value| (value ^ node.checked?) }
end
Capybara.add_selector(:select) do
xpath { |locator, xpath_options| XPath::HTML.select(locator, xpath_options) }
failure_message { |node, selector| "no select box with id, name, or label '#{selector.locator}' found" }
filter(:options) { |node, options| options.all? { |option| node.first(:option, option) } }
2012-01-02 16:12:21 +00:00
filter(:selected) do |node, selected|
actual = node.all(:xpath, './/option').select { |option| option.selected? }.map { |option| option.text }
(selected - actual).empty?
end
end
Capybara.add_selector(:option) do
xpath { |locator| XPath::HTML.option(locator) }
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
xpath { |locator, xpath_options| XPath::HTML.file_field(locator, xpath_options) }
failure_message { |node, selector| "no file field with id, name, or label '#{selector.locator}' found" }
end
Capybara.add_selector(:content) do
xpath { |content| XPath::HTML.content(content) }
end
Capybara.add_selector(:table) do
xpath { |locator, xpath_options| XPath::HTML.table(locator, xpath_options) }
end