CasperJS useful code snippets

Helper Snippets to automate and test stuff

This page has a list of handy snippets we use when automating stuff through CasperJS. If you’re not familiar with CasperJS please have a look at Write simple tests with CasperJS

Click a button, link or anything that has a click event

casper.then(function () {
    // Click the button with id talk'#talk');

How to catch all the errors in the browser console

casper.on("resource.error", function(resourceError) {
    this.echo("Resource error: " + "Error code: "+resourceError.errorCode+" ErrorString: "+resourceError.errorString+" url: "+resourceError.url+" id: ", "ERROR");

Fill text areas and input fields on a page

casper.then(function () {
    // Set a name of the input field with following class name. Reset will clear the previous value first   
    this.sendKeys('input[class="usernameInput"]', 'John', { reset: true });

How to take a screenshot of a page with CasperJS

casper.start('', function() {
    this.capture('dictionary.png', {
        top: 100,
        left: 100,
        width: 500,
        height: 400

Leave A Comment