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

View file

@ -169,6 +169,14 @@ Capybara::SpecHelper.spec "#check" do
expect(extract_results(@session)['cars']).to include('tesla')
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
expect(@session.find(:checkbox, 'form_cars_pagani', unchecked: true, visible: :all)).to be
@session.check('form_cars_pagani', allow_label_click: true)