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. ...@@ -40,6 +40,7 @@ This version is yet to be released.
40 - 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 40 - 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
41 - fixed [#232](https://github.com/n1k0/casperjs/issues/232) - symlink resolution in the ruby version of the `casperjs` executable 41 - fixed [#232](https://github.com/n1k0/casperjs/issues/232) - symlink resolution in the ruby version of the `casperjs` executable
42 - fixed [#236](https://github.com/n1k0/casperjs/issues/236) - fixed `Casper.exit` returned `this` after calling `phantom.exit()` which may caused PhantomJS to hang 42 - fixed [#236](https://github.com/n1k0/casperjs/issues/236) - fixed `Casper.exit` returned `this` after calling `phantom.exit()` which may caused PhantomJS to hang
43 - fixed [#252](https://github.com/n1k0/casperjs/issues/252) - better form.fill() error handling
43 - added [`ClientUtils.getDocumentHeight()`](http://casperjs.org/api.html#clientutils.getDocumentHeight) 44 - added [`ClientUtils.getDocumentHeight()`](http://casperjs.org/api.html#clientutils.getDocumentHeight)
44 - added [`toString()`](http://casperjs.org/api.html#casper.toString) and [`status()`](http://casperjs.org/api.html#casper.status) methods to `Casper` prototype. 45 - added [`toString()`](http://casperjs.org/api.html#casper.toString) and [`status()`](http://casperjs.org/api.html#casper.status) methods to `Casper` prototype.
45 46
......
...@@ -630,19 +630,19 @@ Casper.prototype.fill = function fill(selector, vals, submit) { ...@@ -630,19 +630,19 @@ Casper.prototype.fill = function fill(selector, vals, submit) {
630 } else if (fillResults.errors.length > 0) { 630 } else if (fillResults.errors.length > 0) {
631 (function _each(self){ 631 (function _each(self){
632 fillResults.errors.forEach(function _forEach(error) { 632 fillResults.errors.forEach(function _forEach(error) {
633 self.log("form error: " + error, "error"); 633 throw new CasperError(error);
634 }); 634 });
635 })(this); 635 })(this);
636 if (submit) { 636 if (submit) {
637 this.log("Errors encountered while filling form; submission aborted", "warning"); 637 this.warn("Errors encountered while filling form; submission aborted");
638 submit = false; 638 submit = false;
639 } 639 }
640 } 640 }
641 // File uploads 641 // File uploads
642 if (fillResults.files && fillResults.files.length > 0) { 642 if (fillResults.files && fillResults.files.length > 0) {
643 if (utils.isObject(selector) && selector.type === 'xpath') { 643 if (utils.isObject(selector) && selector.type === 'xpath') {
644 this.echo('⚠ Filling file upload fields is currently not supported using', 'COMMENT'); 644 this.warn('Filling file upload fields is currently not supported using ' +
645 this.echo(' XPath selectors; Please use a CSS selector instead.', 'COMMENT'); 645 'XPath selectors; Please use a CSS selector instead.');
646 } else { 646 } else {
647 (function _each(self) { 647 (function _each(self) {
648 fillResults.files.forEach(function _forEach(file) { 648 fillResults.files.forEach(function _forEach(file) {
......
...@@ -226,9 +226,10 @@ ...@@ -226,9 +226,10 @@
226 name: name, 226 name: name,
227 path: err.path 227 path: err.path
228 }); 228 });
229 } else if(err.name === "FieldNotFound") {
230 out.errors.push('Form field named "' + name + '" was not found.');
229 } else { 231 } else {
230 this.log(err, "error"); 232 out.errors.push(err.toString());
231 throw err;
232 } 233 }
233 } 234 }
234 } 235 }
...@@ -541,8 +542,10 @@ ...@@ -541,8 +542,10 @@
541 fields = field; 542 fields = field;
542 field = fields[0]; 543 field = fields[0];
543 } 544 }
544 if (!field instanceof HTMLElement) { 545 if (!(field instanceof HTMLElement)) {
545 this.log("Invalid field type; only HTMLElement and NodeList are supported", "error"); 546 var error = new Error('Invalid field type; only HTMLElement and NodeList are supported');
547 error.name = 'FieldNotFound';
548 throw error;
546 } 549 }
547 if (this.options && this.options.safeLogs && field.getAttribute('type') === "password") { 550 if (this.options && this.options.safeLogs && field.getAttribute('type') === "password") {
548 // obfuscate password value 551 // obfuscate password value
......
...@@ -52,6 +52,13 @@ casper.then(function() { ...@@ -52,6 +52,13 @@ casper.then(function() {
52 this.test.assertUrlMatch(/topic=bar/, 'Casper.fill() select field was submitted'); 52 this.test.assertUrlMatch(/topic=bar/, 'Casper.fill() select field was submitted');
53 }); 53 });
54 54
55 casper.thenOpen('tests/site/form.html', function() {
56 this.test.comment('Unexistent fields');
57 this.test.assertRaises(this.fill, ['form[action="result.html"]', {
58 unexistent: 42
59 }, true], 'Casper.fill() raises an exception when unable to fill a form');
60 });
61
55 // multiple forms 62 // multiple forms
56 casper.thenOpen('tests/site/multiple-forms.html', function() { 63 casper.thenOpen('tests/site/multiple-forms.html', function() {
57 this.test.comment('Multiple forms'); 64 this.test.comment('Multiple forms');
......