1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

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 # * Locator: Matches against the id, {Capybara.configure test_id} attribute, name, placeholder, or
# associated label text # associated label text
# * Filters: # * Filters:
# * :name (String) - Matches the name attribute # * :name (String, Regexp) - Matches the name attribute
# * :placeholder (String) - Matches the placeholder attribute # * :placeholder (String, Regexp) - Matches the placeholder attribute
# * :type (String) - Matches the type attribute of the field or element type for 'textarea' and 'select' # * :type (String) - Matches the type attribute of the field or element type for 'textarea' and 'select'
# * :readonly (Boolean) - Match on the element being readonly # * :readonly (Boolean) - Match on the element being readonly
# * :with (String, Regexp) - Matches the current value of the field # * :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 ) # * **: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 # * 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: # * Filters:
# * :name (String) - Matches the name attribute # * :name (String, Regexp) - Matches the name attribute
# * :title (String) - Matches the title attribute # * :title (String) - Matches the title attribute
# * :value (String) - Matches the value of an input button # * :value (String) - Matches the value of an input button
# * :type (String) - Matches the type attribute # * :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] ) # * **: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 # * Locator: Matches against the id, {Capybara.configure test_id} attribute, name, placeholder, or associated label text
# * Filters: # * Filters:
# * :name (String) - Matches the name attribute # * :name (String, Regexp) - Matches the name attribute
# * :placeholder (String) - Matches the placeholder attribute # * :placeholder (String, Regexp) - Matches the placeholder attribute
# * :with (String, Regexp) - Matches the current value of the field # * :with (String, Regexp) - Matches the current value of the field
# * :type (String) - Matches the type attribute of the field or element type for 'textarea' # * :type (String) - Matches the type attribute of the field or element type for 'textarea'
# * :disabled (Boolean, :all) - Match disabled field? (Default: false) # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
@ -83,7 +83,7 @@ require 'capybara/selector/definition'
# * **:radio_button** - Find radio buttons # * **:radio_button** - Find radio buttons
# * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text # * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text
# * Filters: # * Filters:
# * :name (String) - Matches the name attribute # * :name (String, Regexp) - Matches the name attribute
# * :checked (Boolean) - Match checked fields? # * :checked (Boolean) - Match checked fields?
# * :unchecked (Boolean) - Match unchecked fields? # * :unchecked (Boolean) - Match unchecked fields?
# * :disabled (Boolean, :all) - Match disabled field? (Default: false) # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
@ -93,18 +93,18 @@ require 'capybara/selector/definition'
# * **:checkbox** - Find checkboxes # * **:checkbox** - Find checkboxes
# * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text # * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text
# * Filters: # * Filters:
# * :name (String) - Matches the name attribute # * :name (String, Regexp) - Matches the name attribute
# * :checked (Boolean) - Match checked fields? # * :checked (Boolean) - Match checked fields?
# * :unchecked (Boolean) - Match unchecked fields? # * :unchecked (Boolean) - Match unchecked fields?
# * :disabled (Boolean, :all) - Match disabled field? (Default: false) # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
# * :option (String, Regexp) - Match the current value # * :with (String, Regexp) - Match the current value
# * :with - Alias of :option # * :option - Alias of :with
# #
# * **:select** - Find select elements # * **:select** - Find select elements
# * Locator: Match id, {Capybara.configure test_id} attribute, name, placeholder, or associated label text # * Locator: Match id, {Capybara.configure test_id} attribute, name, placeholder, or associated label text
# * Filters: # * Filters:
# * :name (String) - Matches the name attribute # * :name (String, Regexp) - Matches the name attribute
# * :placeholder (String) - Matches the placeholder attribute # * :placeholder (String, Placeholder) - Matches the placeholder attribute
# * :disabled (Boolean, :all) - Match disabled field? (Default: false) # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
# * :multiple (Boolean) - Match fields that accept multiple values # * :multiple (Boolean) - Match fields that accept multiple values
# * :options (Array<String>) - Exact match options # * :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, # * Locator: Matches against the id, {Capybara.configure test_id} attribute, name,
# placeholder, or associated label text # placeholder, or associated label text
# * Filters: # * Filters:
# * :name (String) - Matches the name attribute # * :name (String, Regexp) - Matches the name attribute
# * :placeholder (String) - Matches the placeholder attribute # * :placeholder (String, Regexp) - Matches the placeholder attribute
# * :disabled (Boolean, :all) - Match disabled field? (Default: false) # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
# * :options (Array<String>) - Exact match options # * :options (Array<String>) - Exact match options
# * :with_options (Array<String>) - Partial match options # * :with_options (Array<String>) - Partial match options
@ -136,7 +136,7 @@ require 'capybara/selector/definition'
# * **:file_field** - Find file input elements # * **:file_field** - Find file input elements
# * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text # * Locator: Match id, {Capybara.configure test_id} attribute, name, or associated label text
# * Filters: # * Filters:
# * :name (String) - Matches the name attribute # * :name (String, Regexp) - Matches the name attribute
# * :disabled (Boolean, :all) - Match disabled field? (Default: false) # * :disabled (Boolean, :all) - Match disabled field? (Default: false)
# * :multiple (Boolean) - Match field that accepts multiple values # * :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(:unchecked, :boolean) { |node, value| (value ^ node.checked?) }
node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) } 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(: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(:name) do |xpath, val|
expression_filter(:placeholder) { |xpath, val| xpath[XPath.attr(:placeholder) == 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(:disabled) { |xpath, val| val ? xpath : xpath[~XPath.attr(:disabled)] }
expression_filter(:multiple) { |xpath, val| xpath[val ? XPath.attr(:multiple) : ~XPath.attr(:multiple)] } 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] image_btn_xpath = image_btn_xpath[alt_matches]
end 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])] memo[find_by_attr(ef, options[ef])]
end end
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?) } 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)] } 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| describe_expression_filters do |disabled: nil, **options|
desc = +'' desc = +''
desc << ' that is not disabled' if disabled == false 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]') @session.click_button(name: 'form[awesome]')
expect(extract_results(@session)['first_name']).to eq('John') expect(extract_results(@session)['first_name']).to eq('John')
end 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 end
context 'with fields associated with the form using the form attribute', requires: [:form_attribute] do 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
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' expect(@session.find(:field, name: 'form[other_title]')['id']).to eq 'form_other_title'
end 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' expect(@session.find(:field, placeholder: 'FirstName')['id']).to eq 'form_first_name'
end 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 it 'can find by type' do
expect(@session.find(:field, 'Confusion', type: 'checkbox')['id']).to eq 'confusion_checkbox' expect(@session.find(:field, 'Confusion', type: 'checkbox')['id']).to eq 'confusion_checkbox'
expect(@session.find(:field, 'Confusion', type: 'text')['id']).to eq 'confusion_text' expect(@session.find(:field, 'Confusion', type: 'text')['id']).to eq 'confusion_text'