From fcf3c8265e2e6fb0e83702d1c9aa1ba28696db50 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Wed, 1 May 2019 09:56:08 -0700 Subject: [PATCH 1/2] Expand `click_on` beyond just an alias of `click_link_or_button` --- lib/capybara/node/actions.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/capybara/node/actions.rb b/lib/capybara/node/actions.rb index 038bf062c..94e0cdcb8 100644 --- a/lib/capybara/node/actions.rb +++ b/lib/capybara/node/actions.rb @@ -25,7 +25,31 @@ module Actions def click_link_or_button(locator = nil, **options) find(:link_or_button, locator, options).click end - alias_method :click_on, :click_link_or_button + + ## + # + # Finds an element and clicks on it + # + # By default this is an alias of {Capybara::Node::Actions#click_llink_or_button} + # @overload click_on([locator], **options) + # By default this is an alias of {Capybara::Node::Actions#click_link_or_button} + # @macro waiting_behavior + # @param [String] locator See {Capybara::Node::Actions#click_button} and {Capybara::Node::Actions#click_link} + # + # @overload click_on(selector, [locator], **options) + # @macro waiting_behavior + # @param [Symbol] symbol A registered selector type (:css, :xpath, :element, ...) See {Capybara::Selector} for built-in selectors. + # @param [String] locator See {Capybara::Selector} for locator specifics + # + # @return [Capybara::Node::Element] The element clicked + # + def click_on(*args, **options) + if args[0].is_a?(Symbol) + find(*args, **options).click + else + click_link_or_button(*args, **options) + end + end ## # From a29e377922be6f9a73533f86c348dc97c9152acc Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Wed, 1 May 2019 12:12:34 -0700 Subject: [PATCH 2/2] Add test for click_on --- lib/capybara/spec/session/click_on_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/capybara/spec/session/click_on_spec.rb diff --git a/lib/capybara/spec/session/click_on_spec.rb b/lib/capybara/spec/session/click_on_spec.rb new file mode 100644 index 000000000..86e8a8b54 --- /dev/null +++ b/lib/capybara/spec/session/click_on_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +Capybara::SpecHelper.spec '#click_on' do + it 'should default to being an alias for #click_link_or_button' do + @session.visit('/form') + @session.click_on('awe123') + expect(extract_results(@session)['first_name']).to eq('John') + end + + it 'should allow specifying a selector type' do + @session.visit('/form') + cbox = @session.click_on(:checkbox, 'form_terms_of_use') + expect(cbox).to be_checked + end +end