Commit a84af353 a84af3539ff6ab514e3357481042eeab3287118f by Nicolas Perriault

Merge pull request #428 from jfparadis/sendkeys-options-keepFocus

refs #427 - Added sendKeys options keepFocus (with docs and test)
2 parents a82c0cbd 882a486b
......@@ -1311,6 +1311,13 @@ Sends native keyboard events to the element matching the provided :doc:`selector
this.click('form.contact input[type="submit"]');
});
Note that ``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::
casper.then(function() {
this.sendKeys('form.contact input#name', 'action', {keepFocus: true});
this.click('form.contact ul.ui-autocomplete li.ui-menu-item:first-child a');
});
.. index:: auth
``setHttpAuth()``
......
......@@ -1453,7 +1453,7 @@ Casper.prototype.sendKeys = function(selector, keys, options) {
this.click(selector);
}
this.page.sendEvent(options.eventType, keys);
if (isTextInput) {
if (isTextInput && !options.keepFocus) {
// remove the focus
this.evaluate(function(selector) {
__utils__.findOne(selector).blur();
......
......@@ -8,6 +8,7 @@
<form action="result.html" enctype="multipart/form-data">
<input type="text" name="email" placeholder="email">
<input type="password" name="password" placeholder="password">
<input type="text" name="language" placeholder="language">
<textarea name="content"></textarea>
<select name="topic">
<option>foo</option>
......@@ -22,5 +23,66 @@
<input type="checkbox" name="checklist[]" value="3">
<input type="submit" name="submit" value="submit">
</form>
<script>
(function () {
'use strict';
// El-cheapo autocomplete
var choices = [
'dutch',
'danish',
'english',
'french',
'german',
'greek',
'irish',
'italian',
'portuguese',
'spanish'
],
blurAutocomplete = function () {
var el = document.getElementById('autocomplete');
if (el) {
el.parentNode.removeChild(el);
}
},
input = document.querySelector('input[name="language"]');
input.onkeyup = function () {
var that = this,
value = this.value,
html = '',
el,
i;
for (i = 0; i < choices.length; i += 1) {
if (choices[i].search(value) >= 0) {
html += '<li>' + choices[i] + '</li>';
}
}
el = document.getElementById('autocomplete');
if (!el) {
el = document.createElement('ul');
el.setAttribute('id', 'autocomplete');
el.onclick = function (e) {
that.value = e.target.innerHTML;
};
document.getElementsByTagName('body')[0].appendChild(el);
}
el.innerHTML = html;
};
input.onblur = function () {
window.setTimeout(blurAutocomplete, 200);
};
}());
</script>
</body>
</html>
......
......@@ -111,6 +111,7 @@ casper.test.begin('getFormValues() tests', 2, function(test) {
this.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
password: 'chuck',
language: 'english',
content: 'Am watching thou',
check: true,
choice: 'no',
......@@ -129,6 +130,7 @@ casper.test.begin('getFormValues() tests', 2, function(test) {
"file": "C:\\fakepath\\README.md", // phantomjs/webkit sets that
"password": "chuck",
"submit": "submit",
"language": "english",
"topic": "bar"
}, 'Casper.getFormValues() retrieves filled values');
});
......@@ -136,6 +138,7 @@ casper.test.begin('getFormValues() tests', 2, function(test) {
this.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
password: 'chuck',
language: 'english',
content: 'Am watching thou',
check: true,
choice: 'yes',
......@@ -153,6 +156,7 @@ casper.test.begin('getFormValues() tests', 2, function(test) {
"email": "chuck@norris.com",
"file": "C:\\fakepath\\README.md", // phantomjs/webkit sets that
"password": "chuck",
"language": "english",
"submit": "submit",
"topic": "bar"
}, 'Casper.getFormValues() correctly retrieves values from radio inputs regardless of order');
......
/*jshint strict:false*/
/*global CasperError, casper, console, phantom, require*/
casper.test.begin('sendKeys() tests', 2, function(test) {
casper.test.begin('sendKeys() tests', 3, function(test) {
casper.start('tests/site/form.html', function() {
this.sendKeys('input[name="email"]', 'duke@nuk.em');
this.sendKeys('input[name="language"]', 'fr', {keepFocus: true});
this.click('#autocomplete li:first-child');
this.sendKeys('textarea', "Damn, I’m looking good.");
var values = this.getFormValues('form');
test.assertEquals(values.email, 'duke@nuk.em',
'Casper.sendKeys() sends keys to given input');
test.assertEquals(values.language, 'french',
'Casper.sendKeys() sends keys to given input and keeps focus afterweards');
test.assertEquals(values.content, "Damn, I’m looking good.",
'Casper.sendKeys() sends keys to given textarea');
}).run(function() {
......