added Casper#fill() and Casper#setField() methods; moved the click method logic to ClientUtils
Showing
1 changed file
with
118 additions
and
20 deletions
... | @@ -181,26 +181,7 @@ | ... | @@ -181,26 +181,7 @@ |
181 | click: function(selector) { | 181 | click: function(selector) { |
182 | this.log("click on selector: " + selector, "debug"); | 182 | this.log("click on selector: " + selector, "debug"); |
183 | return this.evaluate(function() { | 183 | return this.evaluate(function() { |
184 | var s = '%selector%'; | 184 | return __utils__.click('%selector%'); |
185 | try { | ||
186 | var elem = document.querySelector(s); | ||
187 | } catch (e) { | ||
188 | console.log('invalid selector: ' + s); | ||
189 | return false; | ||
190 | } | ||
191 | if (!elem) { | ||
192 | console.log('selector "' + s + '" did not find any matching element'); | ||
193 | return false; | ||
194 | } | ||
195 | var evt = document.createEvent("MouseEvents"); | ||
196 | evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem); | ||
197 | if (elem.dispatchEvent(evt)) { | ||
198 | return true; | ||
199 | } | ||
200 | if (elem.hasAttribute('href')) { | ||
201 | document.location = elem.getAttribute('href'); | ||
202 | return true; | ||
203 | } | ||
204 | }, { | 185 | }, { |
205 | selector: selector.replace("'", "\'") | 186 | selector: selector.replace("'", "\'") |
206 | }); | 187 | }); |
... | @@ -313,6 +294,24 @@ | ... | @@ -313,6 +294,24 @@ |
313 | }, | 294 | }, |
314 | 295 | ||
315 | /** | 296 | /** |
297 | * Fills a form with provided field values. | ||
298 | * | ||
299 | * @param String selector A CSS3 selector to the target form to fill | ||
300 | * @param Object values Field values | ||
301 | */ | ||
302 | fill: function(selector, values) { | ||
303 | if (!typeof(values) === "object") { | ||
304 | throw "form values must be an object"; | ||
305 | } | ||
306 | return this.evaluate(function() { | ||
307 | __utils__.fill('%selector%', JSON.parse('%values%')); | ||
308 | }, { | ||
309 | selector: selector.replace("'", "\'"), | ||
310 | values: JSON.stringify(values).replace("'", "\'"), | ||
311 | }); | ||
312 | }, | ||
313 | |||
314 | /** | ||
316 | * Logs a message. | 315 | * Logs a message. |
317 | * | 316 | * |
318 | * @param String message The message to log | 317 | * @param String message The message to log |
... | @@ -511,6 +510,35 @@ | ... | @@ -511,6 +510,35 @@ |
511 | */ | 510 | */ |
512 | phantom.Casper.ClientUtils = function() { | 511 | phantom.Casper.ClientUtils = function() { |
513 | /** | 512 | /** |
513 | * Clicks on the DOM element behind the provided selector. | ||
514 | * | ||
515 | * @param String selector A CSS3 selector to the element to click | ||
516 | * @return Boolean | ||
517 | */ | ||
518 | this.click = function(selector) { | ||
519 | try { | ||
520 | var elem = document.querySelector(selector); | ||
521 | } catch (e) { | ||
522 | console.log('invalid selector: ' + selector); | ||
523 | return false; | ||
524 | } | ||
525 | if (!elem) { | ||
526 | console.log('selector "' + selector + '" did not find any matching element'); | ||
527 | return false; | ||
528 | } | ||
529 | var evt = document.createEvent("MouseEvents"); | ||
530 | evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem); | ||
531 | if (elem.dispatchEvent(evt)) { | ||
532 | return true; | ||
533 | } | ||
534 | if (elem.hasAttribute('href')) { | ||
535 | document.location = elem.getAttribute('href'); | ||
536 | return true; | ||
537 | } | ||
538 | return false; | ||
539 | }; | ||
540 | |||
541 | /** | ||
514 | * Base64 encodes a string, even binary ones. Succeeds where | 542 | * Base64 encodes a string, even binary ones. Succeeds where |
515 | * window.btoa() fails. | 543 | * window.btoa() fails. |
516 | * | 544 | * |
... | @@ -560,6 +588,36 @@ | ... | @@ -560,6 +588,36 @@ |
560 | }; | 588 | }; |
561 | 589 | ||
562 | /** | 590 | /** |
591 | * Fills a form with provided field values. | ||
592 | * | ||
593 | * @param HTMLElement|String form A form element, or a CSS3 selector to a form element | ||
594 | * @param Object vals Field values | ||
595 | */ | ||
596 | this.fill = function(form, vals) { | ||
597 | if (!(form instanceof HTMLElement) || typeof(form) === "string") { | ||
598 | form = document.querySelector(form); | ||
599 | console.log('found via selector') | ||
600 | } | ||
601 | if (!form) { | ||
602 | console.log('form not found or invalid'); | ||
603 | return; | ||
604 | } | ||
605 | console.log('form is ' + (typeof(form))) | ||
606 | for (var name in vals) { | ||
607 | if (!vals.hasOwnProperty(name)) { | ||
608 | continue; | ||
609 | } | ||
610 | var field = form.querySelector('[name="' + name + '"]') | ||
611 | , value = vals[name]; | ||
612 | if (!field) { | ||
613 | console.log('no field named "' + name + '" in form'); | ||
614 | continue; | ||
615 | } | ||
616 | this.setField(field, value); | ||
617 | } | ||
618 | }; | ||
619 | |||
620 | /** | ||
563 | * Downloads a resource behind an url and returns its base64-encoded | 621 | * Downloads a resource behind an url and returns its base64-encoded |
564 | * contents. | 622 | * contents. |
565 | * | 623 | * |
... | @@ -583,6 +641,46 @@ | ... | @@ -583,6 +641,46 @@ |
583 | xhr.send(null); | 641 | xhr.send(null); |
584 | return xhr.responseText; | 642 | return xhr.responseText; |
585 | }; | 643 | }; |
644 | |||
645 | /** | ||
646 | * Sets a field value. Fails silently, but log error messages. | ||
647 | * | ||
648 | * @param HTMLElement field The field element | ||
649 | * @param mixed value The field value to set | ||
650 | */ | ||
651 | this.setField = function(field, value) { | ||
652 | if (!field instanceof HTMLElement) { | ||
653 | console.log('the field must be an HTMLElement'); | ||
654 | return; | ||
655 | } | ||
656 | value = value || ""; | ||
657 | switch (field.nodeName.toLowerCase()) { | ||
658 | case "input": | ||
659 | var type = field.getAttribute('type') || "text"; | ||
660 | switch (type.toLowerCase()) { | ||
661 | case "text": | ||
662 | case "password": | ||
663 | default: | ||
664 | field.setAttribute('value', value); | ||
665 | break; | ||
666 | case "checkbox": | ||
667 | field.setAttribute('checked', value ? "checked" : ""); | ||
668 | break; | ||
669 | case "radio": | ||
670 | field.click(); | ||
671 | break; | ||
672 | } | ||
673 | break; | ||
674 | case "textarea": | ||
675 | field.innerText = value; | ||
676 | break; | ||
677 | case "select": | ||
678 | console.log('select tag fillin not implemented'); | ||
679 | break; | ||
680 | default: | ||
681 | console.log('unsupported field type: ' + type); | ||
682 | } | ||
683 | } | ||
586 | }; | 684 | }; |
587 | 685 | ||
588 | /** | 686 | /** | ... | ... |
-
Please register or sign in to post a comment