1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Allow custom filters

This commit is contained in:
Jonas Nicklas 2012-01-02 12:46:04 +01:00
parent 6de89c1038
commit fb01cfaf21
2 changed files with 28 additions and 1 deletions

View file

@ -2,7 +2,7 @@ module Capybara
class Selector
PROPERTY_OPTION_KEYS = [:text, :visible, :with, :checked, :unchecked, :selected]
attr_reader :name
attr_reader :name, :custom_filters
class Normalized
attr_accessor :selector, :locator, :options, :xpath_options, :property_options, :xpaths
@ -17,6 +17,9 @@ module Capybara
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])
selector.custom_filters.each do |name, block|
return false if options.has_key?(name) and not block.call(node, options[name])
end
true
end
@ -79,6 +82,7 @@ module Capybara
def initialize(name, &block)
@name = name
@custom_filters = {}
instance_eval(&block)
end
@ -112,6 +116,10 @@ module Capybara
def match?(locator)
@match and @match.call(locator)
end
def filter(name, &block)
@custom_filters[name] = block
end
end
end

View file

@ -107,6 +107,25 @@ shared_examples_for "find" do
end
end
context "with custom selector with custom filter", :focus => true do
before do
Capybara.add_selector(:monkey) do
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }
filter(:name) { |node, name| node.text == name }
end
end
it "should find elements that match the filter" do
@session.find(:monkey, '1', :name => 'Monkey John').text.should == 'Monkey John'
@session.find(:monkey, '2', :name => 'Monkey Paul').text.should == 'Monkey Paul'
end
it "should not find elements that don't match the filter" do
expect { @session.find(:monkey, '2', :name => 'Monkey John') }.to raise_error(Capybara::ElementNotFound)
expect { @session.find(:monkey, '1', :name => 'Monkey Paul') }.to raise_error(Capybara::ElementNotFound)
end
end
context "with css as default selector" do
before { Capybara.default_selector = :css }
it "should find the first element using the given locator" do