top of page

Launch HTMLUnitDriver & PhantomJS for Headless Browser Testing in Selenium

What is a headless browser in Selenium?

A headless browser is a term used to define browser simulation programs that do not have a GUI. These programs execute like any other browser but do not display any UI. In headless browsers, when Selenium tests run, they execute in the background. Almost all modern browsers provide the capabilities to run them in a headless mode.

  • Selenium is a powerful tool for headless browser testing, allowing developers to run automated tests without the need for a visible user interface. By running tests in the background, Selenium can save time and resources while also helping identify issues that may not be apparent in a traditional UI-based testing environment. This includes performance-related issues and layout problems that may only become evident in a headless setting.

Why use a headless browser for test execution?

Running a Selenium test case in headless mode provides the following benefits:

  • Useful in CI pipeline: When we need to execute automated test cases remotely on a server or in any of the build and release pipelines for continuous integration servers like Jenkins, it is not always possible to install real browsers on such remote machines. We can use headless browsers to run automation tests efficiently.

  • Beneficial in web scraping: When you want to write a web scraper or data extractor that needs to visit some websites and collect data, headless browsers are a perfect choice. Because we are not concerned about functionality in these cases, we visit web pages and get the data.

  • Support for multiple browser versions: Sometimes, the tester would like to simulate multiple browser versions on the same machine. In that case, you would want to use a headless browser because most of them support the simulation of different versions of browsers.

  • Faster automation test execution: The performance with a headless browser is better compared to real browser automation. The real browsers like Google Chrome, Firefox, and Internet Explorer take a significant amount of time to load CSS, JavaScript, Images, and open and render HTML. Headless browsers do not require all this to load and will start performing functions without waiting for a page to load completely. When we need to run the regression scripts, in headless browsers, we could save time as there are much faster and can render results quickly.

  • Multi-Tasking: Headless browsers can help you, multi-task.You can use your browser or your machine to do anything else while the tests run in the background. Save hours that we otherwise spend staring at the screen.

Limitations of headless browser testing

  • Debugging will not be feasible, as the only way to check what's running on the browser is to grab the screenshots and validate the output.

  • Headless browsers don't mimic the exact user behavior, as the page doesn't render precisely with all the dependencies that it will render in an actual browser.

  • Cosmetic bugs like the location of a web element, the color of a web element may get missed while running the tests in headless mode.

Types of Headless Driver’s:

  • HtmlUnit

  • PhantomJS

  • Ghost

  • ZombieJS


  • HTML UnitDriver is the lightest weight and fastest implementation browser for WebDriver, based on HtmlUnit, it is known as a headless browser driver, it is similar to a Chrome, IE, or FireFox driver.

  • Does not have a GUI, so the screen can not see test execution on screen.


Features of HTML Unit Driver:

  • Speed and performance improvements.

  • Allows to test browserless setup.

  • Support for HTTPS and HTTP protocols.

  • Helps you in multitask.

  • Help for cookies.

  • Excellent JavaScript Support.

Steps to Use HTMLUnit Driver with Selenium

Step 1) In Eclipse, copy the following code. Add the standard selenium library files to the project. No additional jar files are required.

package  htmldriver;
import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;	
import org.openqa.selenium.htmlunit.HtmlUnitDriver;		
public class htmlUnitYest {				
		public static void main(String[] args) {
                     // Creating a new instance of the HTML unit driver
                     WebDriver driver = new HtmlUnitDriver();
           		     // Navigate to Google		
					 // Locate the searchbox using its name		
                     WebElement element = driver.findElement("q"));	
                    // Enter a search query		
		            // Submit the query. Webdriver searches for the form using the text input element automatically		
                    // No need to locate/find the submit button		
            		// This code will print the page title		
                    System.out.println("Page title is: " + driver.getTitle());		

Step 2) Run the code. You will observer no browser is launched and results are shown in console.


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. GhostDriver is a implementation of Webdriver Wire protocol in simple JS for PhantomJS.

The latest release of PhatomJS has integrated GhostDriver and there is no need to separately install it.

Features of PhantomJS:

  • Able to Headless Testing.

  • Screen capture Capability.

  • Able to Network monitoring.

  • To run a unit test on the command line.

  • To prepare an employee manual for PDF from HTML.

  • Development is very easy with PhantomJS, when you develop UI, you can write code by selecting the HTML element.

  • Testers need to do manual testing, which seems easy with the PhantomJS.

Here is how the system works-

Steps to run Selenium with PhatomJS

Step 1) You need Eclipse with Selenium installed

Step 2) Download PhantomJS here

Step 3) Extract the downloaded folder to Program Files

Step 4) Download the PhantomJS Driver from here. Add the jar to your project

Step 5) Paste the following code in eclipse

Step 6) Run the code. You will observe the output is shown in console and no browser is launched.

Reference upon:-

Thanks for Reading.....!!!

49 views1 comment

Recent Posts

See All

1 Comment

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Feb 09
Rated 4 out of 5 stars.


bottom of page