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