diff --git a/lib/capybara/node/actions.rb b/lib/capybara/node/actions.rb index 78fed296..fb8199f6 100644 --- a/lib/capybara/node/actions.rb +++ b/lib/capybara/node/actions.rb @@ -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 diff --git a/lib/capybara/spec/session/check_spec.rb b/lib/capybara/spec/session/check_spec.rb index acc58002..85e4ce06 100644 --- a/lib/capybara/spec/session/check_spec.rb +++ b/lib/capybara/spec/session/check_spec.rb @@ -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)