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 assertions. Instead of using Casper's Tester you can use (in this case with mocha-casperjs 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
});
});
});
Examples
Say we have a page like this:
Casper-Chai Example Sign In Home Blog Using Chai in your casper tests Hello Need help? Help
Here are some assertions that pass
tomatchTitle'Casper-Chai Example'shouldmatchTitle'site.css'shouldbeloaded'body > header'shouldbeinDOM'document.querySelectorAll('li').length'shouldevaluatetobeabove2'#help-link'shouldhave0and'ul.breadcrumbs li'shouldcontainanelementwith'.greeting'shouldhavetagName'div' 'span''li[aria-selected]'shouldcontaintext'Using Chai''header a'shouldhavetext/Sign/'.greeting'shouldnothavetext/Bye/
Here are some assertions that fail
'li'shouldhave'li'shouldnothaveanelementwith'.greeting'shouldnothavetagName'span''.help'shouldhavetext'Help''li[aria-selected]'shouldhavetext'Using Chai'
See the full documentation and more examples and check out the unit tests.
Installation
Casper-Chai can be installed with npm using npm install casper-chai
, or
including
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 develop and test casper-chai locally, clone the project and run npm install
to get dependencies
(which, obviously, requires npm to be installed). You will need cake
to generate documentation - which should be possible by running npm install -g coffee-script
. Also make sure you have the latest version of casperjs installed.