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 ...@@ -1466,8 +1466,11 @@ The currently supported HTMLElements that can receive keyboard events from ``sen
1466 Options 1466 Options
1467 ~~~~~~~ 1467 ~~~~~~~
1468 1468
1469 - ``(Boolean) keepFocus``: 1469 - ``(Boolean) reset``:
1470
1471 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.
1470 1472
1473 - ``(Boolean) keepFocus``:
1471 1474
1472 ``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:: 1475 ``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::
1473 1476
......
...@@ -1555,7 +1555,8 @@ Casper.prototype.sendKeys = function(selector, keys, options) { ...@@ -1555,7 +1555,8 @@ Casper.prototype.sendKeys = function(selector, keys, options) {
1555 "use strict"; 1555 "use strict";
1556 this.checkStarted(); 1556 this.checkStarted();
1557 options = utils.mergeObjects({ 1557 options = utils.mergeObjects({
1558 eventType: 'keypress' 1558 eventType: 'keypress',
1559 reset: false
1559 }, options || {}); 1560 }, options || {});
1560 var elemInfos = this.getElementInfo(selector), 1561 var elemInfos = this.getElementInfo(selector),
1561 tag = elemInfos.nodeName.toLowerCase(), 1562 tag = elemInfos.nodeName.toLowerCase(),
...@@ -1573,6 +1574,12 @@ Casper.prototype.sendKeys = function(selector, keys, options) { ...@@ -1573,6 +1574,12 @@ Casper.prototype.sendKeys = function(selector, keys, options) {
1573 isTextInput = true; 1574 isTextInput = true;
1574 this.click(selector); 1575 this.click(selector);
1575 } 1576 }
1577 if (options.reset) {
1578 this.evaluate(function(selector) {
1579 __utils__.setField(__utils__.findOne(selector), '');
1580 }, selector);
1581 this.click(selector);
1582 }
1576 var modifiers = utils.computeModifier(options && options.modifiers, 1583 var modifiers = utils.computeModifier(options && options.modifiers,
1577 this.page.event.modifier) 1584 this.page.event.modifier)
1578 this.page.sendEvent(options.eventType, keys, null, null, modifiers); 1585 this.page.sendEvent(options.eventType, keys, null, null, modifiers);
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
26 </form> 26 </form>
27 <form id="no-type-test-form" action="#" enctype="multipart/form-data"> 27 <form id="no-type-test-form" action="#" enctype="multipart/form-data">
28 <input name="notype"> 28 <input name="notype">
29 <form> 29 </form>
30 30
31 <script> 31 <script>
32 (function () { 32 (function () {
......
...@@ -59,3 +59,14 @@ if (utils.gteVersion(phantom.version, '1.9.0')) { ...@@ -59,3 +59,14 @@ if (utils.gteVersion(phantom.version, '1.9.0')) {
59 }); 59 });
60 }); 60 });
61 } 61 }
62
63 casper.test.begin('sendKeys() reset option', 1, function(test) {
64 casper.start('tests/site/form.html', function() {
65 this.sendKeys('textarea', 'foo');
66 this.sendKeys('textarea', 'bar', {reset: true});
67 var values = this.getFormValues('form[action="result.html"]');
68 test.assertEquals(values.content, "bar");
69 }).run(function() {
70 test.done();
71 });
72 });
......