Skip to main content

Web Functional Testing Automation using CasperJS and Nodejs

Background

We are in era the of Web, Mobility and AI. Days are gone where we used to think Javascript is just for client side validation check before submitting web forms. Thanks to ECMAScript5 and above which advanced javascript language way forward. Many Enterprises across the world already building applications using lot of opensource Javascript technologies. 

Automating the Testing of any web application is becoming challenging and critical for the business. Challenge is in terms of manage and maintain test hygiene throughout the application life cycle and overall product quality. The ideal behavior of any test automation is to be able to integrate as part of CI and CD (Continuous Deployment) build and be able to run without manual test run effort and publish test results with code coverage metrics.

There are many different frameworks available in the market to solve automation problem. To name few: 
  • CodedUI - Browser based test automation suite on .net
  • Selenium - Browser based test automation suite comes with WebDriver and IDE.
  • Jasmin - is a behavior-driven JavaScript framework for testing JavaScript code
  • Protractor - An automated testing framework for testing AngularJs applications in the browser.
  • CasperJS - An open-source navigation scripting and testing utility written in JavaScript for the PhantomJS WebKit headless browser
  • NemoJS - is an open-source, Node.js automation framework that was developed by PayPal 
  • ..
Some of these frameworks like CodedUI,Selenium needs active browser instance to run test suite. And this can be a challenging because of below reasons
  • The VM that runs the testsuite might take much memory intensive operations and might require more time to finish the testcase executions. 
  • Also, browser rendering adds more time to overall test execution. 

What is CasperJS?

CasperJS is testing framework build on top of Headless browsers such as PhantomJS.   

WHY USE CASPERJS

- High speed, low resources
- No need for VMs or external services
- Don't have to worry about cache/cookies


- parallel test execution

PHANTOMJS

*PhantomJS is not a test framework. It's Browser*


Headless version of Webkit
- Window-less, commandline web browser
- Programmable through Javascript
- Supports WebDriver out of the box
Get --
> Install Node.js
> npm install -g phantomjs
Use --
ex:
var page = require('webpage').create();
page.open('https://bing.com/',
function(status){
        console.log(page.content);
        phantom.exit();
}); 
What else can i do?
- inject/execute arbitary JS code
- perform actions
- take screenshots

- monitor performance

CASPER TEST API 

API & test framework layer on top of PhantomJS
Provides all the things required:
   clicking, typing, waiting, assertion, etc.,.
Events, screenshots (even specific elements) & more

-- GET
> install nodejs
> npm install -g casperjs 
-- USE
General Mode:
var casper =
require('casper').create();
        casper.start('https://jdn-int.corp.microsoft.com/',
function() {
                       this.echo(this.getTitle());
        });
...
        casper.thenOpen('https://jdn-int.corp.microsoft.com/CustomerMaster/Account',
function() {
                       this.echo(this.getTitle());
        });
casper.run();
> casperjs myscript.js 
-- USE
Test Mode:
-------------
casper.test.begin(...
casper.start('url', function{
}).run(function(){
 test.done();
})
})

> casperjs test mytest.js 

ORGANIZE TEST CODE

Check out github repo on better organizing the testsuite. 
queen-casperjs_starter_kit

Cons

Debugging experience can be hard.

Resources

Casper API Documentation: http://docs.casperjs.org/en/latest/
PhantomJS: http://phantomjs.org/
Organizing Code: https://github.com/sandeeptalabathula/queen-casperjs_starter_kit

Popular posts from this blog

Remote debugging Windows azure cloud service - Worker Role

Remote debugging Windows azure cloud service - Worker Role Very recently I was working on design and development of a worker role component of cloud service. Locally debugging worker role is pretty easy. You just need to know that you need to set Cloud project as a start-up project and ready to go. Problem is when you deploy worker role to azure and trying to troubleshoot an unknown issue.  Thankfully we have remote debugging enable for cloud services – both web and worker roles. This is really handy tool to remotely debug without having to putting a lot of tracing and digging into it. However, remote debugging in worker role/web role requires few steps to be followed: Make sure you are debugging from same machine where you published Make sure to turn on Remote debugger on while you publish (This should be turned off for Production publish profiles) Make sure to Select Debug mode With all the above settings after you publish, you should be able to Attach D

Differences between Object Serialization and Deserialization?

Serialization = putting the relevant state of the object into a streamable representation. That can mean converting it to a byte stream. This does not necessarily include copying every member variable into the stream. Deserialization = restoring an object from a serial representation and ensuring the invariants of the object. Deserialization can be thought of a separate constructor for the object.

Web-API - RESTful Services on Microsoft .net for building Ubiquitous web world

It's been very interesting to note about the fantastic things happening in the world of web development. Finally, we got the solid framework for building RESTful services on Microsoft platform. Let's have a very  quick look at the basic detail. REST [Representation State Transfer Protocol]           A representation is a opaque string of bytes that is effectively manifestation of a resource. REST was never about pretty URLs. The whole point of the hypermedia is that client should not need to know how to construct these URLs in the first place. For your clients, they are just STRINGS.           Web-API can be used when you have clients which consumes data from server over HTTP. Now a days, lot of browser applications are rich clients with web server returns some static html and then may be it uses client side framework like jquery, backbonejs or knockoutjs and makes calls back to server to pull data to execute some client-side functionality. Web-API's role is not j