Add :valid node filter to :field and :fillable_field selectors

This commit is contained in:
Thomas Walpole 2019-07-31 12:32:44 -07:00
parent 2541eb43d1
commit 5b0c2d6f3c
6 changed files with 33 additions and 3 deletions

View File

@ -31,6 +31,7 @@ Metrics/BlockLength:
- 'capybara.gemspec' - 'capybara.gemspec'
ExcludedMethods: ExcludedMethods:
- Capybara.add_selector - Capybara.add_selector
- Capybara::Selector::FilterSet.add
Metrics/AbcSize: Metrics/AbcSize:
Enabled: false Enabled: false

View File

@ -5,6 +5,7 @@ Release date: unreleased
* Allow forcing HTML5 or legacy dragging via the `:html5` option to `drag_to` when using Selenium with Chrome or Firefox * Allow forcing HTML5 or legacy dragging via the `:html5` option to `drag_to` when using Selenium with Chrome or Firefox
* Autodetection of drag type interprets not seeing the mousedown event as legacy. * Autodetection of drag type interprets not seeing the mousedown event as legacy.
* HTML5 form validation `:valid` node filter added to `:field` and `:fillable_field` selectors
# Version 3.27.0 # Version 3.27.0
Release date: 2019-07-28 Release date: 2019-07-28

View File

@ -34,6 +34,7 @@ require 'capybara/selector/definition'
# * :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)
# * :multiple (Boolean) - Match fields that accept multiple values # * :multiple (Boolean) - Match fields that accept multiple values
# * :valid (Boolean) - Match fields that are valid/invalid according to HTML5 form validation
# #
# * **:fieldset** - Select fieldset elements # * **:fieldset** - Select fieldset elements
# * Locator: Matches id, {Capybara.configure test_id}, or contents of wrapped legend # * Locator: Matches id, {Capybara.configure test_id}, or contents of wrapped legend
@ -72,6 +73,7 @@ require 'capybara/selector/definition'
# * :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)
# * :multiple (Boolean) - Match fields that accept multiple values # * :multiple (Boolean) - Match fields that accept multiple values
# * :valid (Boolean) - Match fields that are valid/invalid according to HTML5 form validation
# #
# * **: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
@ -166,6 +168,7 @@ Capybara::Selector::FilterSet.add(:_field) do
node_filter(:checked, :boolean) { |node, value| !(value ^ node.checked?) } node_filter(:checked, :boolean) { |node, value| !(value ^ node.checked?) }
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 }
expression_filter(:name) { |xpath, val| xpath[XPath.attr(:name) == val] } expression_filter(:name) { |xpath, val| xpath[XPath.attr(:name) == val] }
expression_filter(:placeholder) { |xpath, val| xpath[XPath.attr(:placeholder) == val] } expression_filter(:placeholder) { |xpath, val| xpath[XPath.attr(:placeholder) == val] }
@ -182,12 +185,14 @@ Capybara::Selector::FilterSet.add(:_field) do
desc desc
end end
describe(:node_filters) do |checked: nil, unchecked: nil, disabled: nil, **| describe(:node_filters) do |checked: nil, unchecked: nil, disabled: nil, valid: nil, **|
desc, states = +'', [] desc, states = +'', []
states << 'checked' if checked || (unchecked == false) states << 'checked' if checked || (unchecked == false)
states << 'not checked' if unchecked || (checked == false) states << 'not checked' if unchecked || (checked == false)
states << 'disabled' if disabled == true states << 'disabled' if disabled == true
desc << " that is #{states.join(' and ')}" unless states.empty? desc << " that is #{states.join(' and ')}" unless states.empty?
desc << ' that is valid' if valid == true
desc << ' that is invalid' if valid == false
desc desc
end end
end end

View File

@ -18,7 +18,7 @@ Capybara.add_selector(:fillable_field, locator_type: [String, Symbol]) do
end end
end end
filter_set(:_field, %i[disabled multiple name placeholder]) filter_set(:_field, %i[disabled multiple name placeholder valid])
node_filter(:with) do |node, with| node_filter(:with) do |node, with|
val = node.value val = node.value

View File

@ -93,6 +93,24 @@ Capybara::SpecHelper.spec '#has_field' do
expect(@session).not_to have_field('Html5 Multiple Email', multiple: false) expect(@session).not_to have_field('Html5 Multiple Email', multiple: false)
end end
end end
context 'with valid', requires: [:js] do
it 'should be true if field is valid' do
@session.fill_in 'required', with: 'something'
@session.fill_in 'length', with: 'abcd'
expect(@session).to have_field('required', valid: true)
expect(@session).to have_field('length', valid: true)
end
it 'should be false if field is invalid' do
expect(@session).not_to have_field('required', valid: true)
expect(@session).to have_field('required', valid: false)
@session.fill_in 'length', with: 'abc'
expect(@session).not_to have_field('length', valid: true)
end
end
end end
Capybara::SpecHelper.spec '#has_no_field' do Capybara::SpecHelper.spec '#has_no_field' do

View File

@ -1,7 +1,7 @@
<h1>Form</h1> <h1>Form</h1>
<form action="/form" method="post"> <form action="/form" method="post" novalidate>
<p> <p>
<label for="form_title">Title</label> <label for="form_title">Title</label>
@ -460,6 +460,11 @@ New line after and before textarea tag
<input id="readonly" name="form[readonly_test]" readonly/> <input id="readonly" name="form[readonly_test]" readonly/>
<input id="not_readonly" name="form[readonly_test]" /> <input id="not_readonly" name="form[readonly_test]" />
</p> </p>
<p>
<input id="required" name="form[required]" required />
<input id="length" name="form[length]" minlength="4" maxlength="4" />
</p>
</form> </form>
<input type="text" name="form[outside_input]" value="outside_input" form="form1"/> <input type="text" name="form[outside_input]" value="outside_input" form="form1"/>