Merge pull request #376 from lukeasrodgers/bugfix_getFormValues_for_multiple_forms
Bugfix: `getFormvalues` choked on >1 form with same field names.
Showing
2 changed files
with
19 additions
and
4 deletions
... | @@ -455,10 +455,12 @@ | ... | @@ -455,10 +455,12 @@ |
455 | * Retrieves the value of a form field. | 455 | * Retrieves the value of a form field. |
456 | * | 456 | * |
457 | * @param String inputName The for input name attr value | 457 | * @param String inputName The for input name attr value |
458 | * @param Object options Object with formSelector, optional | ||
458 | * @return Mixed | 459 | * @return Mixed |
459 | */ | 460 | */ |
460 | this.getFieldValue = function getFieldValue(inputName) { | 461 | this.getFieldValue = function getFieldValue(inputName, options) { |
461 | function getSingleValue(input) { | 462 | function getSingleValue(input) { |
463 | var type; | ||
462 | try { | 464 | try { |
463 | type = input.getAttribute('type').toLowerCase(); | 465 | type = input.getAttribute('type').toLowerCase(); |
464 | } catch (e) { | 466 | } catch (e) { |
... | @@ -474,6 +476,7 @@ | ... | @@ -474,6 +476,7 @@ |
474 | return input.checked; | 476 | return input.checked; |
475 | } | 477 | } |
476 | function getMultipleValues(inputs) { | 478 | function getMultipleValues(inputs) { |
479 | var type; | ||
477 | type = inputs[0].getAttribute('type').toLowerCase(); | 480 | type = inputs[0].getAttribute('type').toLowerCase(); |
478 | if (type === 'radio') { | 481 | if (type === 'radio') { |
479 | var value; | 482 | var value; |
... | @@ -491,7 +494,11 @@ | ... | @@ -491,7 +494,11 @@ |
491 | return values; | 494 | return values; |
492 | } | 495 | } |
493 | } | 496 | } |
494 | var inputs = this.findAll('[name="' + inputName + '"]'), type; | 497 | var formSelector = ''; |
498 | if (options && options.formSelector) { | ||
499 | formSelector = options.formSelector + ' '; | ||
500 | } | ||
501 | var inputs = this.findAll(formSelector + '[name="' + inputName + '"]'); | ||
495 | switch (inputs.length) { | 502 | switch (inputs.length) { |
496 | case 0: return null; | 503 | case 0: return null; |
497 | case 1: return getSingleValue(inputs[0]); | 504 | case 1: return getSingleValue(inputs[0]); |
... | @@ -512,7 +519,7 @@ | ... | @@ -512,7 +519,7 @@ |
512 | [].forEach.call(form.elements, function(element) { | 519 | [].forEach.call(form.elements, function(element) { |
513 | var name = element.getAttribute('name'); | 520 | var name = element.getAttribute('name'); |
514 | if (name && !values[name]) { | 521 | if (name && !values[name]) { |
515 | values[name] = self.getFieldValue(name); | 522 | values[name] = self.getFieldValue(name, {formSelector: selector}); |
516 | } | 523 | } |
517 | }); | 524 | }); |
518 | return values; | 525 | return values; | ... | ... |
... | @@ -65,13 +65,21 @@ casper.test.begin('unexistent fields', 1, function(test) { | ... | @@ -65,13 +65,21 @@ casper.test.begin('unexistent fields', 1, function(test) { |
65 | }); | 65 | }); |
66 | }); | 66 | }); |
67 | 67 | ||
68 | casper.test.begin('multiple forms', 1, function(test) { | 68 | casper.test.begin('multiple forms', 2, function(test) { |
69 | casper.start('tests/site/multiple-forms.html', function() { | 69 | casper.start('tests/site/multiple-forms.html', function() { |
70 | this.fill('form[name="f2"]', { | 70 | this.fill('form[name="f2"]', { |
71 | yo: "ok" | 71 | yo: "ok" |
72 | }, true); | 72 | }, true); |
73 | }).then(function() { | 73 | }).then(function() { |
74 | test.assertUrlMatch(/\?f=f2&yo=ok$/, 'Casper.fill() handles multiple forms'); | 74 | test.assertUrlMatch(/\?f=f2&yo=ok$/, 'Casper.fill() handles multiple forms'); |
75 | }).then(function() { | ||
76 | this.fill('form[name="f2"]', { | ||
77 | yo: "ok" | ||
78 | }); | ||
79 | test.assertEquals(this.getFormValues('form[name="f2"]'), { | ||
80 | f: "f2", | ||
81 | yo: "ok" | ||
82 | }, 'Casper.getFormValues() retrieves filled values when multiple forms have same field names'); | ||
75 | }).run(function() { | 83 | }).run(function() { |
76 | test.done(); | 84 | test.done(); |
77 | }); | 85 | }); | ... | ... |
-
Please register or sign in to post a comment