Commit 7c2137eb 7c2137eb036307d8f2cafec2758639f26b092dd1 by Nicolas Perriault

refs #259 - things were harder than initially expected

1 parent a04d47a8
1 Subproject commit 4826e2bb9a2ecbaf0511b6aec0fd3b68d566e23b 1 Subproject commit 3b87b13e5849f5c8d7555c9a5fc6712eda5aa7a8
......
...@@ -408,15 +408,37 @@ ...@@ -408,15 +408,37 @@
408 return nodes; 408 return nodes;
409 }; 409 };
410 410
411 /**
412 * Retrieves the value of a form field.
413 *
414 * @param String inputName The for input name attr value
415 * @return Mixed
416 */
411 this.getFieldValue = function getFieldValue(inputName) { 417 this.getFieldValue = function getFieldValue(inputName) {
412 var inputs = this.findAll('[name="' + inputName + '"]'); 418 var inputs = this.findAll('[name="' + inputName + '"]'), type;
413 switch (inputs.length) { 419 switch (inputs.length) {
414 case 0: 420 case 0:
415 return null; 421 return null;
416 case 1: 422 case 1:
417 return inputs[0].value; 423 //this.log(inputs[0].nodeName.toLowerCase(), "error");
424 var input = inputs[0];
425 try {
426 type = input.getAttribute('type').toLowerCase();
427 } catch (e) {
428 type = 'other';
429 }
430 if (['checkbox', 'radio'].indexOf(type) === -1) {
431 return input.value;
432 }
433 // single checkbox or… radio button (weird, I know)
434 if (input.hasAttribute('value')) {
435 return input.checked ? input.getAttribute('value') : undefined;
436 } else {
437 return input.checked;
438 }
439 break;
418 default: 440 default:
419 var type = inputs[0].getAttribute('type').toLowerCase(); 441 type = inputs[0].getAttribute('type').toLowerCase();
420 if (type === 'radio') { 442 if (type === 'radio') {
421 var value; 443 var value;
422 [].forEach.call(inputs, function(radio) { 444 [].forEach.call(inputs, function(radio) {
......
...@@ -97,11 +97,12 @@ casper.then(function() { ...@@ -97,11 +97,12 @@ casper.then(function() {
97 97
98 casper.thenOpen('tests/site/form.html', function() { 98 casper.thenOpen('tests/site/form.html', function() {
99 t.comment('Tester.assertField()'); 99 t.comment('Tester.assertField()');
100 t.comment('1. Fill inputs');
100 var fpath = phantom.libraryPath + '/README.md'; 101 var fpath = phantom.libraryPath + '/README.md';
101 this.fill('form[action="result.html"]', { 102 this.fill('form[action="result.html"]', {
102 'email': 'chuck@norris.com', 103 'email': 'chuck@norris.com',
103 'content': 'Am watching thou', 104 'content': 'Am watching thou',
104 'check': 'on', 105 'check': true,
105 'choice': 'no', 106 'choice': 'no',
106 'topic': 'bar', 107 'topic': 'bar',
107 'file': fpath, 108 'file': fpath,
...@@ -109,13 +110,33 @@ casper.thenOpen('tests/site/form.html', function() { ...@@ -109,13 +110,33 @@ casper.thenOpen('tests/site/form.html', function() {
109 }); 110 });
110 t.assertField('email', 'chuck@norris.com', 'Tester.assertField() works as expected with inputs'); 111 t.assertField('email', 'chuck@norris.com', 'Tester.assertField() works as expected with inputs');
111 t.assertField('content', 'Am watching thou', 'Tester.assertField() works as expected with textarea'); 112 t.assertField('content', 'Am watching thou', 'Tester.assertField() works as expected with textarea');
112 t.assertField('check', 'on', 'Tester.assertField() works as expected with checkboxes'); 113 t.assertField('check', true, 'Tester.assertField() works as expected with checkboxes');
113 t.assertField('choice', 'no', 'Tester.assertField() works as expected with radios'); 114 t.assertField('choice', 'no', 'Tester.assertField() works as expected with radios');
114 t.assertField('topic', 'bar', 'Tester.assertField() works as expected with selects'); 115 t.assertField('topic', 'bar', 'Tester.assertField() works as expected with selects');
115 t.assertField('file', 'C:\\fakepath\\README.md', 'Tester.assertField() works as expected with file inputs'); 116 t.assertField('file', 'C:\\fakepath\\README.md', 'Tester.assertField() works as expected with file inputs');
116 t.assertField('checklist[]', ['1', '3'], 'Tester.assertField() works as expected with check lists'); 117 t.assertField('checklist[]', ['1', '3'], 'Tester.assertField() works as expected with check lists');
117 }); 118 });
118 119
120 casper.reload(function() {
121 t.comment('2. Unfill inputs');
122 this.fill('form[action="result.html"]', {
123 'email': '',
124 'content': '',
125 'check': false,
126 'choice': '',
127 'topic': '',
128 'file': '',
129 'checklist[]': []
130 });
131 t.assertField('email', '', 'Tester.assertField() works as expected with inputs');
132 t.assertField('content', '', 'Tester.assertField() works as expected with textarea');
133 t.assertField('check', false, 'Tester.assertField() works as expected with checkboxes');
134 t.assertField('choice', null, 'Tester.assertField() works as expected with radios');
135 t.assertField('topic', 'foo', 'Tester.assertField() works as expected with selects');
136 t.assertField('file', '', 'Tester.assertField() works as expected with file inputs');
137 t.assertField('checklist[]', [], 'Tester.assertField() works as expected with check lists');
138 });
139
119 casper.then(function() { 140 casper.then(function() {
120 t.comment('Tester.getFailures()'); 141 t.comment('Tester.getFailures()');
121 t.assertEquals(typeof t.getFailures().length, "number", "Tester.getFailures() works as expected"); 142 t.assertEquals(typeof t.getFailures().length, "number", "Tester.getFailures() works as expected");
......