Casper.JS Assertions for Chai
Casper–Chai provides a set of custom assertions for use with CasperJS.
You get all the benefits of Chai to test with CasperJS.
It is an alternative to Casper's built-in Tester. Instead of using
Casper's Tester you can use (in this case with Mocha and Chai):
describe("my page", function () {
it("can be opened by Casper", function () {
casper.open("http://www.google.com")
casper.then(function () {
expect(casper.currentHTTPStatus).to.equal(200);
});
casper.then(function () {
expect("Google").to.matchTitle
});
});
});
Tests
Name | Description - passes when ... | Example |
---|
attr(attr_name) | exactly one element matching the selector has the given attribute | expect("#header_a").to.have.attr('target') |
attrAny(attr_name) | any element matching the selector has the given attribute | expect(".menu div").to.have.attr('data-bind') |
attrAll(attr_name) | all elements matching selector have the given attribute | expect('.menu div a').to.have.attr('href') |
fieldValue(value) | the named input provided has the given value | "field_name".should.have.fieldValue("someValue") |
inDOM | when the given selector is in the DOM | expect('#header').to.be.inDOM |
loaded | when the given resource exists | expect('styles.css').to.be.loaded |
matchCurrentUrl | the current URL matches | expect(/https:\/\//).to.matchCurrentUrl
|
matchOnRemote | compare the remote evaluation to the given expression | expect("typeof jQuery").to.matchOnRemote('undefined') |
matchTitle | the current Title matches | expect(/Google/).to.matchTitle |
tagName(valid_tags) | all elements matching the selectors are one of the given tags | expect('.menu *').to.have.tagName(['div', 'span']) |
textInDOM | the text can be found in the DOM | expect('About google').to.be.textInDOM |
textMatch(expression) |
the text of the given selector matches the expression (a string or regular expression).
| expect('#my_header').to.have.textMatch("My Header") |
trueOnRemote | the remote expression evaluates to something truthy | expect("true").to.be.trueOnRemote |
visible | the selector matches a visible element | expect('#my_header').to.be.visible |
More documentation and examples.
For even more examples, if you are cool with
CoffeeScript, check out the unit
tests.
Installation
Casper-Chai can be installed with npm using npm install casper-chai
, or
including
build/casper-chai.js
in a directory require
will find it.
Add extensions to Chai with:
casper_chai = require('casper-chai');
chai.use(casper_chai);
To build locally, clone the project and run cake toast test
in the
project directory. You may have to run npm install
to get dependencies
(which, obviously, requires npm to be installed), and make sure cake
is
available - which should be possible by running npm install -g coffee-script
.