“QUALITY MEANS DOING IT RIGHT, EVEN WHEN NO ONE IS LOOKING”
With the help of casperJS, automated testing for web application become very easy and normal tests can be done in a minute and that too, there is no need of traditional browsers and human intervention. It accomplishes the tests without invoking any browser action like scroll or scale the page, follow a link, or click and submission of a form etc.
Installation: It has some prerequisites as below.
- PhantomJS 1.9.1 or greater.
- Python 2.6 or greater.
- NodeJS (if using npm to install)
You can install CasperJS using npm (NodeJS Package Manager):
$ npm install casperjs
It will install casperjs on your system. Now you can run any casperjs file from any where but currently you have to give full path of installation directory. Below command will make it accessible from any where just a simple command-
$ ln -sf /repo/casperjs/bin/casperjs /bin/casperjs
The above directory structure may differ from system to system or OS to OS, please take care of this.
You can also install if directly from github repository by cloing it on your system
$ git clone git://github/com/casperjs/casperjs.git
Writing your first script: Here is the small script that will get the title of the given website and further it can be used for many manipulation or testing cases. For example, using the title you can test that your site in running or not.
In below script, I’m simply fetching google’s home page title-
Now, how to run this script and what will be the output. Below is the screenshot of the command with the result displayed-
There lots of other things that one can do with casperjs, another example is how to trigger a form by casperjs-
Below code is written test opencart’s website for front-end login and admin login. It is taking total 5 arguments website url, customer email, customer password, admin username and admin password.
Below is screenshot, in which you can see the output of above code.