Casper#fill() can now check radiobuttons
Showing
1 changed file
with
21 additions
and
13 deletions
... | @@ -616,7 +616,7 @@ | ... | @@ -616,7 +616,7 @@ |
616 | if (!vals.hasOwnProperty(name)) { | 616 | if (!vals.hasOwnProperty(name)) { |
617 | continue; | 617 | continue; |
618 | } | 618 | } |
619 | var field = form.querySelector('[name="' + name + '"]') | 619 | var field = form.querySelectorAll('[name="' + name + '"]') |
620 | , value = vals[name]; | 620 | , value = vals[name]; |
621 | if (!field) { | 621 | if (!field) { |
622 | console.log('no field named "' + name + '" in form'); | 622 | console.log('no field named "' + name + '" in form'); |
... | @@ -657,19 +657,23 @@ | ... | @@ -657,19 +657,23 @@ |
657 | }; | 657 | }; |
658 | 658 | ||
659 | /** | 659 | /** |
660 | * Sets a field value. Fails silently, but log error messages. | 660 | * Sets a field (or a set of fields) value. Fails silently, but log |
661 | * error messages. | ||
661 | * | 662 | * |
662 | * @param HTMLElement field The field element | 663 | * @param HTMLElement|NodeList field One or more element defining a field |
663 | * @param mixed value The field value to set | 664 | * @param mixed value The field value to set |
664 | */ | 665 | */ |
665 | this.setField = function(field, value) { | 666 | this.setField = function(field, value) { |
667 | var fields; | ||
668 | value = value || ""; | ||
669 | if (field instanceof NodeList) { | ||
670 | fields = field; | ||
671 | field = fields[0]; | ||
672 | } | ||
666 | if (!field instanceof HTMLElement) { | 673 | if (!field instanceof HTMLElement) { |
667 | console.log('the field must be an HTMLElement'); | 674 | console.log('invalid field type; only HTMLElement and NodeList are supported'); |
668 | return; | ||
669 | } else { | ||
670 | console.log('set "' + field.getAttribute('name') + '" value to ' + value); | ||
671 | } | 675 | } |
672 | value = value || ""; | 676 | console.log('set "' + field.getAttribute('name') + '" value to ' + value); |
673 | var nodeName = field.nodeName.toLowerCase(); | 677 | var nodeName = field.nodeName.toLowerCase(); |
674 | switch (nodeName) { | 678 | switch (nodeName) { |
675 | case "input": | 679 | case "input": |
... | @@ -700,16 +704,20 @@ | ... | @@ -700,16 +704,20 @@ |
700 | console.log("file field filling is not supported"); | 704 | console.log("file field filling is not supported"); |
701 | break; | 705 | break; |
702 | case "radio": | 706 | case "radio": |
703 | field.click(); | 707 | if (fields) { |
708 | Array.prototype.forEach.call(fields, function(e) { | ||
709 | e.checked = (e.value === value); | ||
710 | }); | ||
711 | } else { | ||
712 | console.log('radio elements are empty'); | ||
713 | } | ||
704 | break; | 714 | break; |
705 | default: | 715 | default: |
706 | console.log("unsupported field type: " + type); | 716 | console.log("unsupported input field type: " + type); |
707 | break; | 717 | break; |
708 | } | 718 | } |
709 | break; | 719 | break; |
710 | case "select": | 720 | case "select": |
711 | console.log('select tag fillin not implemented'); | ||
712 | break; | ||
713 | case "textarea": | 721 | case "textarea": |
714 | field.value = value; | 722 | field.value = value; |
715 | break; | 723 | break; | ... | ... |
-
Please register or sign in to post a comment