Spread the love

Headless browser

A headless browser is a web browser without a graphical user interface.

Headless browsers provide automated control of a web page in an environment similar to popular web browsers, but are executed via a command-line interface or using network communication. They are particularly useful for testing web pages as they are able to render and understand HTML the same way a browser would, including styling elements such as page layout, colour, font selection and execution of JavaScript and AJAX which are usually not available when using other testing methods.

A headless browser is a web-browser without a graphical user interface. This program will behave just like a browser but will not show any GUI.

Headless browser Testing using Selenium HtmlUnitDriver

// Declaring and initialize  HtmlUnitWebDriver
WebDriver driver = new HtmlUnitDriver();
// open facebook webpage


// Print the title

System.out.println(“Title of the page “+ driver.getTitle());

Benefits of Html Unit Driver:

  • Since it is not using any GUI to test, your tests will run in background without any visual interruption
  • Compared to all other instances execution is faster
  • To run your tests through HtmlUnit driver you can also select other browser versions
  • It is platform independent and easier to run several tests concurrently.

Why do we use headless drivers? How can you visually investigate test failure when using headless drivers?

Headless drivers are typically used in continuous integration (CI) setups. Headless drivers, such as PhantomJS, provide all standard web browser functionalities, but run in the command-line. These drivers are based on command-line tools and don’t produce screen output, making them ideal for completely automated setups.

To be able to visually investigate test failures, the developer needs to implement mechanisms to capture screenshots, otherwise rely on command line output.

A headless browser is a web-browser without a graphical user interface. This program will behave just like a browser but will not show any GUI.

Some of the examples of Headless Drivers include

  • HtmlUnit
  • Ghost
  • PhantomJS
  • ZombieJS
  • Watir-webdriver


PhantomJS is a headless browser with JavaScript API. It is an optimal solution for Headless Website Testing, access and manipulate webpages & comes with the standard DOM API.

In order to use PhantomJS with Seleniun, one has to use GhostDriver.

Ghost Driver is an implementation of the Remote WebDriver Wire protocol, using PhantomJS as back-end.

Basic setup for Headless browser Testing in Selenium Webdriver:

You will need to things:

  1. Phantomjs
  2. PhantomJS driver

PhantomJS can be downloaded from here : http://phantomjs.org/download.html

You will get one zip file from above link, download and extract it in your c:/program files

phantomjs-headless testing by erostrum.com

headless testing by erostrum.com

PhantomJS driver  can be downloaded from here :http://mvnrepository.com/artifact/com.github.detro.ghostdriver/phantomjsdriver

PhantomJSDriver is a Java binding for the PhantomJS WebDriver, GhostDriver. The binding is developed within the GhostDriver project, and distributed through public Maven repository and Selenium official .zip package.

You will get PhantomJS driver  jar file. Add that .jar file to your project build path.

package com.myinterviewpackage;

import java.io.File;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

public class HeadlessTestingDemo {

// WebDriver is an Interface.

public static void main(String[] args) {

System.out.println("Test started");
File file = new File("C:\\Program Files\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());

WebDriver driver=new PhantomJSDriver();

System.out.println("The title of window is : " + driver.getTitle());


Windows pop up may appear :

When you run above code , you might get windows pop up once. You just need to click on “Allow Access ” button.



When to use PhantomJS

Headless Testing
Screen Capture
Page Automation
Network Monitoring
To render dashboard screenshots for their users
To run Unit tests on command line
To generate employee handbooks from HTML to PDF
Combined with QUnit for the test suite.


To test application rapidly in various browsers and without any visual interruption, headless browser Testing is used. Due to its speed, accuracy and easy to access features, HTML unit driver and PhantomJS are gaining popularity for headless browser testing. By following some simple steps you get to know how easily these tools can be integrated with other tools and can execute the test code.


Leave a Reply

Your email address will not be published. Required fields are marked *