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

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') # page.find('li', :text => 'Quox').click_link('Delete')
# #
# @param (see Capybara::Node::Finders#all) # @param (see Capybara::Node::Finders#all)
# @option options [Boolean] match The matching strategy to use.
#
# @return [Capybara::Element] The found element # @return [Capybara::Element] The found element
# @raise [Capybara::ElementNotFound] If the element can't be found before time expires # @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 # @param [String] locator Which field to find
# @return [Capybara::Element] The found element # @return [Capybara::Element] The found element
# #
def find_field(locator) def find_field(locator, options={})
find(:field, locator) find(:field, locator, options)
end end
alias_method :field_labeled, :find_field alias_method :field_labeled, :find_field
@ -60,8 +62,8 @@ module Capybara
# @param [String] locator Which link to find # @param [String] locator Which link to find
# @return [Capybara::Element] The found element # @return [Capybara::Element] The found element
# #
def find_link(locator) def find_link(locator, options={})
find(:link, locator) find(:link, locator, options)
end end
## ##
@ -71,8 +73,8 @@ module Capybara
# @param [String] locator Which button to find # @param [String] locator Which button to find
# @return [Capybara::Element] The found element # @return [Capybara::Element] The found element
# #
def find_button(locator) def find_button(locator, options={})
find(:button, locator) find(:button, locator, options)
end end
## ##
@ -82,8 +84,8 @@ module Capybara
# @param [String] id Which element to find # @param [String] id Which element to find
# @return [Capybara::Element] The found element # @return [Capybara::Element] The found element
# #
def find_by_id(id) def find_by_id(id, options={})
find(:id, id) find(:id, id, options)
end end
## ##
@ -118,8 +120,8 @@ module Capybara
# @param [String] locator The selector # @param [String] locator The selector
# @option options [String, Regexp] text Only find elements which contain this text or match this regexp # @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 # @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 # finds invisible _and_ visible elements.
# 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 # @return [Capybara::Result] A collection of found elements
# #
def all(*args) def all(*args)

View file

@ -17,4 +17,16 @@ Capybara::SpecHelper.spec '#find_button' do
@session.find_button('Does not exist') @session.find_button('Does not exist')
end.to raise_error(Capybara::ElementNotFound) end.to raise_error(Capybara::ElementNotFound)
end 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 end

View file

@ -16,4 +16,16 @@ Capybara::SpecHelper.spec '#find_by_id' do
@session.find_by_id('nothing_with_this_id') @session.find_by_id('nothing_with_this_id')
end.to raise_error(Capybara::ElementNotFound) end.to raise_error(Capybara::ElementNotFound)
end 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 end

View file

@ -25,4 +25,16 @@ Capybara::SpecHelper.spec '#find_field' do
@session.field_labeled('Does not exist') @session.field_labeled('Does not exist')
end.to raise_error(Capybara::ElementNotFound) end.to raise_error(Capybara::ElementNotFound)
end 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 end

View file

@ -17,4 +17,16 @@ Capybara::SpecHelper.spec '#find_link' do
@session.find_link('Does not exist') @session.find_link('Does not exist')
end.to raise_error(Capybara::ElementNotFound) end.to raise_error(Capybara::ElementNotFound)
end 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 end