From f280fddc4599f843f91a12ab5921ee0630ba1c3a Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 16 Nov 2012 23:07:50 +0100 Subject: [PATCH] Properly clear input by moving to the end before backspacing in Selenium driver --- lib/capybara/selenium/node.rb | 3 ++- lib/capybara/spec/session/node_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/capybara/selenium/node.rb b/lib/capybara/selenium/node.rb index 46a33fc8..31c29692 100644 --- a/lib/capybara/selenium/node.rb +++ b/lib/capybara/selenium/node.rb @@ -32,7 +32,8 @@ class Capybara::Selenium::Node < Capybara::Driver::Node path_names = value.to_s.empty? ? [] : value native.send_keys(*path_names) elsif tag_name == 'textarea' or tag_name == 'input' - native.send_keys(("\b" * native[:value].size) + value.to_s) + chars = native[:value].size + native.send_keys(([:right] * chars) + ([:backspace] * chars) + [ value.to_s ]) end end diff --git a/lib/capybara/spec/session/node_spec.rb b/lib/capybara/spec/session/node_spec.rb index 3eae8457..30b4a8bc 100644 --- a/lib/capybara/spec/session/node_spec.rb +++ b/lib/capybara/spec/session/node_spec.rb @@ -65,6 +65,12 @@ Capybara::SpecHelper.spec "node" do @session.first('//input').set('gorilla') @session.first('//input').value.should == 'gorilla' end + + it "should fill the field even if the caret was not at the end", :requires => [:js] do + @session.execute_script("var el = document.getElementById('test_field'); el.focus(); el.setSelectionRange(0, 0);") + @session.first('//input').set('') + @session.first('//input').value.should == '' + end end describe "#tag_name" do