From eb0dcd561362dd605428256e84205ccd8a2ae496 Mon Sep 17 00:00:00 2001 From: Girts Silis Date: Thu, 7 Sep 2017 11:58:14 -0500 Subject: [PATCH] [fix] selenium set_text passing in empty string with options If an empty value if passed in with a clear option, it seems that the caller's intent is for the option to be honored regardless of the value to be set in the field. Just passing in a blank value would behave as it did before. Updated spec to also attach an input event listener to `with_change_event` --- lib/capybara/selenium/node.rb | 2 +- lib/capybara/spec/public/test.js | 3 +++ spec/shared_selenium_session.rb | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/capybara/selenium/node.rb b/lib/capybara/selenium/node.rb index f0879305..4cf780dc 100644 --- a/lib/capybara/selenium/node.rb +++ b/lib/capybara/selenium/node.rb @@ -209,7 +209,7 @@ private def set_text(value, options) if readonly? warn "Attempt to set readonly element with value: #{value} \n *This will raise an exception in a future version of Capybara" - elsif value.to_s.empty? + elsif value.to_s.empty? && options[:clear].nil? native.clear else if options[:clear] == :backspace diff --git a/lib/capybara/spec/public/test.js b/lib/capybara/spec/public/test.js index 5080fd99..84ed47a3 100644 --- a/lib/capybara/spec/public/test.js +++ b/lib/capybara/spec/public/test.js @@ -37,6 +37,9 @@ $(function() { $('#with_change_event').change(function() { $('body').append($('

').text(this.value)); }); + $('#with_change_event').on('input', function() { + $('body').append($('

').text(this.value)); + }); $('#checkbox_with_event').click(function() { $('body').append('

Checkbox event triggered

'); }); diff --git a/spec/shared_selenium_session.rb b/spec/shared_selenium_session.rb index f070ff41..87d62988 100644 --- a/spec/shared_selenium_session.rb +++ b/spec/shared_selenium_session.rb @@ -71,6 +71,16 @@ RSpec.shared_examples "Capybara::Session" do |session, mode| end end + context '#fill_in_with empty string and no options' do + it 'should trigger change when clearing a field' do + @session.visit('/with_js') + @session.fill_in('with_change_event', with: '') + # click outside the field to trigger the change event + @session.find(:css, 'body').click + expect(@session).to have_selector(:css, '.change_event_triggered', match: :one) + end + end + context "#fill_in with { :clear => :backspace } fill_option", requires: [:js] do it 'should fill in a field, replacing an existing value' do @session.visit('/form') @@ -96,6 +106,15 @@ RSpec.shared_examples "Capybara::Session" do |session, mode| @session.find(:css, 'body').click expect(@session).to have_selector(:css, '.change_event_triggered', match: :one) end + + it 'should trigger input event field_value.length times' do + @session.visit('/with_js') + @session.fill_in('with_change_event', with: '', + fill_options: { :clear => :backspace }) + # click outside the field to trigger the change event + @session.find(:css, 'body').click + expect(@session).to have_xpath('//p[@class="input_event_triggered"]', count: 13) + end end context "#fill_in with { clear: :none } fill_options" do