Skip to content

Commit

Permalink
fix: move caret even with clear backspace (#2066)
Browse files Browse the repository at this point in the history
fix: move caret even with clear backspace
  • Loading branch information
cyrilchampier authored and twalpole committed Jul 16, 2018
1 parent d2326b9 commit 1a5f290
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/capybara/selenium/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/shared_selenium_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 1a5f290

Please sign in to comment.