Commit 0fe5118d 0fe5118df365971ef91e486d3265e17c8872f313 by Nicolas Perriault

Merge remote-tracking branch 'andrewchilds/ac/added-assertions' into pr-197

2 parents 8d4083b9 637d7128
...@@ -238,6 +238,30 @@ var Tester = function Tester(casper, options) { ...@@ -238,6 +238,30 @@ var Tester = function Tester(casper, options) {
238 }; 238 };
239 239
240 /** 240 /**
241 * Asserts that a given input field has the provided value.
242 *
243 * @param String input_name The name attribute of the input element
244 * @param String expected_value The expected value of the input element
245 * @param String message Test description
246 * @return Object An assertion result object
247 */
248 this.assertField = function assertField(input_name, expected_value, message) {
249 var actual_value = casper.evaluate(function(input_name) {
250 var input = document.querySelector('input[name="' + input_name + '"]');
251 return input ? input.value : null;
252 }, { input_name: input_name });
253 return this.assert(this.testEquals(actual_value, expected_value), message, {
254 type: 'assertField',
255 standard: f('%s input field has the value %s', this.colorize(input_name, 'COMMENT'), this.colorize(expected_value, 'COMMENT')),
256 values: {
257 input_name: input_name,
258 actual_value: actual_value,
259 expected_value: expected_value
260 }
261 });
262 };
263
264 /**
241 * Asserts that an element matching the provided selector expression exists in 265 * Asserts that an element matching the provided selector expression exists in
242 * remote DOM. 266 * remote DOM.
243 * 267 *
...@@ -372,7 +396,7 @@ var Tester = function Tester(casper, options) { ...@@ -372,7 +396,7 @@ var Tester = function Tester(casper, options) {
372 }; 396 };
373 397
374 /** 398 /**
375 * Asserts that given text exits in the document body. 399 * Asserts that given text exists in the document body.
376 * 400 *
377 * @param String text Text to be found 401 * @param String text Text to be found
378 * @param String message Test description 402 * @param String message Test description
...@@ -392,6 +416,46 @@ var Tester = function Tester(casper, options) { ...@@ -392,6 +416,46 @@ var Tester = function Tester(casper, options) {
392 }; 416 };
393 417
394 /** 418 /**
419 * Asserts that given text exists in the provided selector.
420 *
421 * @param String selector Selector expression
422 * @param String text Text to be found
423 * @param String message Test description
424 * @return Object An assertion result object
425 */
426 this.assertSelectorHasText = function assertSelectorHasText(selector, text, message) {
427 var textFound = casper.fetchText(selector).indexOf(text) !== -1;
428 return this.assert(textFound, message, {
429 type: "assertTextInSelector",
430 standard: f('Found %s within the selector %s', this.colorize(text, 'COMMENT'), this.colorize(selector, 'COMMENT')),
431 values: {
432 selector: selector,
433 text: text
434 }
435 });
436 };
437
438 /**
439 * Asserts that given text does not exist in the provided selector.
440 *
441 * @param String selector Selector expression
442 * @param String text Text not to be found
443 * @param String message Test description
444 * @return Object An assertion result object
445 */
446 this.assertSelectorDoesntHaveText = function assertSelectorDoesntHaveText(selector, text, message) {
447 var textFound = casper.fetchText(selector).indexOf(text) === -1;
448 return this.assert(textFound, message, {
449 type: "assertNoTextInSelector",
450 standard: f('Did not find %s within the selector %s', this.colorize(text, 'COMMENT'), this.colorize(selector, 'COMMENT')),
451 values: {
452 selector: selector,
453 text: text
454 }
455 });
456 };
457
458 /**
395 * Asserts that title of the remote page equals to the expected one. 459 * Asserts that title of the remote page equals to the expected one.
396 * 460 *
397 * @param String expected The expected title string 461 * @param String expected The expected title string
......
...@@ -13,5 +13,7 @@ ...@@ -13,5 +13,7 @@
13 <li>two</li> 13 <li>two</li>
14 <li>three</li> 14 <li>three</li>
15 </ul> 15 </ul>
16 <input type="text" name="dummy_name" value="dummy_value" />
17 <h1>Title</h1>
16 </body> 18 </body>
17 </html> 19 </html>
......
...@@ -45,8 +45,17 @@ var expected = [ ...@@ -45,8 +45,17 @@ var expected = [
45 t.assertEquals(files, expected, 'findTestFiles() find test files and sort them'); 45 t.assertEquals(files, expected, 'findTestFiles() find test files and sort them');
46 46
47 casper.thenOpen('tests/site/index.html', function() { 47 casper.thenOpen('tests/site/index.html', function() {
48 t.comment('Tester.assertField()');
49 t.assertField('dummy_name', 'dummy_value', 'Tester.assertField() works as expected');
50
48 t.comment('Tester.assertTextExists()'); 51 t.comment('Tester.assertTextExists()');
49 t.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text'); 52 t.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text');
53
54 t.comment('Tester.assertSelectorHasText()');
55 t.assertSelectorHasText('h1', 'Title', 'Tester.assertSelectorHasText() works as expected');
56
57 t.comment('Tester.assertSelectorDoesntHaveText()');
58 t.assertSelectorDoesntHaveText('h1', 'Subtitle', 'Tester.assertSelectorDoesntHaveText() works as expected');
50 }); 59 });
51 60
52 casper.then(function() { 61 casper.then(function() {
......