Support Regexp for :name and :placeholder in selectors that import fi… (#2283)

* Support Regexp for :name and :placeholder in selectors that import filters from :_field filter set
This commit is contained in:
Thomas Walpole 2019-12-08 09:21:10 -05:00 committed by GitHub
parent bfe0a72c39
commit d7ddb66a0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 19 deletions

View File

@ -30,8 +30,8 @@ require 'capybara/selector/definition'
# * Locator: Matches against the id, {Capybara.configure test_id} attribute, name, placeholder, or
# associated label text
# * Filters:
# * :name (String) - Matches the name attribute
# * :placeholder (String) - Matches the placeholder attribute
# * :name (String, Regexp) - Matches the name attribute
# * :placeholder (String, Regexp) - Matches the placeholder attribute
# * :type (String) - Matches the type attribute of the field or element type for 'textarea' and 'select'
# * :readonly (Boolean) - Match on the element being readonly
# * :with (String, Regexp) - Matches the current value of the field
@ -58,7 +58,7 @@ require 'capybara/selector/definition'
# * **:button** - Find buttons ( input [of type submit, reset, image, button] or button elements )
# * Locator: Matches the id, {Capybara.configure test_id} attribute, name, 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:
# * :name (String) - Matches the name attribute
# * :name (String, Regexp) - Matches the name attribute
# * :title (String) - Matches the title attribute
# * :value (String) - Matches the value of an input button
# * :type (String) - Matches the type attribute
@ -72,8 +72,8 @@ require 'capybara/selector/definition'
# * **:fillable_field** - Find text fillable fields ( textarea, input [not of type submit, image, radio, checkbox, hidden, file] )
# * Locator: Matches against the id, {Capybara.configure test_id} attribute, name, placeholder, or associated label text
# * Filters:
# * :name (String) - Matches the name attribute
# * :placeholder (String) - Matches the placeholder attribute
# * :name (String, Regexp) - Matches the name attribute
# * :placeholder (String, Regexp) - Matches the placeholder attribute
# * :with (String, Regexp) - Matches the current value of the field
# * :type (String) - Matches the type attribute of the field or element type for 'textarea'
# * :disabled (Boolean, :all) - Match disabled field? (Default: false)
@ -83,7 +83,7 @@ require 'capybara/selector/definition'
# * **:radio_button** - Find radio buttons
# * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text
# * Filters:
# * :name (String) - Matches the name attribute
# * :name (String, Regexp) - Matches the name attribute
# * :checked (Boolean) - Match checked fields?
# * :unchecked (Boolean) - Match unchecked fields?
# * :disabled (Boolean, :all) - Match disabled field? (Default: false)
@ -93,18 +93,18 @@ require 'capybara/selector/definition'
# * **:checkbox** - Find checkboxes
# * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text
# * Filters:
# * :name (String) - Matches the name attribute
# * :name (String, Regexp) - Matches the name attribute
# * :checked (Boolean) - Match checked fields?
# * :unchecked (Boolean) - Match unchecked fields?
# * :disabled (Boolean, :all) - Match disabled field? (Default: false)
# * :option (String, Regexp) - Match the current value
# * :with - Alias of :option
# * :with (String, Regexp) - Match the current value
# * :option - Alias of :with
#
# * **:select** - Find select elements
# * Locator: Match id, {Capybara.configure test_id} attribute, name, placeholder, or associated label text
# * Filters:
# * :name (String) - Matches the name attribute
# * :placeholder (String) - Matches the placeholder attribute
# * :name (String, Regexp) - Matches the name attribute
# * :placeholder (String, Placeholder) - Matches the placeholder attribute
# * :disabled (Boolean, :all) - Match disabled field? (Default: false)
# * :multiple (Boolean) - Match fields that accept multiple values
# * :options (Array<String>) - Exact match options
@ -122,8 +122,8 @@ require 'capybara/selector/definition'
# * Locator: Matches against the id, {Capybara.configure test_id} attribute, name,
# placeholder, or associated label text
# * Filters:
# * :name (String) - Matches the name attribute
# * :placeholder (String) - Matches the placeholder attribute
# * :name (String, Regexp) - Matches the name attribute
# * :placeholder (String, Regexp) - Matches the placeholder attribute
# * :disabled (Boolean, :all) - Match disabled field? (Default: false)
# * :options (Array<String>) - Exact match options
# * :with_options (Array<String>) - Partial match options
@ -136,7 +136,7 @@ require 'capybara/selector/definition'
# * **:file_field** - Find file input elements
# * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text
# * Filters:
# * :name (String) - Matches the name attribute
# * :name (String, Regexp) - Matches the name attribute
# * :disabled (Boolean, :all) - Match disabled field? (Default: false)
# * :multiple (Boolean) - Match field that accepts multiple values
#
@ -174,9 +174,15 @@ Capybara::Selector::FilterSet.add(:_field) do
node_filter(:unchecked, :boolean) { |node, value| (value ^ node.checked?) }
node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }
node_filter(:valid, :boolean) { |node, value| node.evaluate_script('this.validity.valid') == value }
node_filter(:name) { |node, value| !value.is_a?(Regexp) || value.match?(node[:name]) }
node_filter(:placeholder) { |node, value| !value.is_a?(Regexp) || value.match?(node[:placeholder]) }
expression_filter(:name) { |xpath, val| xpath[XPath.attr(:name) == val] }
expression_filter(:placeholder) { |xpath, val| xpath[XPath.attr(:placeholder) == val] }
expression_filter(:name) do |xpath, val|
builder(xpath).add_attribute_conditions(name: val)
end
expression_filter(:placeholder) do |xpath, val|
builder(xpath).add_attribute_conditions(placeholder: val)
end
expression_filter(:disabled) { |xpath, val| val ? xpath : xpath[~XPath.attr(:disabled)] }
expression_filter(:multiple) { |xpath, val| xpath[val ? XPath.attr(:multiple) : ~XPath.attr(:multiple)] }

View File

@ -26,7 +26,7 @@ Capybara.add_selector(:button, locator_type: [String, Symbol]) do
image_btn_xpath = image_btn_xpath[alt_matches]
end
%i[value title type name].inject(input_btn_xpath.union(btn_xpath).union(image_btn_xpath)) do |memo, ef|
%i[value title type].inject(input_btn_xpath.union(btn_xpath).union(image_btn_xpath)) do |memo, ef|
memo[find_by_attr(ef, options[ef])]
end
end
@ -34,6 +34,11 @@ Capybara.add_selector(:button, locator_type: [String, Symbol]) do
node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }
expression_filter(:disabled) { |xpath, val| val ? xpath : xpath[~XPath.attr(:disabled)] }
node_filter(:name) { |node, value| !value.is_a?(Regexp) || value.match?(node[:name]) }
expression_filter(:name) do |xpath, val|
builder(xpath).add_attribute_conditions(name: val)
end
describe_expression_filters do |disabled: nil, **options|
desc = +''
desc << ' that is not disabled' if disabled == false

View File

@ -186,6 +186,11 @@ Capybara::SpecHelper.spec '#click_button' do
@session.click_button(name: 'form[awesome]')
expect(extract_results(@session)['first_name']).to eq('John')
end
it 'should submit by specific button name regex' do
@session.click_button(name: /form\[awes.*\]/)
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

View File

@ -67,14 +67,22 @@ Capybara::SpecHelper.spec Capybara::Selector do
end
end
it 'can find specifically by name' do
it 'can find specifically by name string' do
expect(@session.find(:field, name: 'form[other_title]')['id']).to eq 'form_other_title'
end
it 'can find specifically by placeholder' do
it 'can find specifically by name regex' do
expect(@session.find(:field, name: /form\[other_.*\]/)['id']).to eq 'form_other_title'
end
it 'can find specifically by placeholder string' do
expect(@session.find(:field, placeholder: 'FirstName')['id']).to eq 'form_first_name'
end
it 'can find specifically by placeholder regex' do
expect(@session.find(:field, placeholder: /FirstN.*/)['id']).to eq 'form_first_name'
end
it 'can find by type' do
expect(@session.find(:field, 'Confusion', type: 'checkbox')['id']).to eq 'confusion_checkbox'
expect(@session.find(:field, 'Confusion', type: 'text')['id']).to eq 'confusion_text'