Commit 067b6dcf 067b6dcf9e45d9e0e08f25cc3e9fc039ea53ae55 by Nicolas Perriault

added Tester.assertElementCount()

1 parent 41710652
......@@ -281,6 +281,35 @@ Tester.prototype.assertNotEquals = function assertNotEquals(subject, shouldnt, m
};
/**
* Asserts that a selector expression matches n elements.
*
* @param Mixed selector A selector expression
* @param Number count Expected number of matching elements
* @param String message Test description (Optional)
* @return Object An assertion result object
*/
Tester.prototype.assertElementCount = function assertElementCount(selector, count, message) {
"use strict";
if (!utils.isNumber(count) || count < 0) {
throw new CasperError('assertElementCount() needs a positive integer count');
}
return this.assert(this.casper.evaluate(function(selector) {
try {
return __utils__.findAll(selector).length;
} catch (e) {
return -1;
}
}, selector) === count, message, {
type: "assertElementCount",
standard: f("%d matching element(s) found", count),
values: {
selector: selector,
count: count
}
});
};
/**
* Asserts that a code evaluation in remote DOM resolves to true.
*
* @param Function fn A function to be evaluated in remote DOM
......
......@@ -2,7 +2,7 @@
/*jshint strict:false maxstatements:99*/
var fs = require('fs');
casper.test.begin('Common assertions tests', 40, function(test) {
casper.test.begin('Common assertions tests', 43, function(test) {
casper.start('tests/site/index.html', function() {
test.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text');
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) {
test.assertEvalEqual(function() {
return 42;
}, 42, 'Tester.assertEvalEqual() works as expected [alias]');
test.assertElementCount('ul', 1, 'Tester.assertElementCount() works as expected');
test.assertElementCount('li', 3, 'Tester.assertElementCount() works as expected');
test.assertElementCount('address', 0, 'Tester.assertElementCount() works as expected');
test.assertExists('body', 'Tester.assertExists() works as expected');
test.assertExist('body', 'Tester.assertExist() works as expected [alias]');
test.assertSelectorExists('body', 'Tester.assertSelectorExists() works as expected [alias]');
......