Skip to content

Commit

Permalink
Do not auto-submit forms if they have multiple inputs (#2696)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellhenke committed Aug 10, 2023
1 parent 08f5d47 commit abf976e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/capybara/rack_test/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def set_input(value) # rubocop:disable Naming/AccessorMethodName
native.remove
else
value.to_s.tap do |set_value|
if set_value.end_with?("\n") && form&.css('input, textarea')&.count
if set_value.end_with?("\n") && form&.css('input, textarea')&.count == 1
native['value'] = set_value.to_s.chop
Capybara::RackTest::Form.new(driver, form).submit(self)
else
Expand Down
6 changes: 6 additions & 0 deletions lib/capybara/spec/session/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@
@session.find(:css, '#single_input').set("my entry\n")
expect(extract_results(@session)['single_input']).to eq('my entry')
end

it 'should not submit single text input forms if ended with \n and has multiple values' do
@session.visit('/form')
@session.find(:css, '#two_input_1').set("my entry\n")
expect(@session.find(:css, '#two_input_1').value).to eq("my entry\n")
end
end

describe '#tag_name' do
Expand Down
5 changes: 5 additions & 0 deletions lib/capybara/spec/views/form.erb
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@ New line after and before textarea tag
<input type="text" name="form[single_input]" id="single_input"/>
</form>

<form id="two_input_form" action="/form" method="post">
<input type="text" name="form[two_input_1]" id="two_input_1"/>
<input type="text" name="form[two_input_2]" id="two_input_2"/>
</form>

<label>Confusion
<input type="checkbox" id="confusion_checkbox" class="confusion-checkbox confusion"/>
</label>
Expand Down

0 comments on commit abf976e

Please sign in to comment.