Add `name` attribute to button selector matching

This commit is contained in:
Thomas Walpole 2019-04-11 10:48:39 -07:00
parent cb56046153
commit c7c22789b7
5 changed files with 37 additions and 8 deletions

View File

@ -45,7 +45,7 @@ module Capybara
#
# Finds a button on the page and clicks it.
# This can be any \<input> element of type submit, reset, image, button or it can be a
# \<button> element. All buttons can be found by their id, Capybara.test_id attribute, value, or title. \<button> elements can also be found
# \<button> element. All buttons can be found by their id, name, Capybara.test_id attribute, value, or title. \<button> elements can also be found
# by their text content, and image \<input> elements by their alt attribute
#
# @overload click_button([locator], **options)

View File

@ -154,11 +154,11 @@ module Capybara
#
# Find a button on the page.
# This can be any \<input> element of type submit, reset, image, button or it can be a
# \<button> element. All buttons can be found by their id, Capbyara.test_id attribute, value, or title. \<button> elements can also be found
# \<button> element. All buttons can be found by their id, name, Capbyara.test_id attribute, value, or title. \<button> elements can also be found
# by their text content, and image \<input> elements by their alt attribute
#
# @overload find_button([locator], **options)
# @param [String] locator id, Capybara.test_id attribute, value, title, text content, alt of image
# @param [String] locator id, name, Capybara.test_id attribute, value, title, text content, alt of image
#
# @overload find_button(**options)
#
@ -169,8 +169,9 @@ module Capybara
# * false - only finds an enabled button
# * :all - finds either an enabled or disabled button
# @option options [String, Regexp] id Match buttons with the id provided
# @option options [String] title Match buttons with the title provided
# @option options [String] value Match buttons with the value provided
# @option options [String] name Match buttons with the name provided
# @option options [String] title Match buttons with the title provided
# @option options [String] value Match buttons with the value provided
# @option options [String, Array<String>, Regexp] class Match buttons that match the class(es) provided
# @return [Capybara::Node::Element] The found element
#

View File

@ -143,14 +143,14 @@ Capybara.add_selector(:link, locator_type: [String, Symbol]) do
end
Capybara.add_selector(:button, locator_type: [String, Symbol]) do
xpath(:value, :title, :type) do |locator, **options|
xpath(:value, :title, :type, :name) do |locator, **options|
input_btn_xpath = XPath.descendant(:input)[XPath.attr(:type).one_of('submit', 'reset', 'image', 'button')]
btn_xpath = XPath.descendant(:button)
image_btn_xpath = XPath.descendant(:input)[XPath.attr(:type) == 'image']
unless locator.nil?
locator = locator.to_s
locator_matchers = XPath.attr(:id).equals(locator) | XPath.attr(:value).is(locator) | XPath.attr(:title).is(locator)
locator_matchers = XPath.attr(:id).equals(locator) | XPath.attr(:name).equals(locator) | XPath.attr(:value).is(locator) | XPath.attr(:title).is(locator)
locator_matchers |= XPath.attr(:'aria-label').is(locator) if enable_aria_label
locator_matchers |= XPath.attr(test_id) == locator if test_id
@ -163,7 +163,7 @@ Capybara.add_selector(:button, locator_type: [String, Symbol]) do
image_btn_xpath = image_btn_xpath[alt_matches]
end
%i[value title type].inject(input_btn_xpath.union(btn_xpath).union(image_btn_xpath)) do |memo, ef|
%i[value title type name].inject(input_btn_xpath.union(btn_xpath).union(image_btn_xpath)) do |memo, ef|
memo[find_by_attr(ef, options[ef])]
end
end

View File

@ -59,6 +59,7 @@ module Capybara
# * Locator: Matches the id, Capybara.test_id attribute, value, or title attributes, string content of a button, or the alt attribute of an image type button or of a descendant image of a button
# * Filters:
# * :id (String, Regexp, XPath::Expression) — Matches the id attribute
# * :name (String) - Matches the name attribute
# * :title (String) — Matches the title attribute
# * :class (String, Array<String>, Regexp, XPath::Expression) — Matches the class(es) provided
# * :value (String) — Matches the value of an input button

View File

@ -162,6 +162,11 @@ Capybara::SpecHelper.spec '#click_button' do
expect(extract_results(@session)['first_name']).to eq('John')
end
it 'should submit by specific button id' do
@session.click_button(id: 'awe123')
expect(extract_results(@session)['first_name']).to eq('John')
end
it 'should submit by button title' do
@session.click_button('What an Awesome Button')
expect(extract_results(@session)['first_name']).to eq('John')
@ -171,6 +176,16 @@ Capybara::SpecHelper.spec '#click_button' do
@session.click_button('What an Awesome')
expect(extract_results(@session)['first_name']).to eq('John')
end
it 'should submit by button name' do
@session.click_button('form[awesome]')
expect(extract_results(@session)['first_name']).to eq('John')
end
it 'should submit by specific button name' do
@session.click_button(name: 'form[awesome]')
expect(extract_results(@session)['first_name']).to eq('John')
end
end
context 'with fields associated with the form using the form attribute', requires: [:form_attribute] do
@ -314,6 +329,18 @@ Capybara::SpecHelper.spec '#click_button' do
end
end
context 'with name given on a button defined by <button> tag' do
it 'should submit the associated form when name is locator' do
@session.click_button('form[no_value]')
expect(extract_results(@session)['first_name']).to eq('John')
end
it 'should submit the associated form when name is specific' do
@session.click_button(name: 'form[no_value]')
expect(extract_results(@session)['first_name']).to eq('John')
end
end
context 'with value given on a button defined by <button> tag' do
it 'should submit the associated form' do
@session.click_button('click_me')