Merge pull request #1684 from jnicklas/optional_locator
Optional locator
This commit is contained in:
commit
3f515ff826
|
@ -7,10 +7,19 @@ module Capybara
|
|||
#
|
||||
# Finds a button or link by id, text or value and clicks it. Also looks at image
|
||||
# alt text inside the link.
|
||||
# @!macro waiting_behavior
|
||||
# If the driver is capable of executing JavaScript, +$0+ will wait for a set amount of time
|
||||
# and continuously retry finding the element until either the element is found or the time
|
||||
# expires. The length of time +find+ will wait is controlled through {Capybara.default_max_wait_time}
|
||||
#
|
||||
# @param [String] locator Text, id or value of link or button
|
||||
# @option options [false, Numeric] wait (Capybara.default_max_wait_time) Maximum time to wait for matching element to appear.
|
||||
#
|
||||
def click_link_or_button(locator, options={})
|
||||
# @overload click_link_or_button([locator], options)
|
||||
#
|
||||
# @param [String] locator Text, id or value of link or button
|
||||
#
|
||||
def click_link_or_button(locator=nil, options={})
|
||||
locator, options = nil, locator if locator.is_a? Hash
|
||||
find(:link_or_button, locator, options).click
|
||||
end
|
||||
alias_method :click_on, :click_link_or_button
|
||||
|
@ -20,10 +29,14 @@ module Capybara
|
|||
# Finds a link by id, text or title and clicks it. Also looks at image
|
||||
# alt text inside the link.
|
||||
#
|
||||
# @param [String] locator text, id, title or nested image's alt attribute
|
||||
# @param options See {Capybara::Node::Finders#find_link}
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
def click_link(locator, options={})
|
||||
# @overload click_link([locator], options)
|
||||
# @param [String] locator text, id, title or nested image's alt attribute
|
||||
# @param options See {Capybara::Node::Finders#find_link}
|
||||
#
|
||||
def click_link(locator=nil, options={})
|
||||
locator, options = nil, locator if locator.is_a? Hash
|
||||
find(:link, locator, options).click
|
||||
end
|
||||
|
||||
|
@ -34,9 +47,13 @@ module Capybara
|
|||
# \<button> element. All buttons can be found by their id, value, or title. \<button> elements can also be found
|
||||
# by their text content, and image \<input> elements by their alt attribute
|
||||
#
|
||||
# @param [String] locator Which button to find
|
||||
# @param options See {Capybara::Node::Finders#find_button}
|
||||
def click_button(locator, options={})
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @overload click_button([locator], options)
|
||||
# @param [String] locator Which button to find
|
||||
# @param options See {Capybara::Node::Finders#find_button}
|
||||
def click_button(locator=nil, options={})
|
||||
locator, options = nil, locator if locator.is_a? Hash
|
||||
find(:button, locator, options).click
|
||||
end
|
||||
|
||||
|
@ -47,10 +64,13 @@ module Capybara
|
|||
#
|
||||
# page.fill_in 'Name', :with => 'Bob'
|
||||
#
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @param [String] locator Which field to fill in
|
||||
# @param [Hash] options
|
||||
# @option options [String] :with The value to fill in - required
|
||||
# @option options [Hash] :fill_options Driver specific options regarding how to fill fields
|
||||
# @option options [String] :with The value to fill in - required
|
||||
# @option options [Hash] :fill_options Driver specific options regarding how to fill fields
|
||||
# @option options [Boolean] :multiple Match fields that can have multiple values?
|
||||
#
|
||||
def fill_in(locator, options={})
|
||||
raise "Must pass a hash containing 'with'" if not options.is_a?(Hash) or not options.has_key?(:with)
|
||||
|
@ -66,9 +86,15 @@ module Capybara
|
|||
#
|
||||
# page.choose('Male')
|
||||
#
|
||||
# @param [String] locator Which radio button to choose
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @overload choose([locator], options)
|
||||
# @param [String] locator Which radio button to choose
|
||||
#
|
||||
# @option options [String] :option Value of the radio_button to choose
|
||||
#
|
||||
def choose(locator, options={})
|
||||
locator, options = nil, locator if locator.is_a? Hash
|
||||
find(:radio_button, locator, options).set(true)
|
||||
end
|
||||
|
||||
|
@ -79,9 +105,15 @@ module Capybara
|
|||
#
|
||||
# page.check('German')
|
||||
#
|
||||
# @param [String] locator Which check box to check
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @overload check([locator], options)
|
||||
# @param [String] locator Which check box to check
|
||||
#
|
||||
# @option options [String] :option Value of the checkbox to select
|
||||
#
|
||||
def check(locator, options={})
|
||||
locator, options = nil, locator if locator.is_a? Hash
|
||||
find(:checkbox, locator, options).set(true)
|
||||
end
|
||||
|
||||
|
@ -92,9 +124,15 @@ module Capybara
|
|||
#
|
||||
# page.uncheck('German')
|
||||
#
|
||||
# @param [String] locator Which check box to uncheck
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @overload uncheck([locator], options)
|
||||
# @param [String] locator Which check box to uncheck
|
||||
#
|
||||
# @option options [String] :option Value of the checkbox to deselect
|
||||
#
|
||||
def uncheck(locator, options={})
|
||||
locator, options = nil, locator if locator.is_a? Hash
|
||||
find(:checkbox, locator, options).set(false)
|
||||
end
|
||||
|
||||
|
@ -109,6 +147,8 @@ module Capybara
|
|||
#
|
||||
# page.select 'March', :from => 'Month'
|
||||
#
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @param [String] value Which option to select
|
||||
# @option options [String] :from The id, name or label of the select box
|
||||
#
|
||||
|
@ -129,6 +169,8 @@ module Capybara
|
|||
#
|
||||
# page.unselect 'March', :from => 'Month'
|
||||
#
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @param [String] value Which option to unselect
|
||||
# @param [Hash{:from => String}] options The id, name or label of the select box
|
||||
#
|
||||
|
@ -148,12 +190,13 @@ module Capybara
|
|||
#
|
||||
# page.attach_file(locator, '/path/to/file.png')
|
||||
#
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @param [String] locator Which field to attach the file to
|
||||
# @param [String] path The path of the file that will be attached, or an array of paths
|
||||
#
|
||||
# @option options [Symbol] match (Capybara.match) The matching strategy to use (:one, :first, :prefer_exact, :smart).
|
||||
# @option options [Boolean] exact (Capybara.exact) Match the exact label name/contents or accept a partial match.
|
||||
# @option options [Fixnum] wait (Capybara.default_max_wait_time) If using a Javascript driver, maximum number of seconds during which the element will be searched for.
|
||||
# @option options [Boolean] multiple Match field which allows multiple file selection
|
||||
#
|
||||
def attach_file(locator, path, options={})
|
||||
|
|
|
@ -51,22 +51,27 @@ module Capybara
|
|||
#
|
||||
# Find a form field on the page. The field can be found by its name, id or label text.
|
||||
#
|
||||
# @macro waiting_behavior
|
||||
# @overload find_field([locator], options={})
|
||||
# @param [String] locator name, id, placeholder or text of associated label element
|
||||
#
|
||||
# @param [String] locator Which field to find
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @option options [Boolean] checked Match checked field?
|
||||
# @option options [Boolean] unchecked Match unchecked field?
|
||||
# @option options [Boolean, Symbol] disabled (false) Match disabled field?
|
||||
# * true - only finds a disabled field
|
||||
# * false - only finds an enabled field
|
||||
# * :all - finds either an enabled or disabled field
|
||||
# @option options [Boolean] readonly Match readonly field?
|
||||
# @option options [String] with Value of field to match on
|
||||
# @option options [String] type Type of field to match on
|
||||
#
|
||||
# @option options [Boolean] checked Match checked field?
|
||||
# @option options [Boolean] unchecked Match unchecked field?
|
||||
# @option options [Boolean, Symbol] disabled (false) Match disabled field?
|
||||
# * true - only finds a disabled field
|
||||
# * false - only finds an enabled field
|
||||
# * :all - finds either an enabled or disabled field
|
||||
# @option options [Boolean] readonly Match readonly field?
|
||||
# @option options [String] with Value of field to match on
|
||||
# @option options [String] type Type of field to match on
|
||||
# @option options [Boolean] multiple Match fields that can have multiple values?
|
||||
# @return [Capybara::Node::Element] The found element
|
||||
#
|
||||
def find_field(locator, options={})
|
||||
|
||||
def find_field(locator=nil, options={})
|
||||
locator, options = nil, locator if locator.is_a? Hash
|
||||
find(:field, locator, options)
|
||||
end
|
||||
alias_method :field_labeled, :find_field
|
||||
|
@ -75,13 +80,16 @@ module Capybara
|
|||
#
|
||||
# Find a link on the page. The link can be found by its id or text.
|
||||
#
|
||||
# @macro waiting_behavior
|
||||
# @overload find_link([locator], options={})
|
||||
# @param [String] locator id, title, text, or alt of enclosed img element
|
||||
#
|
||||
# @param [String] locator Which link to find
|
||||
# @option options [String,Regexp] href Value to match against the links href
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @option options [String,Regexp] href Value to match against the links href
|
||||
# @return [Capybara::Node::Element] The found element
|
||||
#
|
||||
def find_link(locator, options={})
|
||||
def find_link(locator=nil, options={})
|
||||
locator, options = nil, locator if locator.is_a? Hash
|
||||
find(:link, locator, options)
|
||||
end
|
||||
|
||||
|
@ -91,17 +99,22 @@ module Capybara
|
|||
# This can be any \<input> element of type submit, reset, image, button or it can be a
|
||||
# \<button> element. All buttons can be found by their id, value, or title. \<button> elements can also be found
|
||||
# by their text content, and image \<input> elements by their alt attribute
|
||||
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @param [String] locator Which button to find
|
||||
# @option options [Boolean, Symbol] disabled (false) Match disabled button?
|
||||
# * true - only finds a disabled button
|
||||
# * false - only finds an enabled button
|
||||
# * :all - finds either an enabled or disabled button
|
||||
# @overload find_button([locator], options={})
|
||||
# @param [String] locator id, value, title, text content, alt of image
|
||||
#
|
||||
# @overload find_button(options={})
|
||||
#
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @option options [Boolean, Symbol] disabled (false) Match disabled button?
|
||||
# * true - only finds a disabled button
|
||||
# * false - only finds an enabled button
|
||||
# * :all - finds either an enabled or disabled button
|
||||
# @return [Capybara::Node::Element] The found element
|
||||
#
|
||||
def find_button(locator, options={})
|
||||
def find_button(locator=nil, options={})
|
||||
locator, options = nil, locator if locator.is_a? Hash
|
||||
find(:button, locator, options)
|
||||
end
|
||||
|
||||
|
@ -111,7 +124,7 @@ module Capybara
|
|||
#
|
||||
# @macro waiting_behavior
|
||||
#
|
||||
# @param [String] id Which element to find
|
||||
# @param [String] id id of element
|
||||
#
|
||||
# @return [Capybara::Node::Element] The found element
|
||||
#
|
||||
|
|
|
@ -179,4 +179,11 @@ Capybara::SpecHelper.spec '#click_link' do
|
|||
end.to raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
end
|
||||
|
||||
context "without locator" do
|
||||
it "uses options" do
|
||||
@session.click_link(href: '/foo')
|
||||
expect(@session).to have_content('Another World')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -52,4 +52,10 @@ Capybara::SpecHelper.spec '#find_button' do
|
|||
expect(@session.find_button('Disabled button', :disabled => :all).value).to eq("Disabled button")
|
||||
end
|
||||
end
|
||||
|
||||
context "without locator" do
|
||||
it "should use options" do
|
||||
expect(@session.find_button(disabled: true).value).to eq("Disabled button")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -71,7 +71,6 @@ Capybara::SpecHelper.spec '#find_field' do
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
context 'with :readonly option' do
|
||||
it "should find readonly fields when true" do
|
||||
expect(@session.find_field('form[readonly_test]', readonly: true)[:id]).to eq 'readonly'
|
||||
|
@ -87,4 +86,10 @@ Capybara::SpecHelper.spec '#find_field' do
|
|||
end.to raise_error(Capybara::Ambiguous, /found 2 elements/)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with no locator', locator: true do
|
||||
it 'should use options to find the field' do
|
||||
expect(@session.find_field(with: 'dog')['id']).to eq "form_pets_dog"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -30,4 +30,10 @@ Capybara::SpecHelper.spec '#find_link' do
|
|||
end.to raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
end
|
||||
|
||||
context "without locator" do
|
||||
it "should use options" do
|
||||
expect(@session.find_link(href: '#anchor').text).to eq "Normal Anchor"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue