provide option to disable label clicking behavior

This commit is contained in:
Thomas Walpole 2016-04-22 10:11:19 -07:00
parent 0d365d5b96
commit 8de813c830
4 changed files with 33 additions and 8 deletions

View File

@ -79,6 +79,9 @@ module Capybara
find(:fillable_field, locator, options).set(with, fill_options)
end
# @!macro click_label
# @option options [Boolean] :click_label (true) Attempt to click the label to toggle state if element is non-visible.
##
#
# Find a radio button and mark it as checked. The radio button can be found
@ -86,18 +89,20 @@ module Capybara
#
# page.choose('Male')
#
# @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
#
# @macro waiting_behavior
# @macro click_label
def choose(locator, options={})
locator, options = nil, locator if locator.is_a? Hash
allow_click_label = options.delete(:click_label) { true }
begin
find(:radio_button, locator, options).set(true)
rescue Capybara::ElementNotFound => e
raise unless allow_click_label
begin
radio = find(:radio_button, locator, options.merge({wait: 0, visible: :hidden}))
label = find(:label, for: radio, wait: 0, visible: true)
@ -115,18 +120,22 @@ module Capybara
#
# page.check('German')
#
# @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
# @macro click_label
# @macro waiting_behavior
#
def check(locator, options={})
locator, options = nil, locator if locator.is_a? Hash
allow_click_label = options.delete(:click_label) { true }
begin
find(:checkbox, locator, options).set(true)
rescue Capybara::ElementNotFound => e
raise unless allow_click_label
begin
cbox = find(:checkbox, locator, options.merge({wait: 0, visible: :hidden}))
label = find(:label, for: cbox, wait: 0, visible: true)
@ -144,18 +153,22 @@ module Capybara
#
# page.uncheck('German')
#
# @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
# @macro click_label
# @macro waiting_behavior
#
def uncheck(locator, options={})
locator, options = nil, locator if locator.is_a? Hash
allow_click_label = options.delete(:click_label) { true }
begin
find(:checkbox, locator, options).set(false)
rescue Capybara::ElementNotFound => e
raise unless allow_click_label
begin
cbox = find(:checkbox, locator, options.merge({wait: 0, visible: :hidden}))
label = find(:label, for: cbox, wait: 0, visible: true)

View File

@ -112,7 +112,7 @@ Capybara::SpecHelper.spec "#check" do
end
end
context "when checkbox hidden", hidden: true do
context "when checkbox hidden" do
it "should check via clicking the label with :for attribute if possible" do
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be
@session.check('form_cars_tesla')
@ -137,5 +137,9 @@ Capybara::SpecHelper.spec "#check" do
it "should raise original error when no label available" do
expect { @session.check('form_cars_ariel') }.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_ariel"')
end
it "should raise error if not allowed to click label" do
expect{@session.check('form_cars_mclaren', click_label: false)}.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_mclaren"')
end
end
end

View File

@ -67,11 +67,15 @@ Capybara::SpecHelper.spec "#choose" do
end
end
context "with hidden radio buttons", hidden: true do
context "with hidden radio buttons" do
it "should select by clicking the link if available" do
@session.choose("party_democrat")
@session.click_button('awesome')
expect(extract_results(@session)['party']).to eq('democrat')
end
it "should raise error if not allowed to click label" do
expect{@session.choose("party_democrat", click_label: false)}.to raise_error(Capybara::ElementNotFound, 'Unable to find radio button "party_democrat"')
end
end
end

View File

@ -39,7 +39,7 @@ Capybara::SpecHelper.spec "#uncheck" do
end
end
context "when checkbox hidden", hidden: true do
context "when checkbox hidden" do
it "should uncheck via clicking the label with :for attribute if possible" do
expect(@session.find(:checkbox, 'form_cars_jaguar', checked: true, visible: :hidden)).to be
@session.uncheck('form_cars_jaguar')
@ -64,5 +64,9 @@ Capybara::SpecHelper.spec "#uncheck" do
it "should raise original error when no label available" do
expect { @session.uncheck('form_cars_ariel') }.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_ariel"')
end
it "should raise error if not allowed to click label" do
expect{@session.uncheck('form_cars_jaguar', click_label: false)}.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_jaguar"')
end
end
end