Extract `selected` into filter

This commit is contained in:
Jonas Nicklas 2012-01-02 17:12:21 +01:00
parent 9b09d18066
commit 2d1b71cebc
2 changed files with 5 additions and 9 deletions

View File

@ -14,19 +14,11 @@ module Capybara
return false if options[:with] and not node.value == options[:with]
return false if options[:checked] and not node.checked?
return false if options[:unchecked] and node.checked?
return false if options[:selected] and not has_selected_options?(node, options[:selected])
selector.custom_filters.each do |name, block|
return false if options.has_key?(name) and not block.call(node, options[name])
end
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
end
class << self
@ -164,6 +156,10 @@ 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) } }
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

View File

@ -107,7 +107,7 @@ shared_examples_for "find" do
end
end
context "with custom selector with custom filter", :focus => true do
context "with custom selector with custom filter" do
before do
Capybara.add_selector(:monkey) do
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }