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

Merge pull request #1789 from jnicklas/label_click_speedup

speedup label clicking behavior
This commit is contained in:
Thomas Walpole 2016-11-10 09:20:42 -08:00 committed by GitHub
commit d54d3d164e
2 changed files with 44 additions and 30 deletions

View file

@ -115,20 +115,22 @@ module Capybara
locator, options = nil, locator if locator.is_a? Hash locator, options = nil, locator if locator.is_a? Hash
allow_label_click = options.delete(:allow_label_click) { Capybara.automatic_label_click } allow_label_click = options.delete(:allow_label_click) { Capybara.automatic_label_click }
synchronize(Capybara::Queries::BaseQuery::wait(options)) do
begin begin
radio = find(:radio_button, locator, options) radio = find(:radio_button, locator, options)
radio.set(true) radio.set(true)
rescue => e rescue => e
raise unless allow_label_click && catch_error?(e) raise unless allow_label_click && catch_error?(e)
begin begin
radio ||= find(:radio_button, locator, options.merge({wait: 0, visible: :all})) radio ||= find(:radio_button, locator, options.merge(visible: :all))
label = find(:label, for: radio, wait: 0, visible: true) label = find(:label, for: radio, visible: true)
label.click unless radio.checked? label.click unless radio.checked?
rescue rescue
raise e raise e
end end
end end
end end
end
## ##
# #
@ -153,20 +155,22 @@ module Capybara
locator, options = nil, locator if locator.is_a? Hash locator, options = nil, locator if locator.is_a? Hash
allow_label_click = options.delete(:allow_label_click) { Capybara.automatic_label_click } allow_label_click = options.delete(:allow_label_click) { Capybara.automatic_label_click }
synchronize(Capybara::Queries::BaseQuery::wait(options)) do
begin begin
cbox = find(:checkbox, locator, options) cbox = find(:checkbox, locator, options)
cbox.set(true) cbox.set(true)
rescue => e rescue => e
raise unless allow_label_click && catch_error?(e) raise unless allow_label_click && catch_error?(e)
begin begin
cbox ||= find(:checkbox, locator, options.merge({wait: 0, visible: :all})) cbox ||= find(:checkbox, locator, options.merge(visible: :all))
label = find(:label, for: cbox, wait: 0, visible: true) label = find(:label, for: cbox, visible: true)
label.click unless cbox.checked? label.click unless cbox.checked?
rescue rescue
raise e raise e
end end
end end
end end
end
## ##
# #
@ -191,20 +195,22 @@ module Capybara
locator, options = nil, locator if locator.is_a? Hash locator, options = nil, locator if locator.is_a? Hash
allow_label_click = options.delete(:allow_label_click) { Capybara.automatic_label_click } allow_label_click = options.delete(:allow_label_click) { Capybara.automatic_label_click }
synchronize(Capybara::Queries::BaseQuery::wait(options)) do
begin begin
cbox = find(:checkbox, locator, options) cbox = find(:checkbox, locator, options)
cbox.set(false) cbox.set(false)
rescue => e rescue => e
raise unless allow_label_click && catch_error?(e) raise unless allow_label_click && catch_error?(e)
begin begin
cbox ||= find(:checkbox, locator, options.merge({wait: 0, visible: :all})) cbox ||= find(:checkbox, locator, options.merge(visible: :all))
label = find(:label, for: cbox, wait: 0, visible: true) label = find(:label, for: cbox, visible: true)
label.click if cbox.checked? label.click if cbox.checked?
rescue rescue
raise e raise e
end end
end end
end end
end
## ##
# #

View file

@ -169,6 +169,14 @@ Capybara::SpecHelper.spec "#check" do
expect(extract_results(@session)['cars']).to include('tesla') expect(extract_results(@session)['cars']).to include('tesla')
end end
it "should not wait the full time if label can be clicked" do
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be
start_time = Time.now
@session.check('form_cars_tesla', allow_label_click: true, wait: 10)
end_time = Time.now
expect(end_time - start_time).to be < 10
end
it "should check via the label if input is moved off the left edge of the page" do it "should check via the label if input is moved off the left edge of the page" do
expect(@session.find(:checkbox, 'form_cars_pagani', unchecked: true, visible: :all)).to be expect(@session.find(:checkbox, 'form_cars_pagani', unchecked: true, visible: :all)).to be
@session.check('form_cars_pagani', allow_label_click: true) @session.check('form_cars_pagani', allow_label_click: true)