Commit 33f335a6 33f335a6d5b433665aeaf5c01fd74a3b8c26be91 by Nicolas Perriault

fixed checkbox list filling, added tests

1 parent 61c69bda
......@@ -1061,6 +1061,7 @@
path: err.path
});
} else {
this.log(err, "error");
throw err;
}
}
......@@ -1187,20 +1188,16 @@
field.value = value;
break;
case "checkbox":
if (fields) {
if (fields.length > 1) {
var values = value;
if (!Array.isArray(values)) {
values = [values];
}
Array.prototype.forEach.call(fields, function(e) {
if (values.indexOf(e.value) !== -1) {
e.setAttribute('checked', 'checked');
} else {
e.removeAttribute('checked');
}
Array.prototype.forEach.call(fields, function(f) {
f.checked = values.indexOf(f.value) !== -1 ? true : false;
});
} else {
field.setAttribute('checked', value ? "checked" : "");
field.checked = value ? true : false;
}
break;
case "file":
......
......@@ -104,12 +104,13 @@ casper.then(function(self) {
self.test.assertTitle('CasperJS test form', 'Casper.click() casper can click on a text link and react when it is loaded 2/2');
self.test.comment('filling a form');
self.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
content: 'Am watching thou',
check: true,
choice: 'no',
topic: 'bar',
file: phantom.libraryPath + '/README.md'
email: 'chuck@norris.com',
content: 'Am watching thou',
check: true,
choice: 'no',
topic: 'bar',
file: phantom.libraryPath + '/README.md',
'checklist[]': ['1', '3']
});
self.test.assertEvalEquals(function() {
return document.querySelector('input[name="email"]').value;
......@@ -132,6 +133,11 @@ casper.then(function(self) {
self.test.assertEvalEquals(function() {
return document.querySelector('input[name="file"]').files.length === 1;
}, true, 'Casper.fill() can select a file to upload');
self.test.assertEvalEquals(function() {
return document.querySelector('input[name="checklist[]"][value="1"]').checked
&& !document.querySelector('input[name="checklist[]"][value="2"]').checked
&& document.querySelector('input[name="checklist[]"][value="3"]').checked
}, true, 'Casper.fill() can fill a list of checkboxes');
self.click('input[type="submit"]');
});
......
......@@ -16,6 +16,9 @@
<input type="radio" name="choice" value="yes"/>
<input type="radio" name="choice" value="no"/>
<input type="file" name="file"/>
<input type="checkbox" name="checklist[]" value="1" />
<input type="checkbox" name="checklist[]" value="2" />
<input type="checkbox" name="checklist[]" value="3" />
<input type="submit"/>
</form>
</body>
......