From a43fe704aa6adb134befc13c30f8ef63d7af00f3 Mon Sep 17 00:00:00 2001 From: Daniel van Hoesel Date: Wed, 24 Oct 2012 10:38:54 +0200 Subject: [PATCH] clearing input before setting a new value by sending a backspace Conflicts: README.md --- README.md | 7 +++++++ lib/capybara/poltergeist/client/compiled/node.js | 1 + lib/capybara/poltergeist/client/node.coffee | 3 +++ spec/integration/session_spec.rb | 10 ++++++++-- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fefd9cf..8bd6f23 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,13 @@ Include as much information as possible. For example: ## Changes ## +### 1.0.2 ### + +#### Bug fixes #### + +* Clearing the value before setting a new value by sending a backspace. + This fixes the issue that you can't set an empty value. [Issue #184] + ### 1.0.1 ### #### Bug fixes #### diff --git a/lib/capybara/poltergeist/client/compiled/node.js b/lib/capybara/poltergeist/client/compiled/node.js index 0b84ed7..79b0e55 100644 --- a/lib/capybara/poltergeist/client/compiled/node.js +++ b/lib/capybara/poltergeist/client/compiled/node.js @@ -69,6 +69,7 @@ Poltergeist.Node = (function() { Node.prototype.set = function(value) { this.focusAndHighlight(); + this.page.sendEvent('keypress', 16777219); this.page.sendEvent('keypress', value.toString()); return this.blur(); }; diff --git a/lib/capybara/poltergeist/client/node.coffee b/lib/capybara/poltergeist/client/node.coffee index 8344769..08a4dde 100644 --- a/lib/capybara/poltergeist/client/node.coffee +++ b/lib/capybara/poltergeist/client/node.coffee @@ -54,5 +54,8 @@ class Poltergeist.Node set: (value) -> this.focusAndHighlight() + # Sending backspace to clear the input + # keycode from: https://github.com/ariya/phantomjs/commit/cab2635e66d74b7e665c44400b8b20a8f225153a#L0R370 + @page.sendEvent('keypress', 16777219) @page.sendEvent('keypress', value.toString()) this.blur() diff --git a/spec/integration/session_spec.rb b/spec/integration/session_spec.rb index 960b3e9..6738575 100644 --- a/spec/integration/session_spec.rb +++ b/spec/integration/session_spec.rb @@ -120,11 +120,11 @@ describe Capybara::Session do end it 'fires the keydown event' do - @session.find(:css, '#changes_on_keydown').text.should == "6" + @session.find(:css, '#changes_on_keydown').text.should == "7" end it 'fires the keyup event' do - @session.find(:css, '#changes_on_keyup').text.should == "6" + @session.find(:css, '#changes_on_keyup').text.should == "7" end it 'fires the keypress event' do @@ -146,6 +146,12 @@ describe Capybara::Session do it "fires the keyup event after the value is updated" do @session.find(:css, '#value_on_keyup').text.should == "Hello!" end + + it "clears the input before setting the new value" do + element = @session.find(:css, '#change_me') + element.set "" + element.value.should == "" + end end it 'has no trouble clicking elements when the size of a document changes' do