Commit 06646b11 06646b11cd3a724677061943e042662981699d22 by Nicolas Perriault

Added `reset` option to Casper#sendKeys()

This will empty current field value before sending keys to it.
1 parent 32d7f146
......@@ -1466,8 +1466,11 @@ The currently supported HTMLElements that can receive keyboard events from ``sen
Options
~~~~~~~
- ``(Boolean) keepFocus``:
- ``(Boolean) reset``:
When set to ``true``, this option will first empty the current field value. By default, it's set to ``false`` and ``sendKeys()`` will just append string to the current field value.
- ``(Boolean) keepFocus``:
``sendKeys()`` by default will remove the focus on text input fields, which will typically close autocomplete widgets. If you want to maintain focus, use the ``keepFocus`` option. For example, if using jQuery-UI, you can click on the first autocomplete suggestion using::
......
......@@ -1555,7 +1555,8 @@ Casper.prototype.sendKeys = function(selector, keys, options) {
"use strict";
this.checkStarted();
options = utils.mergeObjects({
eventType: 'keypress'
eventType: 'keypress',
reset: false
}, options || {});
var elemInfos = this.getElementInfo(selector),
tag = elemInfos.nodeName.toLowerCase(),
......@@ -1573,6 +1574,12 @@ Casper.prototype.sendKeys = function(selector, keys, options) {
isTextInput = true;
this.click(selector);
}
if (options.reset) {
this.evaluate(function(selector) {
__utils__.setField(__utils__.findOne(selector), '');
}, selector);
this.click(selector);
}
var modifiers = utils.computeModifier(options && options.modifiers,
this.page.event.modifier)
this.page.sendEvent(options.eventType, keys, null, null, modifiers);
......
......@@ -26,7 +26,7 @@
</form>
<form id="no-type-test-form" action="#" enctype="multipart/form-data">
<input name="notype">
<form>
</form>
<script>
(function () {
......
......@@ -59,3 +59,14 @@ if (utils.gteVersion(phantom.version, '1.9.0')) {
});
});
}
casper.test.begin('sendKeys() reset option', 1, function(test) {
casper.start('tests/site/form.html', function() {
this.sendKeys('textarea', 'foo');
this.sendKeys('textarea', 'bar', {reset: true});
var values = this.getFormValues('form[action="result.html"]');
test.assertEquals(values.content, "bar");
}).run(function() {
test.done();
});
});
......