better error handling and logging in Casper#fill
Showing
1 changed file
with
26 additions
and
6 deletions
... | @@ -309,11 +309,12 @@ | ... | @@ -309,11 +309,12 @@ |
309 | * @param Boolean submit Submit the form? | 309 | * @param Boolean submit Submit the form? |
310 | */ | 310 | */ |
311 | fill: function(selector, vals, submit) { | 311 | fill: function(selector, vals, submit) { |
312 | if (typeof(selector) !== "string") { | 312 | submit = submit === true ? submit : false; |
313 | throw "selector must be a string: " + selector; | 313 | if (typeof(selector) !== "string" || !selector.length) { |
314 | throw "form selector must be a non-empty string"; | ||
314 | } | 315 | } |
315 | if (!typeof(vals) === "object") { | 316 | if (!typeof(vals) === "object") { |
316 | throw "form values must be an object"; | 317 | throw "form values must be provided as an object"; |
317 | } | 318 | } |
318 | var fillResults = this.evaluate(function() { | 319 | var fillResults = this.evaluate(function() { |
319 | return __utils__.fill('%selector%', JSON.parse('%values%')); | 320 | return __utils__.fill('%selector%', JSON.parse('%values%')); |
... | @@ -323,6 +324,16 @@ | ... | @@ -323,6 +324,16 @@ |
323 | }); | 324 | }); |
324 | if (!fillResults) { | 325 | if (!fillResults) { |
325 | throw "unable to fill form"; | 326 | throw "unable to fill form"; |
327 | } else if (fillResults.errors.length > 0) { | ||
328 | (function(self){ | ||
329 | fillResults.errors.forEach(function(error) { | ||
330 | self.log("form error: " + error, "error"); | ||
331 | }); | ||
332 | })(this); | ||
333 | if (submit) { | ||
334 | this.log("errors encountered while filling form; submission aborted", "warning"); | ||
335 | submit = false; | ||
336 | } | ||
326 | } | 337 | } |
327 | // File uploads | 338 | // File uploads |
328 | if (fillResults.files && fillResults.files.length > 0) { | 339 | if (fillResults.files && fillResults.files.length > 0) { |
... | @@ -334,7 +345,7 @@ | ... | @@ -334,7 +345,7 @@ |
334 | })(this); | 345 | })(this); |
335 | } | 346 | } |
336 | // Form submission? | 347 | // Form submission? |
337 | if ((submit||false) === true) { | 348 | if (submit) { |
338 | this.evaluate(function() { | 349 | this.evaluate(function() { |
339 | var form = document.querySelector('%selector%'); | 350 | var form = document.querySelector('%selector%'); |
340 | console.log('submitting form to ' + (form.getAttribute('action') || "unknown") | 351 | console.log('submitting form to ' + (form.getAttribute('action') || "unknown") |
... | @@ -637,14 +648,23 @@ | ... | @@ -637,14 +648,23 @@ |
637 | */ | 648 | */ |
638 | this.fill = function(form, vals) { | 649 | this.fill = function(form, vals) { |
639 | var out = { | 650 | var out = { |
651 | errors: [], | ||
640 | fields: [], | 652 | fields: [], |
641 | files: [], | 653 | files: [], |
642 | }; | 654 | }; |
643 | if (!(form instanceof HTMLElement) || typeof(form) === "string") { | 655 | if (!(form instanceof HTMLElement) || typeof(form) === "string") { |
656 | console.log("attempting to fetch form element from selector: '" + form + "'"); | ||
657 | try { | ||
644 | form = document.querySelector(form); | 658 | form = document.querySelector(form); |
659 | } catch (e) { | ||
660 | if (e.name === "SYNTAX_ERR") { | ||
661 | out.errors.push("invalid form selector provided: '" + form + "'"); | ||
662 | return out; | ||
663 | } | ||
664 | } | ||
645 | } | 665 | } |
646 | if (!form) { | 666 | if (!form) { |
647 | console.log('form not found or invalid selector provided:'); | 667 | out.errors.push("form not found"); |
648 | return out; | 668 | return out; |
649 | } | 669 | } |
650 | for (var name in vals) { | 670 | for (var name in vals) { |
... | @@ -654,7 +674,7 @@ | ... | @@ -654,7 +674,7 @@ |
654 | var field = form.querySelectorAll('[name="' + name + '"]') | 674 | var field = form.querySelectorAll('[name="' + name + '"]') |
655 | , value = vals[name]; | 675 | , value = vals[name]; |
656 | if (!field) { | 676 | if (!field) { |
657 | console.log('no field named "' + name + '" in form'); | 677 | out.errors.push('no field named "' + name + '" in form'); |
658 | continue; | 678 | continue; |
659 | } | 679 | } |
660 | try { | 680 | try { | ... | ... |
-
Please register or sign in to post a comment