Commit 78683e2c 78683e2cbf78c9993eadce82f3ed4c10962497c1 by Nicolas Perriault

refs #252 - better error handling for form.fill()

1 parent 1fded36e
......@@ -40,6 +40,7 @@ This version is yet to be released.
- fixed [#231](https://github.com/n1k0/casperjs/pull/231) - added `--pre` and `--post` options to the `casperjs test` command to load test files before and after the execution of testsuite
- fixed [#232](https://github.com/n1k0/casperjs/issues/232) - symlink resolution in the ruby version of the `casperjs` executable
- fixed [#236](https://github.com/n1k0/casperjs/issues/236) - fixed `Casper.exit` returned `this` after calling `phantom.exit()` which may caused PhantomJS to hang
- fixed [#252](https://github.com/n1k0/casperjs/issues/252) - better form.fill() error handling
- added [`ClientUtils.getDocumentHeight()`](http://casperjs.org/api.html#clientutils.getDocumentHeight)
- added [`toString()`](http://casperjs.org/api.html#casper.toString) and [`status()`](http://casperjs.org/api.html#casper.status) methods to `Casper` prototype.
......
......@@ -630,19 +630,19 @@ Casper.prototype.fill = function fill(selector, vals, submit) {
} else if (fillResults.errors.length > 0) {
(function _each(self){
fillResults.errors.forEach(function _forEach(error) {
self.log("form error: " + error, "error");
throw new CasperError(error);
});
})(this);
if (submit) {
this.log("Errors encountered while filling form; submission aborted", "warning");
this.warn("Errors encountered while filling form; submission aborted");
submit = false;
}
}
// File uploads
if (fillResults.files && fillResults.files.length > 0) {
if (utils.isObject(selector) && selector.type === 'xpath') {
this.echo('⚠ Filling file upload fields is currently not supported using', 'COMMENT');
this.echo(' XPath selectors; Please use a CSS selector instead.', 'COMMENT');
this.warn('Filling file upload fields is currently not supported using ' +
'XPath selectors; Please use a CSS selector instead.');
} else {
(function _each(self) {
fillResults.files.forEach(function _forEach(file) {
......
......@@ -226,9 +226,10 @@
name: name,
path: err.path
});
} else if(err.name === "FieldNotFound") {
out.errors.push('Form field named "' + name + '" was not found.');
} else {
this.log(err, "error");
throw err;
out.errors.push(err.toString());
}
}
}
......@@ -541,8 +542,10 @@
fields = field;
field = fields[0];
}
if (!field instanceof HTMLElement) {
this.log("Invalid field type; only HTMLElement and NodeList are supported", "error");
if (!(field instanceof HTMLElement)) {
var error = new Error('Invalid field type; only HTMLElement and NodeList are supported');
error.name = 'FieldNotFound';
throw error;
}
if (this.options && this.options.safeLogs && field.getAttribute('type') === "password") {
// obfuscate password value
......
......@@ -52,6 +52,13 @@ casper.then(function() {
this.test.assertUrlMatch(/topic=bar/, 'Casper.fill() select field was submitted');
});
casper.thenOpen('tests/site/form.html', function() {
this.test.comment('Unexistent fields');
this.test.assertRaises(this.fill, ['form[action="result.html"]', {
unexistent: 42
}, true], 'Casper.fill() raises an exception when unable to fill a form');
});
// multiple forms
casper.thenOpen('tests/site/multiple-forms.html', function() {
this.test.comment('Multiple forms');
......