Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand click_on beyond just an alias of click_link_or_button #2182

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/capybara/node/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
#
Expand Down
15 changes: 15 additions & 0 deletions lib/capybara/spec/session/click_on_spec.rb
Original file line number Diff line number Diff line change
@@ -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