Here's a selection of the most frequently asked questions by casperjs newcomers.
I'm stuck! What can I do?
Your best bet is probably to ask for help on the CasperJS discussion group
What is the versioning policy of CasperJS?
Releases will follow the SemVer standard; they will be numbered with the follow format:
<major>.<minor>.<patch>[-<identifier>]
And constructed with the following guidelines:
- Breaking backwards compatibility bumps the major
- New additions without breaking backwards compatibility bumps the minor
- Bug fixes and misc changes bump the patch
- Unstable, special and trunk versions will have a proper identifier
I'm getting TypeError: 'undefined' is not a function; WTF?
Unfortunately PhantomJS ships with a QtWebKit JS engine which would need quite a serious upgrade to have more explicit error messages, stack traces, location, etc.
I cannot do much to make the situation really better, unfortunately :/
Can I use jQuery with CasperJS?
Sure, as every single other javascript library on Earth.
A first solution is to inject it into the remote DOM environment by
hand using the standard WebPage.injectJs()
method:
casper.page.injectJs('/path/to/jquery.js');
If you need jQuery being available everytime, you can also make it being
injected in every received response by setting the clientScripts
option of CasperJS:
var casper = require('casper').create({
clientScripts: ["includes/jquery.min.js"]
});
Note You can't inject scripts using the HTTP protocol, you actually have to use a relative/absolute filesystem path to the script resource.
Can I use CasperJS without using the casperjs
executable?
Yes, you can call a CasperJS script directly with the phantomjs
executable, but if you do so, you must set the phantom.casperPath
property to the path where the library root is located on your system:
// casperscript.js
phantom.casperPath = '/path/to/casperjs';
phantom.injectJs(phantom.casperPath + '/bin/bootstrap.js');
var casper = require('casper').create();
// ...
You can run such a script like any other standard PhantomJS script:
$ phantomjs casperscript.js
If you're on Windows, this is the way you may manage to get casper working the most easily:
phantom.casperPath = 'C:\\path\\to\\your\\repo\\lib\\casperjs-0.6.X';
phantom.injectJs(phantom.casperPath + '\\bin\\bootstrap.js');
var casper = require('casper').create();
// do stuff
How can I catch HTTP 404 and other status codes?
You can define your own
HTTP status code
handlers by using the httpStatusHandlers
option of the Casper object. You can
also catch other HTTP status codes as well, as demoed below:
var casper = require('casper').create();
casper.on('http.status.404', function(resource) {
this.echo('wait, this url is 404: ' + resource.url);
});
casper.on('http.status.500', function(resource) {
this.echo('woops, 500 error: ' + resource.url);
});
casper.start('http://mywebsite/404', function() {
this.echo('We suppose this url return an HTTP 404');
});
casper.thenOpen('http://mywebsite/500', function() {
this.echo('We suppose this url return an HTTP 500');
});
casper.run(function() {
this.echo('Done.').exit();
});
Hint Check out all the other cool events you may use as well.