Selenium is a de-facto testing framework for web applications
...RECENT BLOG

2020 / 2 / 27

2020 / 2 / 27

2020 / 2 / 27

2020 / 2 / 27

2020 / 2 / 27
I must say when the first time I heard about Headless Browser, I was completely clueless about how a browser can be called Headless. Why there is a testing framework when we have such a good testing framework Selenium. But, let me say don’t compare Selenium and Headless Browser. Both of them are completely different. Both have different purposes.
A headless browser is a browser without a graphical user interface (GUI). They are executed via command line or via network communication. The main advantage they have over normal browsers is that they provide automated control of the web pages without using much CPU resources like memory, RAM.
On the other hand, Selenium is a portable testing framework for testing web applications. It also provides a record/ playback tool for authoring tests without the need to learn a test scripting language.
CasperJS is a Headless Browser Testing Framework. Till now in the last blog post, we have discussed what Headless Browser Testing is, what are the advantages and what are the limitations of this testing. Now, we will discuss a specific Headless Browser testing framework.
What is CasperJS?
CasperJS is a Javascript framework built on top of PhantomJS(Webkit) and SlimerJS(Gecko). Now, you must be wondering what is WebKit and what is Gecko? Let me first explain a WebKit. Webkit is an open-source web browser engine rendering HTML, XHTML, CSS, XML images in Safari, Chrome and Opera. Gecko is a web browser engine for Firefox.
CasperJS is testing framework for page status, functional navigation, scraping data off the web page, screen capture.
CasperJS also allows assertions, which are an easier way to track failed tests.
We will start discussing a few functions that are most commonly used for testing.
CasperJS Modules
The most important feature of CasperJS is its tester module, where it offers a handful of functions that can testing processes using CasperJS.
We will be discussing the testing code to test the HTTP Status Code of the page after deployment.
Let’s first have a look at the list of some modules, then we will discuss the code for HTTP Status Code.
Module | Description |
---|---|
Casper | It is the easiest way to create a Casper instance and pass Casper options. |
Tester | This module provides the unit and functional test assertions. |
Colorizer | This module is used to generate ANSI color output on the screen |
Mouse | This module is responsible for providing mouse operations like clicking, double-clicking, rollover and many more. |
Utils | This module provides simpler helper functions. |
Clientutils | This module is used to inject client-side utilities into the DOM Environment. |
These are just a few modules out of so many modules that are available. It is highly recommended that you go through the API Documentation before proceeding for the coding part.
The CasperJS Documentation is a very good document listing all the functions with examples.
Install CasperJS:
You can install CasperJS using npm:
Install PhantomJS:
Now, it’s time to write a simple code to print the HTTP Status Code of a web page.
Code:
var baseUrl='https://www.frugaltesting.com/';
var links=["/features/features","/services","/pricing","/ourteam",];
var casper = require('casper').create();
var i=0;
var nTimes=links.length;
casper.start().repeat(nTimes,function(){
casper.thenOpen(baseUrl+links[i],function(){
this.echo(this.currentHTTPStatus);
this.echo(this.getTitle());
i++;
});
});
casper.run();
Let’s discuss the code in a bit detail. Here I am querying a web page www.frugaltesting.com. What I am doing is printing the title of 4 different pages of the main website and simultaneously printing the HTTP Status Code of the pages.
If you see the output below, you will notice that Page Title and HTTP Status Code 200 is printed. HTTP Status Code 200 means action requested by the client was received, understood and accepted.
With this, we are done with the basic code for CasperJS. Go ahead and feel free to experiment with all the Modules available in CasperJS and try all the functions.
2020 / 2 / 27
2020 / 2 / 27
2020 / 2 / 27
2020 / 2 / 27
2020 / 2 / 27
Can Selenium be used for Testing ?
Impact Of Heavy User Load On Your Applications Performance