From af89d99c9713c94b22e234882909761bb568d4e8 Mon Sep 17 00:00:00 2001 From: Mitchell Henke Date: Mon, 7 Aug 2023 16:42:29 -0500 Subject: [PATCH] Do not auto-submit forms if they have multiple inputs --- lib/capybara/rack_test/node.rb | 2 +- lib/capybara/spec/session/node_spec.rb | 6 ++++++ lib/capybara/spec/views/form.erb | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/capybara/rack_test/node.rb b/lib/capybara/rack_test/node.rb index 658797c89b..361041a7b7 100644 --- a/lib/capybara/rack_test/node.rb +++ b/lib/capybara/rack_test/node.rb @@ -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 diff --git a/lib/capybara/spec/session/node_spec.rb b/lib/capybara/spec/session/node_spec.rb index cc79f953b5..2b71fe15b1 100644 --- a/lib/capybara/spec/session/node_spec.rb +++ b/lib/capybara/spec/session/node_spec.rb @@ -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 diff --git a/lib/capybara/spec/views/form.erb b/lib/capybara/spec/views/form.erb index 4d9439e269..650366bf14 100644 --- a/lib/capybara/spec/views/form.erb +++ b/lib/capybara/spec/views/form.erb @@ -699,6 +699,11 @@ New line after and before textarea tag +
+ + +
+