2010-08-27 18:37:39 +00:00
|
|
|
module Capybara
|
|
|
|
class Selector
|
2012-01-02 11:46:04 +00:00
|
|
|
attr_reader :name, :custom_filters
|
2010-08-27 18:37:39 +00:00
|
|
|
|
2011-02-10 15:59:26 +00:00
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2010-10-22 11:29:08 +00:00
|
|
|
def initialize(name, &block)
|
2010-08-27 18:37:39 +00:00
|
|
|
@name = name
|
2012-01-02 11:46:04 +00:00
|
|
|
@custom_filters = {}
|
2012-01-08 13:50:41 +00:00
|
|
|
@match = nil
|
2012-01-08 13:50:55 +00:00
|
|
|
@failure_message = nil
|
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
|
|
|
|
|
2012-01-03 18:33:39 +00:00
|
|
|
def call(locator)
|
|
|
|
@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
|
2012-01-02 11:46:04 +00:00
|
|
|
|
|
|
|
def filter(name, &block)
|
|
|
|
@custom_filters[name] = block
|
|
|
|
end
|
2010-08-27 18:37:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-22 11:29:08 +00:00
|
|
|
Capybara.add_selector(:xpath) do
|
2011-09-05 16:14:10 +00:00
|
|
|
xpath { |xpath| xpath }
|
2010-10-22 11:29:08 +00:00
|
|
|
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
|
2011-09-05 16:14:10 +00:00
|
|
|
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
|
2010-10-22 11:29:08 +00:00
|
|
|
match { |value| value.is_a?(Symbol) }
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
|
|
|
|
Capybara.add_selector(:field) do
|
2011-09-05 16:14:10 +00:00
|
|
|
xpath { |locator| XPath::HTML.field(locator) }
|
2012-01-02 16:26:23 +00:00
|
|
|
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 }
|
2012-03-01 02:57:05 +00:00
|
|
|
filter(:type) { |node, type| node[:type] == type }
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
2011-08-27 21:57:12 +00:00
|
|
|
Capybara.add_selector(:fieldset) do
|
2011-09-05 16:14:10 +00:00
|
|
|
xpath { |locator| XPath::HTML.fieldset(locator) }
|
2011-08-27 21:57:12 +00:00
|
|
|
end
|
|
|
|
|
2011-08-27 02:16:15 +00:00
|
|
|
Capybara.add_selector(:link_or_button) do
|
2011-09-05 16:14:10 +00:00
|
|
|
xpath { |locator| XPath::HTML.link_or_button(locator) }
|
2011-08-27 02:16:15 +00:00
|
|
|
failure_message { |node, selector| "no link or button '#{selector.locator}' found" }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:link) do
|
2012-01-03 12:14:26 +00:00
|
|
|
xpath { |locator| XPath::HTML.link(locator) }
|
2011-08-27 02:16:15 +00:00
|
|
|
failure_message { |node, selector| "no link with title, id or text '#{selector.locator}' found" }
|
2012-01-03 08:43:11 +00:00
|
|
|
filter(:href) do |node, href|
|
2012-01-03 12:14:26 +00:00
|
|
|
node.first(:xpath, XPath.axis(:self)[XPath.attr(:href).equals(href)])
|
2012-01-03 08:43:11 +00:00
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:button) do
|
2011-09-05 16:14:10 +00:00
|
|
|
xpath { |locator| XPath::HTML.button(locator) }
|
2011-08-27 02:16:15 +00:00
|
|
|
failure_message { |node, selector| "no button with value or id or text '#{selector.locator}' found" }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:fillable_field) do
|
2012-01-03 12:14:26 +00:00
|
|
|
xpath { |locator| XPath::HTML.fillable_field(locator) }
|
2011-08-27 02:16:15 +00: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
|
2012-01-03 12:14:26 +00:00
|
|
|
xpath { |locator| XPath::HTML.radio_button(locator) }
|
2011-08-27 02:16:15 +00:00
|
|
|
failure_message { |node, selector| "no radio button with id, name, or label '#{selector.locator}' found" }
|
2012-01-02 16:26:23 +00:00
|
|
|
filter(:checked) { |node, value| not(value ^ node.checked?) }
|
|
|
|
filter(:unchecked) { |node, value| (value ^ node.checked?) }
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:checkbox) do
|
2012-01-03 12:14:26 +00:00
|
|
|
xpath { |locator| XPath::HTML.checkbox(locator) }
|
2011-08-27 02:16:15 +00:00
|
|
|
failure_message { |node, selector| "no checkbox with id, name, or label '#{selector.locator}' found" }
|
2012-01-02 16:26:23 +00:00
|
|
|
filter(:checked) { |node, value| not(value ^ node.checked?) }
|
|
|
|
filter(:unchecked) { |node, value| (value ^ node.checked?) }
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:select) do
|
2012-01-03 12:14:26 +00:00
|
|
|
xpath { |locator| XPath::HTML.select(locator) }
|
2011-08-27 02:16:15 +00:00
|
|
|
failure_message { |node, selector| "no select box with id, name, or label '#{selector.locator}' found" }
|
2012-01-02 11:53:03 +00:00
|
|
|
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 }
|
2012-01-02 17:01:58 +00:00
|
|
|
([selected].flatten - actual).empty?
|
2012-01-02 16:12:21 +00:00
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:option) do
|
2011-09-05 16:14:10 +00:00
|
|
|
xpath { |locator| XPath::HTML.option(locator) }
|
2011-08-27 02:16:15 +00:00
|
|
|
failure_message do |node, selector|
|
|
|
|
"no option with text '#{selector.locator}'".tap do |message|
|
2012-01-31 15:06:42 +00:00
|
|
|
if node.is_a?(Capybara::Node::Element) and node.tag_name == 'select'
|
|
|
|
message << " in the select box"
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:file_field) do
|
2012-01-03 12:14:26 +00:00
|
|
|
xpath { |locator| XPath::HTML.file_field(locator) }
|
2011-08-27 02:16:15 +00:00
|
|
|
failure_message { |node, selector| "no file field with id, name, or label '#{selector.locator}' found" }
|
|
|
|
end
|
2011-08-27 21:54:55 +00:00
|
|
|
|
|
|
|
Capybara.add_selector(:content) do
|
2011-09-05 16:14:10 +00:00
|
|
|
xpath { |content| XPath::HTML.content(content) }
|
2011-08-27 21:54:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:table) do
|
2012-01-03 12:14:26 +00:00
|
|
|
xpath { |locator| XPath::HTML.table(locator) }
|
2011-08-27 21:54:55 +00:00
|
|
|
end
|