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

Make default label click behavior configurable and update option name to clarify

This commit is contained in:
Thomas Walpole 2016-08-01 14:14:10 -07:00
parent 3afdd391e5
commit 6a4676a62e
6 changed files with 111 additions and 66 deletions

View file

@ -8,7 +8,7 @@ Release date: Unreleased
* background/given/given! RSoec aliases will work if RSpec config.shared_context_metadata_behavior == :apply_to_host_groups [Thomas Walpole]
* Fixed setting of unexpectedAlertError now that Selenium will be freezing the Capabilities::DEFAULTS [Thomas Walpole]
### Added
* 'check', 'uncheck', and 'choose' will now click the associated label if the checkbox/radio button is not visible [Thomas Walpole]
* 'check', 'uncheck', and 'choose' can now optionally click the associated label if the checkbox/radio button is not visible [Thomas Walpole]
* Raise error if Capybara.app_host/default_host are specified incorrectly [Thomas Walpole]
* Capybara::Selector::FilterSet allows for sharing filter definitions between selectors [Thomas Walpole]
* Remove need to pass nil locator in most node actions when locator is not needed [Thomas Walpole]

View file

@ -24,7 +24,7 @@ module Capybara
attr_accessor :asset_host, :run_server, :always_include_port
attr_accessor :server_port, :exact, :match, :exact_options, :visible_text_only
attr_accessor :default_selector, :default_max_wait_time, :ignore_hidden_elements
attr_accessor :save_path, :wait_on_first_by_default, :automatic_reload
attr_accessor :save_path, :wait_on_first_by_default, :automatic_label_click, :automatic_reload
attr_accessor :reuse_server, :raise_server_errors, :server_errors
attr_writer :default_driver, :current_driver, :javascript_driver, :session_name, :server_host
attr_reader :save_and_open_page_path
@ -53,6 +53,7 @@ module Capybara
# [automatic_reload = Boolean] Whether to automatically reload elements as Capybara is waiting (Default: true)
# [save_path = String] Where to put pages saved through save_(page|screenshot), save_and_open_(page|screenshot) (Default: Dir.pwd)
# [wait_on_first_by_default = Boolean] Whether Node#first defaults to Capybara waiting behavior for at least 1 element to match (Default: false)
# [automatic_label_click = Boolean] Whether Node#choose, Node#check, Node#uncheck will attempt to click the associated label element if the checkbox/radio button are non-visible (Default: false)
# [reuse_server = Boolean] Reuse the server thread between multiple sessions using the same app object (Default: true)
# === DSL Options
#
@ -498,6 +499,7 @@ Capybara.configure do |config|
config.server_errors = [StandardError]
config.visible_text_only = false
config.wait_on_first_by_default = false
config.automatic_label_click = false
config.reuse_server = true
end

View file

@ -83,8 +83,8 @@ 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.
# @!macro label_click
# @option options [Boolean] :allow_label_click (Capybara.automatic_label_click) Attempt to click the label to toggle state if element is non-visible.
##
#
@ -100,15 +100,15 @@ module Capybara
# @option options [String] id Match fields that match the id attribute
# @option options [String] name Match fields that match the name attribute
# @macro waiting_behavior
# @macro click_label
# @macro label_click
def choose(locator, options={})
locator, options = nil, locator if locator.is_a? Hash
allow_click_label = options.delete(:click_label) { true }
allow_label_click = options.delete(:allow_label_click) { Capybara.automatic_label_click }
begin
find(:radio_button, locator, options).set(true)
rescue Capybara::ElementNotFound => e
raise unless allow_click_label
raise unless allow_label_click
begin
radio = find(:radio_button, locator, options.merge({wait: 0, visible: :hidden}))
label = find(:label, for: radio, wait: 0, visible: true)
@ -133,17 +133,17 @@ module Capybara
# @option options [String] :option Value of the checkbox to select
# @option options [String] id Match fields that match the id attribute
# @option options [String] name Match fields that match the name attribute
# @macro click_label
# @macro label_click
# @macro waiting_behavior
#
def check(locator, options={})
locator, options = nil, locator if locator.is_a? Hash
allow_click_label = options.delete(:click_label) { true }
allow_label_click = options.delete(:allow_label_click) { Capybara.automatic_label_click }
begin
find(:checkbox, locator, options).set(true)
rescue Capybara::ElementNotFound => e
raise unless allow_click_label
raise unless allow_label_click
begin
cbox = find(:checkbox, locator, options.merge({wait: 0, visible: :hidden}))
label = find(:label, for: cbox, wait: 0, visible: true)
@ -168,17 +168,17 @@ module Capybara
# @option options [String] :option Value of the checkbox to deselect
# @option options [String] id Match fields that match the id attribute
# @option options [String] name Match fields that match the name attribute
# @macro click_label
# @macro label_click
# @macro waiting_behavior
#
def uncheck(locator, options={})
locator, options = nil, locator if locator.is_a? Hash
allow_click_label = options.delete(:click_label) { true }
allow_label_click = options.delete(:allow_label_click) { Capybara.automatic_label_click }
begin
find(:checkbox, locator, options).set(false)
rescue Capybara::ElementNotFound => e
raise unless allow_click_label
raise unless allow_label_click
begin
cbox = find(:checkbox, locator, options.merge({wait: 0, visible: :hidden}))
label = find(:label, for: cbox, wait: 0, visible: true)

View file

@ -113,33 +113,60 @@ Capybara::SpecHelper.spec "#check" do
end
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')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('tesla')
context "with Capybara.automatic_label_click == true" do
around do |spec|
old_click_label, Capybara.automatic_label_click = Capybara.automatic_label_click, true
spec.run
Capybara.automatic_label_click = old_click_label
end
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')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('tesla')
end
it "should check via clicking the wrapping label if possible" do
expect(@session.find(:checkbox, 'form_cars_mclaren', unchecked: true, visible: :hidden)).to be
@session.check('form_cars_mclaren')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('mclaren')
end
it "should not click the label if unneeded" do
expect(@session.find(:checkbox, 'form_cars_jaguar', checked: true, visible: :hidden)).to be
@session.check('form_cars_jaguar')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('jaguar')
end
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', allow_label_click: false)}.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_mclaren"')
end
end
it "should check via clicking the wrapping label if possible" do
expect(@session.find(:checkbox, 'form_cars_mclaren', unchecked: true, visible: :hidden)).to be
@session.check('form_cars_mclaren')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('mclaren')
end
context "with Capybara.automatic_label_click == false" do
around do |spec|
old_label_click, Capybara.automatic_label_click = Capybara.automatic_label_click, false
spec.run
Capybara.automatic_label_click = old_label_click
end
it "should not click the label if unneeded" do
expect(@session.find(:checkbox, 'form_cars_jaguar', checked: true, visible: :hidden)).to be
@session.check('form_cars_jaguar')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('jaguar')
end
it "should raise error if checkbox not visible" do
expect{@session.check('form_cars_mclaren')}.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_mclaren"')
end
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"')
it "should check via the label if allow_label_click == true" do
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be
@session.check('form_cars_tesla', allow_label_click: true)
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('tesla')
end
end
end
end

View file

@ -68,14 +68,22 @@ Capybara::SpecHelper.spec "#choose" do
end
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
context "with Capybara.automatic_label_click == true" do
around do |spec|
old_click_label, Capybara.automatic_label_click = Capybara.automatic_label_click, true
spec.run
Capybara.automatic_label_click = old_click_label
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"')
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", allow_label_click: false)}.to raise_error(Capybara::ElementNotFound, 'Unable to find radio button "party_democrat"')
end
end
end
end

View file

@ -40,33 +40,41 @@ Capybara::SpecHelper.spec "#uncheck" do
end
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')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).not_to include('jaguar')
end
context "with Capybara.automatic_label_click == true" do
around do |spec|
old_click_label, Capybara.automatic_label_click = Capybara.automatic_label_click, true
spec.run
Capybara.automatic_label_click = old_click_label
end
it "should uncheck via clicking the wrapping label if possible" do
expect(@session.find(:checkbox, 'form_cars_koenigsegg', checked: true, visible: :hidden)).to be
@session.uncheck('form_cars_koenigsegg')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).not_to include('koenigsegg')
end
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')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).not_to include('jaguar')
end
it "should not click the label if unneeded" do
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be
@session.uncheck('form_cars_tesla')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).not_to include('tesla')
end
it "should uncheck via clicking the wrapping label if possible" do
expect(@session.find(:checkbox, 'form_cars_koenigsegg', checked: true, visible: :hidden)).to be
@session.uncheck('form_cars_koenigsegg')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).not_to include('koenigsegg')
end
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 not click the label if unneeded" do
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be
@session.uncheck('form_cars_tesla')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).not_to include('tesla')
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"')
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', allow_label_click: false)}.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_jaguar"')
end
end
end
end