Add options to finders

This commit is contained in:
CJ Kihlbom and Jonas Nicklas 2013-02-24 17:47:53 +01:00
parent 60e21d27c3
commit ab8f723349
5 changed files with 60 additions and 10 deletions

View File

@ -19,6 +19,8 @@ module Capybara
# page.find('li', :text => 'Quox').click_link('Delete')
#
# @param (see Capybara::Node::Finders#all)
# @option options [Boolean] match The matching strategy to use.
#
# @return [Capybara::Element] The found element
# @raise [Capybara::ElementNotFound] If the element can't be found before time expires
#
@ -48,8 +50,8 @@ module Capybara
# @param [String] locator Which field to find
# @return [Capybara::Element] The found element
#
def find_field(locator)
find(:field, locator)
def find_field(locator, options={})
find(:field, locator, options)
end
alias_method :field_labeled, :find_field
@ -60,8 +62,8 @@ module Capybara
# @param [String] locator Which link to find
# @return [Capybara::Element] The found element
#
def find_link(locator)
find(:link, locator)
def find_link(locator, options={})
find(:link, locator, options)
end
##
@ -71,8 +73,8 @@ module Capybara
# @param [String] locator Which button to find
# @return [Capybara::Element] The found element
#
def find_button(locator)
find(:button, locator)
def find_button(locator, options={})
find(:button, locator, options)
end
##
@ -82,8 +84,8 @@ module Capybara
# @param [String] id Which element to find
# @return [Capybara::Element] The found element
#
def find_by_id(id)
find(:id, id)
def find_by_id(id, options={})
find(:id, id, options)
end
##
@ -118,8 +120,8 @@ module Capybara
# @param [String] locator The selector
# @option options [String, Regexp] text Only find elements which contain this text or match this regexp
# @option options [Boolean] visible Only find elements that are visible on the page. Setting this to false
# (the default, unless Capybara.ignore_hidden_elements = true), finds
# invisible _and_ visible elements.
# finds invisible _and_ visible elements.
# @option options [Boolean] exact Control whether `is` expressions in the given XPath match exactly or partially
# @return [Capybara::Result] A collection of found elements
#
def all(*args)

View File

@ -17,4 +17,16 @@ Capybara::SpecHelper.spec '#find_button' do
@session.find_button('Does not exist')
end.to raise_error(Capybara::ElementNotFound)
end
context "with :exact option" do
it "should accept partial matches when false" do
@session.find_button('What an Awesome', :exact => false)[:value].should == "awesome"
end
it "should not accept partial matches when true" do
expect do
@session.find_button('What an Awesome', :exact => true)
end.to raise_error(Capybara::ElementNotFound)
end
end
end

View File

@ -16,4 +16,16 @@ Capybara::SpecHelper.spec '#find_by_id' do
@session.find_by_id('nothing_with_this_id')
end.to raise_error(Capybara::ElementNotFound)
end
context "with :visible option" do
it "finds invisible elements when `false`" do
@session.find_by_id("hidden_via_ancestor", :visible => false).text(:all).should =~ /with hidden ancestor/
end
it "finds invisible elements when `false`" do
expect do
@session.find_by_id("hidden_via_ancestor", :visible => true)
end.to raise_error(Capybara::ElementNotFound)
end
end
end

View File

@ -25,4 +25,16 @@ Capybara::SpecHelper.spec '#find_field' do
@session.field_labeled('Does not exist')
end.to raise_error(Capybara::ElementNotFound)
end
context "with :exact option" do
it "should accept partial matches when false" do
@session.find_field("Explanation", :exact => false)[:name].should == "form[name_explanation]"
end
it "should not accept partial matches when true" do
expect do
@session.find_field("Explanation", :exact => true)
end.to raise_error(Capybara::ElementNotFound)
end
end
end

View File

@ -17,4 +17,16 @@ Capybara::SpecHelper.spec '#find_link' do
@session.find_link('Does not exist')
end.to raise_error(Capybara::ElementNotFound)
end
context "with :exact option" do
it "should accept partial matches when false" do
@session.find_link('abo', :exact => false).text.should == "labore"
end
it "should not accept partial matches when true" do
expect do
@session.find_link('abo', :exact => true)
end.to raise_error(Capybara::ElementNotFound)
end
end
end