Book Image

Instant Testing with CasperJS

By : Eric Brehault
Book Image

Instant Testing with CasperJS

By: Eric Brehault

Overview of this book

Professional web development implies systematic testing. While JavaScript unit tests will validate your JavaScript library’s quality, web functional testing is the only way to guarantee the expected behavior of your web pages. CasperJS is a fast and simple JavaScript testing API that can run on any platform, and it is currently one of the best and easiest ways to write your functional tests. Instant Testing with CasperJS will teach you how to write efficient and accurate tests for your professional web developments. This practical guide explains the various different CasperJS principles through clear and detailed examples, covering a large set of common use cases. This book will progressively cover everything you need to know from CasperJS basic principles to the most advanced testing practices. This book starts off by introducing you to the different testing assertions that you can perform with the CasperJS API. We will then move on to cover why bad timing between event triggering can ruin tests and learn strategies to avoid it. Finally, you will learn how to test efficient and complex web interactions like drag and drop, authentication, and file uploading. With Instant Testing with CasperJS, you will be able to set up an advanced and functional test suite for your web development projects quickly and efficiently.
Table of Contents (7 chapters)


CasperJS is a fantastic command-line utility offering a wide JavaScript API to script and test any web page. Unlike Selenium, CasperJS does not automate a locally installed web browser; it behaves as an actual independent web browser.

To automate a locally installed web browser, CasperJS uses PhantomJS, which is a headless WebKit engine. Well, "headless WebKit engine" might sound like quite a barbaric term, but just imagine this as a WebKit engine (the famous web engine of Chrome and Safari) without a graphical user interface.

So it is basically a web browser. It can load web pages, apply their CSS, run their JavaScript, interact with their content, and so on; but when we launch it from a command line, we do not see anything on the screen.

Instead of a standard graphical user interface, PhantomJS offers a JavaScript API so we can control what the browser is doing. By running its own web engine instead of automating an external web browser, PhantomJS is much faster and lighter than Selenium.

Unfortunately, the PhantomJS JavaScript API is very low level. So by writing a simple interaction scenario rapidly, you could end up with quite a heavy and long script code.

This is precisely where CasperJS makes its entrance. It comes with two precious assets. Firstly, with a large, high-level API to perform actions such as clicking links, filling forms, and finding elements; and secondly, with the ability to execute those actions sequentially without requiring an endless cascade of JavaScript callbacks.

Thanks to this, our CasperJS scripts will be just as concise and readable as we wish, which is priceless when dealing with testing.

CasperJS was created by Nicolas Perriault in 2011, and he is still actively maintaining it. All the CasperJS users are very grateful for it. Thank you Nicolas!

What this book covers

Installing CasperJS (Simple) explains how to properly install PhantomJS and CasperJS plus their system dependencies needed on our machine, step by step.

Getting started with CasperJS (Simple) covers how to write basic CasperJS tests. Starting with simple scripts, we will progressively get familiar with the CasperJS approach, discover what kind of assertions can be performed with CasperJS, learn how to establish the proper timing to get an accurate test, and how to record a web session as a CasperJS script.

Writing advanced tests (Intermediate) details how to simulate rich web interactions using CasperJS in order to achieve more complex testing, such as handling files, managing authentication, or simulating keyboard and mouse events efficiently.

Best practices (Intermediate) focuses on making our tests more accurate and robust and explains how to run them using continuous integration (CI) tools, such as Jenkins or Travis CI.

Beyond testing (Advanced) discusses other interesting CasperJS usages such as web scripting and automated screenshot or server-side printing.

What you need for this book

To execute the examples contained in this book, you will need a computer compatible with Windows, Mac OS X, or Linux, with sufficient rights to be able to install new software.

Who this book is for

This book assumes that you are familiar with web development and have a good knowledge of JavaScript.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can print log messages using the log method."

A block of code is set as follows:

casper.start('', function() {

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

casper.start('', function() {

Any command-line input or output is written as follows:

~ casperjs test.js

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "It shows a Click me button, and if we click on it, its label is changed to Done."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.