Commit 81f8744b 81f8744b57a37ef2f2aafe1b9d0d30e8d1cb377a by Nicolas Perriault

fixes #433 - Tester#assertField check for nonexistent fields

1 parent 3787e0af
...@@ -104,11 +104,12 @@ Last, all the casper test suites have been upgraded to use the new testing featu ...@@ -104,11 +104,12 @@ Last, all the casper test suites have been upgraded to use the new testing featu
104 ### Bugfixes & enhancements 104 ### Bugfixes & enhancements
105 105
106 - heavy lifting of casperjs bootstrap script 106 - heavy lifting of casperjs bootstrap script
107 - fixed [#387](https://github.com/n1k0/casperjs/issues/387) - Setting viewport isn't quite synchronous
108 - fixed [#410](https://github.com/n1k0/casperjs/issues/410) - trigger `mousedown` and `mousedown` events on click
109 - fixed [#433](https://github.com/n1k0/casperjs/issues/433) - `assertField("field", "")` will always pass even though the `field` doesn't exist
107 - closed [#392](https://github.com/n1k0/casperjs/issues/392) - `--direct` & `--log-level` options available for the `casperjs` executable 110 - closed [#392](https://github.com/n1k0/casperjs/issues/392) - `--direct` & `--log-level` options available for the `casperjs` executable
108 - closed [#350](https://github.com/n1k0/casperjs/issues/350) - Add a [`Casper#waitForSelectorTextChange()`](http://docs.casperjs.org/en/latest/modules/casper.html#waitforselectortextchange) method 111 - closed [#350](https://github.com/n1k0/casperjs/issues/350) - Add a [`Casper#waitForSelectorTextChange()`](http://docs.casperjs.org/en/latest/modules/casper.html#waitforselectortextchange) method
109 - fixed [#387](https://github.com/n1k0/casperjs/issues/387) - Setting viewport isn't quite synchronous
110 - Added [`Casper#bypass`](http://docs.casperjs.org/en/latest/modules/casper.html#bypass), [`Casper#thenBypass`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypass), [`Casper#thenBypassIf`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypassif), [`Casper#thenBypassUnless`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypassunless) methods 112 - Added [`Casper#bypass`](http://docs.casperjs.org/en/latest/modules/casper.html#bypass), [`Casper#thenBypass`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypass), [`Casper#thenBypassIf`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypassif), [`Casper#thenBypassUnless`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypassunless) methods
111 - fixes [#410](https://github.com/n1k0/casperjs/issues/410) - trigger `mousedown` and `mousedown` events on click
112 - Added [`Tester#skip`](http://docs.casperjs.org/en/latest/modules/tester.html#skip) method 113 - Added [`Tester#skip`](http://docs.casperjs.org/en/latest/modules/tester.html#skip) method
113 - Added [`Casper#eachThen()`](http://docs.casperjs.org/en/latest/modules/casper.html#eachThen) 114 - Added [`Casper#eachThen()`](http://docs.casperjs.org/en/latest/modules/casper.html#eachThen)
114 - `cli`: Now dropping an arg or an option will be reflected in their *raw* equivalent 115 - `cli`: Now dropping an arg or an option will be reflected in their *raw* equivalent
......
...@@ -242,12 +242,14 @@ Retrieves all DOM elements matching a given :ref:`XPath expression <selectors>`, ...@@ -242,12 +242,14 @@ Retrieves all DOM elements matching a given :ref:`XPath expression <selectors>`,
242 242
243 The ``scope`` argument allows to set the context for executing the XPath query. 243 The ``scope`` argument allows to set the context for executing the XPath query.
244 244
245 .. _clientutils_getfieldvalue:
246
245 .. index:: Form 247 .. index:: Form
246 248
247 ``getFieldValue()`` 249 ``getFieldValue()``
248 ------------------------------------------------------------------------------- 250 -------------------------------------------------------------------------------
249 251
250 **Signature:** ``getFieldValue(String inputName)`` 252 **Signature:** ``getFieldValue(String inputName[, Object options])``
251 253
252 .. versionadded:: 1.0 254 .. versionadded:: 1.0
253 255
...@@ -263,6 +265,10 @@ Using the ``getFieldValue()`` method for ``plop``:: ...@@ -263,6 +265,10 @@ Using the ``getFieldValue()`` method for ``plop``::
263 265
264 __utils__.getFieldValue('plop'); // 42 266 __utils__.getFieldValue('plop'); // 42
265 267
268 Options:
269
270 - ``formSelector``: allows to set the selector for the form containing the target field.
271
266 .. index:: Form 272 .. index:: Form
267 273
268 ``getFormValues()`` 274 ``getFormValues()``
......
...@@ -160,7 +160,7 @@ Asserts that a given subject is `falsy <http://11heavens.com/falsy-and-truthy-in ...@@ -160,7 +160,7 @@ Asserts that a given subject is `falsy <http://11heavens.com/falsy-and-truthy-in
160 ``assertField()`` 160 ``assertField()``
161 ------------------------------------------------------------------------------- 161 -------------------------------------------------------------------------------
162 162
163 **Signature:** ``assertField(String inputName, String expected[, String message])`` 163 **Signature:** ``assertField(String inputName, String expected[, String message, Object options])``
164 164
165 Asserts that a given form field has the provided value:: 165 Asserts that a given form field has the provided value::
166 166
...@@ -177,6 +177,11 @@ Asserts that a given form field has the provided value:: ...@@ -177,6 +177,11 @@ Asserts that a given form field has the provided value::
177 177
178 This also works with any input type: ``select``, ``textarea``, etc. 178 This also works with any input type: ``select``, ``textarea``, etc.
179 179
180 .. versionadded:: 1.1
181
182 The `options` parameter allows to set the options to use with
183 :ref:`ClientUtils#getFieldValue() <clientutils_getfieldvalue>`.
184
180 .. index:: HTTP, HTTP Status Code 185 .. index:: HTTP, HTTP Status Code
181 186
182 ``assertHttpStatus()`` 187 ``assertHttpStatus()``
......
...@@ -501,7 +501,7 @@ ...@@ -501,7 +501,7 @@
501 } 501 }
502 var inputs = this.findAll(formSelector + '[name="' + inputName + '"]'); 502 var inputs = this.findAll(formSelector + '[name="' + inputName + '"]');
503 switch (inputs.length) { 503 switch (inputs.length) {
504 case 0: return null; 504 case 0: return undefined;
505 case 1: return getSingleValue(inputs[0]); 505 case 1: return getSingleValue(inputs[0]);
506 default: return getMultipleValues(inputs); 506 default: return getMultipleValues(inputs);
507 } 507 }
......
...@@ -420,18 +420,40 @@ Tester.prototype.assertEvalEqual = function assertEvalEquals(fn, expected, messa ...@@ -420,18 +420,40 @@ Tester.prototype.assertEvalEqual = function assertEvalEquals(fn, expected, messa
420 }; 420 };
421 421
422 /** 422 /**
423 * Asserts that the provided assertion fails (used for internal testing).
424 *
425 * @param Function fn A closure calling an assertion
426 * @param String|null message Test description
427 * @return Object An assertion result object
428 */
429 Tester.prototype.assertFail = function assertFail(fn, message) {
430 "use strict";
431 var failed = false;
432 try {
433 fn();
434 } catch (e) {
435 failed = true;
436 }
437 return this.assert(failed, message, {
438 type: "assertFail",
439 standard: "Assertion fails as expected"
440 });
441 };
442
443 /**
423 * Asserts that a given input field has the provided value. 444 * Asserts that a given input field has the provided value.
424 * 445 *
425 * @param String inputName The name attribute of the input element 446 * @param String inputName The name attribute of the input element
426 * @param String expected The expected value of the input element 447 * @param String expected The expected value of the input element
427 * @param String message Test description 448 * @param String message Test description
449 * @param Object options ClientUtils#getFieldValue options (optional)
428 * @return Object An assertion result object 450 * @return Object An assertion result object
429 */ 451 */
430 Tester.prototype.assertField = function assertField(inputName, expected, message) { 452 Tester.prototype.assertField = function assertField(inputName, expected, message, options) {
431 "use strict"; 453 "use strict";
432 var actual = this.casper.evaluate(function(inputName) { 454 var actual = this.casper.evaluate(function(inputName, options) {
433 return __utils__.getFieldValue(inputName); 455 return __utils__.getFieldValue(inputName, options);
434 }, inputName); 456 }, inputName, options);
435 return this.assert(utils.equals(actual, expected), message, { 457 return this.assert(utils.equals(actual, expected), message, {
436 type: 'assertField', 458 type: 'assertField',
437 standard: f('"%s" input field has the value "%s"', inputName, expected), 459 standard: f('"%s" input field has the value "%s"', inputName, expected),
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
2 /*jshint strict:false, maxstatements:99*/ 2 /*jshint strict:false, maxstatements:99*/
3 var fs = require('fs'); 3 var fs = require('fs');
4 4
5 casper.test.begin('Common assertions tests', 43, function(test) { 5 casper.test.begin('Common assertions tests', 45, function(test) {
6 casper.start('tests/site/index.html', function() { 6 casper.start('tests/site/index.html', function() {
7 test.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text'); 7 test.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text');
8 test.assertTextExist('form', 'Tester.assertTextExist() checks that page body contains text [alias]'); 8 test.assertTextExist('form', 'Tester.assertTextExist() checks that page body contains text [alias]');
...@@ -34,6 +34,9 @@ casper.test.begin('Common assertions tests', 43, function(test) { ...@@ -34,6 +34,9 @@ casper.test.begin('Common assertions tests', 43, function(test) {
34 test.assertElementCount('address', 0, 'Tester.assertElementCount() works as expected'); 34 test.assertElementCount('address', 0, 'Tester.assertElementCount() works as expected');
35 test.assertExists('body', 'Tester.assertExists() works as expected'); 35 test.assertExists('body', 'Tester.assertExists() works as expected');
36 test.assertExist('body', 'Tester.assertExist() works as expected [alias]'); 36 test.assertExist('body', 'Tester.assertExist() works as expected [alias]');
37 test.assertFail(function() {
38 test.assert(false);
39 }, 'Tester.assertFail() tests for a failing assertion');
37 test.assertSelectorExists('body', 'Tester.assertSelectorExists() works as expected [alias]'); 40 test.assertSelectorExists('body', 'Tester.assertSelectorExists() works as expected [alias]');
38 test.assertSelectorExist('body', 'Tester.assertSelectorExist() works as expected [alias]'); 41 test.assertSelectorExist('body', 'Tester.assertSelectorExist() works as expected [alias]');
39 test.assertDoesntExist('foobar', 'Tester.assertDoesntExist() works as expected'); 42 test.assertDoesntExist('foobar', 'Tester.assertDoesntExist() works as expected');
...@@ -114,3 +117,14 @@ casper.test.begin('Tester.assertField(): unfilled inputs', 7, function(test) { ...@@ -114,3 +117,14 @@ casper.test.begin('Tester.assertField(): unfilled inputs', 7, function(test) {
114 test.done(); 117 test.done();
115 }); 118 });
116 }); 119 });
120
121 casper.test.begin('Tester.assertField(): nonexistent fields', 2, function(test) {
122 casper.start('tests/site/form.html', function() {
123 test.assertFail(function() {
124 test.assertField('nonexistent', '');
125 }, 'Tester.assertField() only checks for existing fields');
126 });
127 casper.run(function() {
128 test.done();
129 })
130 });
......