added Tester.assertElementCount()
Showing
2 changed files
with
33 additions
and
1 deletions
... | @@ -281,6 +281,35 @@ Tester.prototype.assertNotEquals = function assertNotEquals(subject, shouldnt, m | ... | @@ -281,6 +281,35 @@ Tester.prototype.assertNotEquals = function assertNotEquals(subject, shouldnt, m |
281 | }; | 281 | }; |
282 | 282 | ||
283 | /** | 283 | /** |
284 | * Asserts that a selector expression matches n elements. | ||
285 | * | ||
286 | * @param Mixed selector A selector expression | ||
287 | * @param Number count Expected number of matching elements | ||
288 | * @param String message Test description (Optional) | ||
289 | * @return Object An assertion result object | ||
290 | */ | ||
291 | Tester.prototype.assertElementCount = function assertElementCount(selector, count, message) { | ||
292 | "use strict"; | ||
293 | if (!utils.isNumber(count) || count < 0) { | ||
294 | throw new CasperError('assertElementCount() needs a positive integer count'); | ||
295 | } | ||
296 | return this.assert(this.casper.evaluate(function(selector) { | ||
297 | try { | ||
298 | return __utils__.findAll(selector).length; | ||
299 | } catch (e) { | ||
300 | return -1; | ||
301 | } | ||
302 | }, selector) === count, message, { | ||
303 | type: "assertElementCount", | ||
304 | standard: f("%d matching element(s) found", count), | ||
305 | values: { | ||
306 | selector: selector, | ||
307 | count: count | ||
308 | } | ||
309 | }); | ||
310 | }; | ||
311 | |||
312 | /** | ||
284 | * Asserts that a code evaluation in remote DOM resolves to true. | 313 | * Asserts that a code evaluation in remote DOM resolves to true. |
285 | * | 314 | * |
286 | * @param Function fn A function to be evaluated in remote DOM | 315 | * @param Function fn A function to be evaluated in remote DOM | ... | ... |
... | @@ -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', 40, function(test) { | 5 | casper.test.begin('Common assertions tests', 43, 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]'); |
... | @@ -29,6 +29,9 @@ casper.test.begin('Common assertions tests', 40, function(test) { | ... | @@ -29,6 +29,9 @@ casper.test.begin('Common assertions tests', 40, function(test) { |
29 | test.assertEvalEqual(function() { | 29 | test.assertEvalEqual(function() { |
30 | return 42; | 30 | return 42; |
31 | }, 42, 'Tester.assertEvalEqual() works as expected [alias]'); | 31 | }, 42, 'Tester.assertEvalEqual() works as expected [alias]'); |
32 | test.assertElementCount('ul', 1, 'Tester.assertElementCount() works as expected'); | ||
33 | test.assertElementCount('li', 3, 'Tester.assertElementCount() works as expected'); | ||
34 | test.assertElementCount('address', 0, 'Tester.assertElementCount() works as expected'); | ||
32 | test.assertExists('body', 'Tester.assertExists() works as expected'); | 35 | test.assertExists('body', 'Tester.assertExists() works as expected'); |
33 | test.assertExist('body', 'Tester.assertExist() works as expected [alias]'); | 36 | test.assertExist('body', 'Tester.assertExist() works as expected [alias]'); |
34 | test.assertSelectorExists('body', 'Tester.assertSelectorExists() works as expected [alias]'); | 37 | test.assertSelectorExists('body', 'Tester.assertSelectorExists() works as expected [alias]'); | ... | ... |
-
Please register or sign in to post a comment