From 272a052f68c6079ae90940910b5da0bc197faf07 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Wed, 21 Sep 2016 12:10:32 -0700 Subject: [PATCH] Allow all invalid element error types to trigger allow_label_click behavior - Issue #1762 --- .travis.yml | 2 +- lib/capybara/node/actions.rb | 27 ++++++++++++++----------- lib/capybara/spec/session/check_spec.rb | 19 ++++++++++++----- lib/capybara/spec/views/form.erb | 2 ++ 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index 35dd7d51..cb07d49c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ matrix: - gemfile: gemfiles/Gemfile.base-versions rvm: 1.9.3 - gemfile: gemfiles/Gemfile.beta-versions - rvm: 2.4.0-preview1 + rvm: 2.4.0-preview2 - gemfile: gemfiles/Gemfile.beta-marionette env: GECKODRIVER=true rvm: 2.3.1 diff --git a/lib/capybara/node/actions.rb b/lib/capybara/node/actions.rb index 60fd9550..1b634914 100644 --- a/lib/capybara/node/actions.rb +++ b/lib/capybara/node/actions.rb @@ -109,11 +109,12 @@ module Capybara 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_label_click + 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({wait: 0, visible: :hidden})) + 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 @@ -145,11 +146,12 @@ module Capybara 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_label_click + 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({wait: 0, visible: :hidden})) + 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 @@ -181,11 +183,12 @@ module Capybara 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_label_click + 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({wait: 0, visible: :hidden})) + 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 diff --git a/lib/capybara/spec/session/check_spec.rb b/lib/capybara/spec/session/check_spec.rb index dc643daf..ce835c72 100644 --- a/lib/capybara/spec/session/check_spec.rb +++ b/lib/capybara/spec/session/check_spec.rb @@ -161,11 +161,20 @@ Capybara::SpecHelper.spec "#check" do expect{@session.check('form_cars_mclaren')}.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_mclaren"') end - 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') + context "with allow_label_click == true" do + it "should check via the label if input is hidden" 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 + + 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) + @session.click_button('awesome') + expect(extract_results(@session)['cars']).to include('pagani') + end end end end diff --git a/lib/capybara/spec/views/form.erb b/lib/capybara/spec/views/form.erb index 6ae7809c..942524bf 100644 --- a/lib/capybara/spec/views/form.erb +++ b/lib/capybara/spec/views/form.erb @@ -183,6 +183,8 @@ New line after and before textarea tag + +