fixes #259 - enhanced Tester.assertField()
Tester.assertField() can now test for field values from other types than inputs.
Showing
6 changed files
with
72 additions
and
21 deletions
1 | CasperJS Changelog | 1 | CasperJS Changelog |
2 | ================== | 2 | ================== |
3 | 3 | ||
4 | XXXX-XX-XX, v1.0.0 | ||
5 | ------------------ | ||
6 | |||
7 | This version is yet to be released. | ||
8 | |||
9 | - fixed [#259](https://github.com/n1k0/casperjs/issues/259) - enhanced `Tester.assertField()` method, which can now tests for other field types than `input`s. | ||
10 | - added [`ClientUtils.getFieldValue()`](http://casperjs.org/api.html#clientutils.getFieldValue) | ||
11 | |||
4 | 2012-10-23, v1.0.0-RC3 | 12 | 2012-10-23, v1.0.0-RC3 |
5 | ---------------------- | 13 | ---------------------- |
6 | 14 | ... | ... |
... | @@ -408,6 +408,34 @@ | ... | @@ -408,6 +408,34 @@ |
408 | return nodes; | 408 | return nodes; |
409 | }; | 409 | }; |
410 | 410 | ||
411 | this.getFieldValue = function getFieldValue(inputName) { | ||
412 | var inputs = this.findAll('[name="' + inputName + '"]'); | ||
413 | switch (inputs.length) { | ||
414 | case 0: | ||
415 | return null; | ||
416 | case 1: | ||
417 | return inputs[0].value; | ||
418 | default: | ||
419 | var type = inputs[0].getAttribute('type').toLowerCase(); | ||
420 | if (type === 'radio') { | ||
421 | var value; | ||
422 | [].forEach.call(inputs, function(radio) { | ||
423 | value = radio.checked ? radio.value : undefined; | ||
424 | }); | ||
425 | return value; | ||
426 | } else if (type === 'checkbox') { | ||
427 | var values = []; | ||
428 | [].forEach.call(inputs, function(checkbox) { | ||
429 | if (checkbox.checked) { | ||
430 | values.push(checkbox.value); | ||
431 | } | ||
432 | }); | ||
433 | return values; | ||
434 | } | ||
435 | break; | ||
436 | } | ||
437 | }; | ||
438 | |||
411 | /** | 439 | /** |
412 | * Logs a message. Will format the message a way CasperJS will be able | 440 | * Logs a message. Will format the message a way CasperJS will be able |
413 | * to log phantomjs side. | 441 | * to log phantomjs side. | ... | ... |
... | @@ -28,7 +28,7 @@ | ... | @@ -28,7 +28,7 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global CasperError exports phantom require*/ | 31 | /*global CasperError exports phantom require __utils__*/ |
32 | 32 | ||
33 | var fs = require('fs'); | 33 | var fs = require('fs'); |
34 | var events = require('events'); | 34 | var events = require('events'); |
... | @@ -238,24 +238,23 @@ Tester.prototype.assertEvalEquals = Tester.prototype.assertEvalEqual = function | ... | @@ -238,24 +238,23 @@ Tester.prototype.assertEvalEquals = Tester.prototype.assertEvalEqual = function |
238 | /** | 238 | /** |
239 | * Asserts that a given input field has the provided value. | 239 | * Asserts that a given input field has the provided value. |
240 | * | 240 | * |
241 | * @param String input_name The name attribute of the input element | 241 | * @param String inputName The name attribute of the input element |
242 | * @param String expected_value The expected value of the input element | 242 | * @param String expected The expected value of the input element |
243 | * @param String message Test description | 243 | * @param String message Test description |
244 | * @return Object An assertion result object | 244 | * @return Object An assertion result object |
245 | */ | 245 | */ |
246 | Tester.prototype.assertField = function assertField(input_name, expected_value, message) { | 246 | Tester.prototype.assertField = function assertField(inputName, expected, message) { |
247 | "use strict"; | 247 | "use strict"; |
248 | var actual_value = this.casper.evaluate(function(input_name) { | 248 | var actual = this.casper.evaluate(function(inputName) { |
249 | var input = document.querySelector('input[name="' + input_name + '"]'); | 249 | return __utils__.getFieldValue(inputName); |
250 | return input ? input.value : null; | 250 | }, { inputName: inputName }); |
251 | }, { input_name: input_name }); | 251 | return this.assert(this.testEquals(actual, expected), message, { |
252 | return this.assert(this.testEquals(actual_value, expected_value), message, { | ||
253 | type: 'assertField', | 252 | type: 'assertField', |
254 | standard: f('"%s" input field has the value "%s"', input_name, expected_value), | 253 | standard: f('"%s" input field has the value "%s"', inputName, expected), |
255 | values: { | 254 | values: { |
256 | input_name: input_name, | 255 | inputName: inputName, |
257 | actual_value: actual_value, | 256 | actual: actual, |
258 | expected_value: expected_value | 257 | expected: expected |
259 | } | 258 | } |
260 | }); | 259 | }); |
261 | }; | 260 | }; | ... | ... |
... | @@ -90,9 +90,6 @@ exports.dump = dump; | ... | @@ -90,9 +90,6 @@ exports.dump = dump; |
90 | */ | 90 | */ |
91 | function equals(v1, v2) { | 91 | function equals(v1, v2) { |
92 | "use strict"; | 92 | "use strict"; |
93 | if (betterTypeOf(v1) !== betterTypeOf(v2)) { | ||
94 | return false; | ||
95 | } | ||
96 | if (isFunction(v1)) { | 93 | if (isFunction(v1)) { |
97 | return v1.toString() === v2.toString(); | 94 | return v1.toString() === v2.toString(); |
98 | } | 95 | } | ... | ... |
1 | /*global casper*/ | ||
2 | /*jshint strict:false maxstatements:99*/ | ||
1 | var fs = require('fs'); | 3 | var fs = require('fs'); |
2 | |||
3 | var t = casper.test; | 4 | var t = casper.test; |
4 | 5 | ||
5 | casper.start(); | 6 | casper.start(); |
... | @@ -21,9 +22,6 @@ var expected = [ | ... | @@ -21,9 +22,6 @@ var expected = [ |
21 | t.assertEquals(files, expected, 'findTestFiles() find test files and sort them'); | 22 | t.assertEquals(files, expected, 'findTestFiles() find test files and sort them'); |
22 | 23 | ||
23 | casper.thenOpen('tests/site/index.html', function() { | 24 | casper.thenOpen('tests/site/index.html', function() { |
24 | t.comment('Tester.assertField()'); | ||
25 | t.assertField('dummy_name', 'dummy_value', 'Tester.assertField() works as expected'); | ||
26 | |||
27 | t.comment('Tester.assertTextExists()'); | 25 | t.comment('Tester.assertTextExists()'); |
28 | t.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text'); | 26 | t.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text'); |
29 | 27 | ||
... | @@ -97,6 +95,27 @@ casper.then(function() { | ... | @@ -97,6 +95,27 @@ casper.then(function() { |
97 | t.assertNotVisible('p#hidden', 'Tester.assertNotVisible() works as expected'); | 95 | t.assertNotVisible('p#hidden', 'Tester.assertNotVisible() works as expected'); |
98 | }); | 96 | }); |
99 | 97 | ||
98 | casper.thenOpen('tests/site/form.html', function() { | ||
99 | t.comment('Tester.assertField()'); | ||
100 | var fpath = phantom.libraryPath + '/README.md'; | ||
101 | this.fill('form[action="result.html"]', { | ||
102 | 'email': 'chuck@norris.com', | ||
103 | 'content': 'Am watching thou', | ||
104 | 'check': 'on', | ||
105 | 'choice': 'no', | ||
106 | 'topic': 'bar', | ||
107 | 'file': fpath, | ||
108 | 'checklist[]': ['1', '3'] | ||
109 | }); | ||
110 | 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('check', 'on', 'Tester.assertField() works as expected with checkboxes'); | ||
113 | 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('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 | }); | ||
118 | |||
100 | casper.then(function() { | 119 | casper.then(function() { |
101 | t.comment('Tester.getFailures()'); | 120 | t.comment('Tester.getFailures()'); |
102 | t.assertEquals(typeof t.getFailures().length, "number", "Tester.getFailures() works as expected"); | 121 | t.assertEquals(typeof t.getFailures().length, "number", "Tester.getFailures() works as expected"); | ... | ... |
-
Please register or sign in to post a comment