From 1a5f290ee174feb504481b7cab818053075e4f7b Mon Sep 17 00:00:00 2001 From: Champier Cyril Date: Mon, 16 Jul 2018 18:19:35 +0200 Subject: [PATCH] fix: move caret even with clear backspace (#2066) fix: move caret even with clear backspace --- lib/capybara/selenium/node.rb | 2 +- spec/shared_selenium_session.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/capybara/selenium/node.rb b/lib/capybara/selenium/node.rb index cc17c87da..718e66b1b 100644 --- a/lib/capybara/selenium/node.rb +++ b/lib/capybara/selenium/node.rb @@ -205,7 +205,7 @@ def set_text(value, clear: nil, **_unused) elsif clear == :backspace # Clear field by sending the correct number of backspace keys. backspaces = [:backspace] * self.value.to_s.length - native.send_keys(*(backspaces + [value.to_s])) + native.send_keys(*([:end] + backspaces + [value.to_s])) elsif clear == :none native.send_keys(value.to_s) elsif clear.is_a? Array diff --git a/spec/shared_selenium_session.rb b/spec/shared_selenium_session.rb index 9e3347f20..138211841 100644 --- a/spec/shared_selenium_session.rb +++ b/spec/shared_selenium_session.rb @@ -93,6 +93,21 @@ expect(session.find(:fillable_field, 'form_first_name').value).to eq('Harry') end + it 'should fill in a field, replacing an existing value, even with caret position' do + session.visit('/form') + el = session.find(:css, '#form_first_name') + move_caret_to_the_beginning_js = <<-JS + this.focus(); + this.setSelectionRange(0, 0); + JS + el.execute_script(move_caret_to_the_beginning_js) + + session.fill_in('form_first_name', + with: 'Harry', + fill_options: { clear: :backspace }) + expect(session.find(:fillable_field, 'form_first_name').value).to eq('Harry') + end + it 'should fill in if the option is set via global option' do Capybara.default_set_options = { clear: :backspace } session.visit('/form')