Commit 1027e05d 1027e05d29342078109c1c28e7a900d66d8f46f0 by Nicolas Perriault

Merge pull request #706 from mduvall/assert-field-css-selector-redux

Add CSS selector support to assertField
2 parents 0051fe74 3ac34514
...@@ -160,9 +160,9 @@ Asserts that a given subject is `falsy <http://11heavens.com/falsy-and-truthy-in ...@@ -160,9 +160,9 @@ 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, Object options])`` 163 **Signature:** ``assertField(String|Object input, 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 with input name or :ref:`selector expression <selectors>`::
166 166
167 casper.test.begin('assertField() tests', 1, function(test) { 167 casper.test.begin('assertField() tests', 1, function(test) {
168 casper.start('http://www.google.fr/', function() { 168 casper.start('http://www.google.fr/', function() {
...@@ -173,6 +173,16 @@ Asserts that a given form field has the provided value:: ...@@ -173,6 +173,16 @@ Asserts that a given form field has the provided value::
173 }); 173 });
174 }); 174 });
175 175
176 // Path usage with type 'css'
177 casper.test.begin('assertField() tests', 1, function(test) {
178 casper.start('http://www.google.fr/', function() {
179 this.fill('form[name="gs"]', { q: 'plop' }, false);
180 test.assertField({type: 'css', path: '.q.foo'}, 'plop');
181 }).run(function() {
182 test.done();
183 });
184 });
185
176 .. versionadded:: 1.0 186 .. versionadded:: 1.0
177 187
178 This also works with any input type: ``select``, ``textarea``, etc. 188 This also works with any input type: ``select``, ``textarea``, etc.
...@@ -182,6 +192,63 @@ This also works with any input type: ``select``, ``textarea``, etc. ...@@ -182,6 +192,63 @@ This also works with any input type: ``select``, ``textarea``, etc.
182 The `options` parameter allows to set the options to use with 192 The `options` parameter allows to set the options to use with
183 :ref:`ClientUtils#getFieldValue() <clientutils_getfieldvalue>`. 193 :ref:`ClientUtils#getFieldValue() <clientutils_getfieldvalue>`.
184 194
195 `input` parameter introspects whether or not a `type` key is passed in with `xpath` or `css` and a property `path` specified along with it.
196
197 ``assertFieldName()``
198 -------------------------------------------------------------------------------
199
200 **Signature:** ``assertFieldName(String inputName, String expected[, String message, Object options])``
201
202 .. versionadded:: 1.1-beta3
203
204 Asserts that a given form field has the provided value::
205
206 casper.test.begin('assertField() tests', 1, function(test) {
207 casper.start('http://www.google.fr/', function() {
208 this.fill('form[name="gs"]', { q: 'plop' }, false);
209 test.assertField('q', 'plop', 'did not plop', {formSelector: 'plopper'});
210 }).run(function() {
211 test.done();
212 });
213 });
214
215 ``assertFieldCSS()``
216 -------------------------------------------------------------------------------
217
218 **Signature:** ``assertFieldCSS(String cssSelector, String expected, String message)``
219
220 .. versionadded:: 1.1
221
222 Asserts that a given form field has the provided value given a CSS selector::
223
224 casper.test.begin('assertField() tests', 1, function(test) {
225 casper.start('http://www.google.fr/', function() {
226 this.fill('form[name="gs"]', { q: 'plop' }, false);
227 test.assertField('q', 'plop', 'did not plop', 'input.plop');
228 }).run(function() {
229 test.done();
230 });
231 });
232
233 ``assertFieldXPath()``
234 -------------------------------------------------------------------------------
235
236 **Signature:** ``assertFieldXPath(String xpathSelector, String expected, String message)``
237
238 .. versionadded:: 1.1
239
240 Asserts that a given form field has the provided value given a XPath selector::
241
242 casper.test.begin('assertField() tests', 1, function(test) {
243 casper.start('http://www.google.fr/', function() {
244 this.fill('form[name="gs"]', { q: 'plop' }, false);
245 test.assertField('q', 'plop', 'did not plop', '/html/body/form[0]/input[1]');
246 }).run(function() {
247 test.done();
248 });
249 });
250
251
185 .. index:: HTTP, HTTP Status Code 252 .. index:: HTTP, HTTP Status Code
186 253
187 ``assertHttpStatus()`` 254 ``assertHttpStatus()``
......
...@@ -290,7 +290,7 @@ ...@@ -290,7 +290,7 @@
290 * 290 *
291 * @param String selector CSS3 selector 291 * @param String selector CSS3 selector
292 * @param HTMLElement|null scope Element to search child elements within 292 * @param HTMLElement|null scope Element to search child elements within
293 * @return NodeList|undefined 293 * @return Array|undefined
294 */ 294 */
295 this.findAll = function findAll(selector, scope) { 295 this.findAll = function findAll(selector, scope) {
296 scope = scope || this.options.scope; 296 scope = scope || this.options.scope;
...@@ -299,7 +299,7 @@ ...@@ -299,7 +299,7 @@
299 if (pSelector.type === 'xpath') { 299 if (pSelector.type === 'xpath') {
300 return this.getElementsByXPath(pSelector.path, scope); 300 return this.getElementsByXPath(pSelector.path, scope);
301 } else { 301 } else {
302 return scope.querySelectorAll(pSelector.path); 302 return Array.prototype.slice.call(scope.querySelectorAll(pSelector.path));
303 } 303 }
304 } catch (e) { 304 } catch (e) {
305 this.log('findAll(): invalid selector provided "' + selector + '":' + e, "error"); 305 this.log('findAll(): invalid selector provided "' + selector + '":' + e, "error");
...@@ -560,10 +560,19 @@ ...@@ -560,10 +560,19 @@
560 } 560 }
561 } 561 }
562 var formSelector = ''; 562 var formSelector = '';
563 if (options && options.formSelector) { 563 if (options.formSelector) {
564 formSelector = options.formSelector + ' '; 564 formSelector = options.formSelector + ' ';
565 } 565 }
566 var inputs = this.findAll(formSelector + '[name="' + inputName + '"]'); 566 var inputs = this.findAll(formSelector + '[name="' + inputName + '"]');
567
568 if (options.inputSelector) {
569 inputs = inputs.concat(this.findAll(options.inputSelector));
570 }
571
572 if (options.inputXPath) {
573 inputs = inputs.concat(this.getElementsByXPath(options.inputXPath));
574 }
575
567 switch (inputs.length) { 576 switch (inputs.length) {
568 case 0: return undefined; 577 case 0: return undefined;
569 case 1: return getSingleValue(inputs[0]); 578 case 1: return getSingleValue(inputs[0]);
......
...@@ -449,6 +449,21 @@ Tester.prototype.assertEvalEqual = function assertEvalEquals(fn, expected, messa ...@@ -449,6 +449,21 @@ Tester.prototype.assertEvalEqual = function assertEvalEquals(fn, expected, messa
449 }); 449 });
450 }; 450 };
451 451
452 function baseFieldAssert(inputName, expected, actual, message) {
453 /*jshint validthis:true */
454 "use strict";
455
456 return this.assert(utils.equals(actual, expected), message, {
457 type: 'assertField',
458 standard: f('"%s" input field has the value "%s"', inputName, expected),
459 values: {
460 inputName: inputName,
461 actual: actual,
462 expected: expected
463 }
464 });
465 }
466
452 /** 467 /**
453 * Asserts that the provided assertion fails (used for internal testing). 468 * Asserts that the provided assertion fails (used for internal testing).
454 * 469 *
...@@ -473,26 +488,67 @@ Tester.prototype.assertFail = function assertFail(fn, message) { ...@@ -473,26 +488,67 @@ Tester.prototype.assertFail = function assertFail(fn, message) {
473 /** 488 /**
474 * Asserts that a given input field has the provided value. 489 * Asserts that a given input field has the provided value.
475 * 490 *
476 * @param String inputName The name attribute of the input element 491 * @param String|Object input The name attribute of the input element
477 * @param String expected The expected value of the input element 492 * or an object with the selector
478 * @param String message Test description 493 * @param String expected The expected value of the input element
479 * @param Object options ClientUtils#getFieldValue options (optional) 494 * @param String message Test description
480 * @return Object An assertion result object 495 * @param Object options ClientUtils#getFieldValue options (optional)
481 */ 496 * @return Object An assertion result object
482 Tester.prototype.assertField = function assertField(inputName, expected, message, options) { 497 */
483 "use strict"; 498 Tester.prototype.assertField = function assertField(input, expected, message, options) {
499 "use strict";
500
501 if (typeof input === 'object') {
502 switch (input.type) {
503 case 'css':
504 return this.assertFieldCSS(input.path, expected, message);
505 case 'xpath':
506 return this.assertFieldXPath(input.path, expected, message);
507 default:
508 throw new CasperError('Invalid regexp.');
509 // no default
510 }
511 }
512
484 var actual = this.casper.evaluate(function(inputName, options) { 513 var actual = this.casper.evaluate(function(inputName, options) {
485 return __utils__.getFieldValue(inputName, options); 514 return __utils__.getFieldValue(inputName, options);
486 }, inputName, options); 515 }, input, options);
487 return this.assert(utils.equals(actual, expected), message, { 516
488 type: 'assertField', 517 return baseFieldAssert.call(this, input, expected, actual, message);
489 standard: f('"%s" input field has the value "%s"', inputName, expected), 518 };
490 values: { 519
491 inputName: inputName, 520 /**
492 actual: actual, 521 * Asserts that a given input field by CSS selector has the provided value.
493 expected: expected 522 *
494 } 523 * @param Object cssSelector The CSS selector to use for the assert field value
495 }); 524 * @param String expected The expected value of the input element
525 * @param String message Test description
526 * @return Object An assertion result object
527 */
528 Tester.prototype.assertFieldCSS = function assertFieldCSS(cssSelector, expected, message) {
529 "use strict";
530 var actual = this.casper.evaluate(function(inputName, cssSelector) {
531 return __utils__.getFieldValue(inputName, {inputSelector: cssSelector});
532 }, null, cssSelector);
533
534 return baseFieldAssert.call(this, null, expected, actual, message);
535 };
536
537 /**
538 * Asserts that a given input field by XPath selector has the provided value.
539 *
540 * @param Object xPathSelector The XPath selector to use for the assert field value
541 * @param String expected The expected value of the input element
542 * @param String message Test description
543 * @return Object An assertion result object
544 */
545 Tester.prototype.assertFieldXPath = function assertFieldXPath(xPathSelector, expected, message) {
546 "use strict";
547 var actual = this.casper.evaluate(function(inputName, xPathSelector) {
548 return __utils__.getFieldValue(inputName, {inputXPath: xPathSelector});
549 }, null, xPathSelector);
550
551 return baseFieldAssert.call(this, null, expected, actual, message);
496 }; 552 };
497 553
498 /** 554 /**
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
6 </head> 6 </head>
7 <body> 7 <body>
8 <form action="result.html" enctype="multipart/form-data"> 8 <form action="result.html" enctype="multipart/form-data">
9 <input type="text" name="email" placeholder="email"> 9 <input type="text" name="email" placeholder="email" id="email">
10 <input type="password" name="password" placeholder="password"> 10 <input type="password" name="password" placeholder="password">
11 <input type="text" name="language" placeholder="language"> 11 <input type="text" name="language" placeholder="language">
12 <input type="whatever" name="strange"> 12 <input type="whatever" name="strange">
......
...@@ -51,17 +51,17 @@ casper.test.begin('ClientUtils.exists() tests', 5, function(test) { ...@@ -51,17 +51,17 @@ casper.test.begin('ClientUtils.exists() tests', 5, function(test) {
51 casper.test.begin('ClientUtils.findAll() tests', 7, function(test) { 51 casper.test.begin('ClientUtils.findAll() tests', 7, function(test) {
52 var clientutils = require('clientutils').create(); 52 var clientutils = require('clientutils').create();
53 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>'); 53 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>');
54 test.assertType(clientutils.findAll('li'), 'nodelist', 54 test.assertType(clientutils.findAll('li'), 'array',
55 'ClientUtils.findAll() can find matching DOM elements'); 55 'ClientUtils.findAll() can find matching DOM elements');
56 test.assertEquals(clientutils.findAll('li').length, 2, 56 test.assertEquals(clientutils.findAll('li').length, 2,
57 'ClientUtils.findAll() can find matching DOM elements'); 57 'ClientUtils.findAll() can find matching DOM elements');
58 test.assertType(clientutils.findAll('ol'), 'nodelist', 58 test.assertType(clientutils.findAll('ol'), 'array',
59 'ClientUtils.findAll() can find matching DOM elements'); 59 'ClientUtils.findAll() can find matching DOM elements');
60 test.assertEquals(clientutils.findAll('ol').length, 0, 60 test.assertEquals(clientutils.findAll('ol').length, 0,
61 'ClientUtils.findAll() can find matching DOM elements'); 61 'ClientUtils.findAll() can find matching DOM elements');
62 // scoped 62 // scoped
63 var scope = clientutils.findOne('ul'); 63 var scope = clientutils.findOne('ul');
64 test.assertType(clientutils.findAll('li', scope), 'nodelist', 64 test.assertType(clientutils.findAll('li', scope), 'array',
65 'ClientUtils.findAll() can find matching DOM elements within a given scope'); 65 'ClientUtils.findAll() can find matching DOM elements within a given scope');
66 test.assertEquals(clientutils.findAll('li', scope).length, 2, 66 test.assertEquals(clientutils.findAll('li', scope).length, 2,
67 'ClientUtils.findAll() can find matching DOM elements within a given scope'); 67 'ClientUtils.findAll() can find matching DOM elements within a given scope');
......
...@@ -129,8 +129,94 @@ casper.test.begin('Tester.assertField(): nonexistent fields', 2, function(test) ...@@ -129,8 +129,94 @@ casper.test.begin('Tester.assertField(): nonexistent fields', 2, function(test)
129 test.assertFail(function() { 129 test.assertFail(function() {
130 test.assertField('nonexistent', ''); 130 test.assertField('nonexistent', '');
131 }, 'Tester.assertField() only checks for existing fields'); 131 }, 'Tester.assertField() only checks for existing fields');
132 }).run(function() {
133 test.done();
134 });
135 });
136
137 casper.test.begin('Tester.assertField(): CSS selectors', 1, function(test) {
138 casper.start('tests/site/form.html', function() {
139 this.fill('form[action="result.html"]', {
140 'email': 'albert@camus.com'
141 });
142
143 test.assertField({
144 type: 'css',
145 path: '#email'
146 },
147 'albert@camus.com',
148 'Tester.assertField() works as expected with CSS selectors'
149 );
150 }).run(function() {
151 test.done();
132 }); 152 });
133 casper.run(function() { 153 });
154
155 casper.test.begin('Tester.assertField(): XPath selectors', 1, function(test) {
156 casper.start('tests/site/form.html', function() {
157 this.fill('form[action="result.html"]', {
158 'email': 'albert@camus.com'
159 });
160
161 test.assertField({
162 type: 'xpath',
163 path: '/html/body/form[1]/input[1]'
164 },
165 'albert@camus.com',
166 'Tester.assertField() works as expected with XPath selectors'
167 );
168 }).run(function() {
134 test.done(); 169 test.done();
135 }) 170 });
171 });
172
173 casper.test.begin('Tester.assertField(): invalid selectors', 1, function(test) {
174 casper.start('tests/site/form.html', function() {
175 this.fill('form[action="result.html"]', {
176 'email': 'albert@camus.com'
177 });
178
179 test.assertRaise(function() {
180 test.assertField({
181 type: 'albert'
182 },
183 'albert@camus.com',
184 'Tester.assertField() works as expected with XPath selectors'
185 );
186 }, [], 'should throw an error for an invalid selector');
187 }).run(function() {
188 test.done();
189 });
190 });
191
192 casper.test.begin('Tester.assertFieldCSS(): CSS selectors', 1, function(test) {
193 casper.start('tests/site/form.html', function() {
194 this.fill('form[action="result.html"]', {
195 'email': 'albert@camus.com'
196 });
197
198 test.assertFieldCSS(
199 '#email',
200 'albert@camus.com',
201 'Tester.assertFieldCSS() works as expected with CSS selectors'
202 );
203 }).run(function() {
204 test.done();
205 });
206 });
207
208 casper.test.begin('Tester.assertFieldXPath(): XPath selectors', 1, function(test) {
209 casper.start('tests/site/form.html', function() {
210 this.fill('form[action="result.html"]', {
211 'email': 'albert@camus.com'
212 });
213
214 test.assertFieldXPath(
215 '/html/body/form[1]/input[1]',
216 'albert@camus.com',
217 'Tester.assertFieldXPath() works as expected with XPath selectors'
218 );
219 }).run(function() {
220 test.done();
221 });
136 }); 222 });
......