Headless Browsers in Selenium

A headless browser is a browser simulation program that does not have a user interface (UI less).

Headless browser programs operate like any other browser, but do not display any UI. Selenium executes its' tests in the background.

While there are several such headless browsers available in the market, the following are the most popular ones:

  • Chrome
  • Firefox
  • HTMLUnit driver
  • SlimerJS
  • TrifleJS

What is headless testing ?

Executing the web applications' UI tests without opening a browser’s user interface is called headless browser testing. The Headless browser acts similarly to a normal web browser. Testers have full control over the web pages loaded into the headless browsers. The Only difference is you will not see a graphical user interface.

When to use headless browser testing?

We can use headless testing once the cross-browser testing is completed and want to run regression test cases in subsequent releases and with continuous integration.

You have no option other than using headless testing when your machine does not have a GUI, for instance, if you want to run your tests in Unix.

It is recommended to use the headless browser when tests are executed in parallel as User Interface based browsers consume a lot of Memory/resources. So headless browsers can be used for server-side performance testing too.

When developers push new functionality code into QA, we should be able to test the smoke tests as quickly as possible because if any show stopper is there, then we can revert the development code.

Custom Locators in Selenium

The Disadvantage of headless browser testing

  • Headless browsers are a bad idea. They get you some testing, but nothing like what a real user will see, and they mask lots of problems that only real browsers encounter.
  • Hard to debug inconsistent failures on locating elements due to page loading too fast
  • The code for non-headless drivers will not always work when the driver is switched to htmlunit.I.e., switching between ChromeDriver and FirefoxDriver is usually pretty consistent in success rate with the same code, but switching to HtmlUnit might fail the test cases
  • Headless browsers aren’t too faster than real browsers
  • Managing to take a screenshot is very difficult in a UI-less browser.

TestNG Listeners with Selenium

Headless browser automation in Selenium Java

We can automate the headless browser in selenium; only automation can be performed on the headless browser

For users, there is no such thing called as a Headless or UI less browser as their eyes cannot see the UI less browser. In this headless browser, we can execute the tests created on UI browsers, so debugging occurs on UI browsers only.

Below are the browsers we are going to automate in this selenium tutorial:

  • Chrome
  • Firefox
  • HtmlUnit browser
  • PhantomJS

Headless Chrome in Selenium

Chrome 59+ versions support headless Chrome; Chrome 59+ versions combine all the latest features available in Chromium and Blink rendering engine.

With Selenium, we can achieve a headless chrome browser by using org.openqa.selenium.chrome.ChromeOptions class. Headless Chrome mode has been available on Mac and Linux since Chrome 59; Windows support came in Chrome 60.

Steps to Create headless Chrome :
1. Set the path of the Chrome driver server path using System.setProperty

2. Create object for ChromeOptions which is present under org.openqa.selenium.chrome.ChromeOptions package

3. Call a method called setHeadless from the ChromeOptions class object, pass the parameter as true. This method makes chrome lose the UI so that the user will have a Headless chrome browser.

// set chrome as Headless

We can also use the addArguments method present in the Chrome options object to create a headless chrome browser; we need to pass --headless as a parameter to create UI less chrome.


4. Now create the Object for ChromeDriver but do not forget to pass the option to the constructor of the ChromeDriver class

Using Options classes, we can set the required features for a browser ( Firefox, Chrome). The below program tries to navigate to Google and fetches the title, URL, webelement.

Complete program for running the chrome headless.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class HeadlessChromeTesting
	public static void main(String[] args) {
		//set the driver server exe path
		System.setProperty("webdriver.chrome.driver", "C:/Users/user/Pictures/chromedriver.exe");
		ChromeOptions options = new ChromeOptions();
		// set chrome as Headless
		//Instantiate Chrome Driver
		WebDriver driver = new ChromeDriver(options);
		// get the title of the page
		System.out.println("Page title is - " + driver.getTitle());
		// get the title of the url
		System.out.println("Current Url : "+ driver.getCurrentUrl());
		// find the element
		// close the browser

The output of the Headless Chrome browser

Chrome 65 does not support Headless :

Headless chrome is not working in Chrome 65 with Chromedriver 2.29 in selenium webdriver. If we try to execute in the headless chrome browser, we will get the below error message.

This WebDriverException occurs when you want to use operations like Sendkeys and Clear a field.

Updating Chrome and chromedriver.exe will solve the below Error

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: call function result missing 'value'
(Session info: headless chrome=65.0.3325.181)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

Headless Firefox Browser in Selenium :

We can also make Firefox a headless browser in selenium Webdriver; for firefox also have to use the setHeadless method to make the Firefox headless, but this time, the setHeadless from the FirefoxOptions class.

public static void main(String[] args) throws InterruptedException {
		//set the driver server exe path
		System.setProperty("webdriver.gecko.driver", "C:/Users/user/Pictures/geckodriver.exe");
		FirefoxOptions options = new FirefoxOptions();

		//Instantiate Web Driver
		WebDriver driver = new FirefoxDriver(options);
		// get the title of the page
		System.out.println("Page title is - " + driver.getTitle());
		// get the title of the url
		System.out.println("Current Url : "+ driver.getCurrentUrl());
		// find the element
		// close the browser

The output of Headless Firefox :

How to handle browser windows using selenium webdriver

HtmlUnitDriver in Selenium :

HtmlUnitDriver is the built-in headless browser in selenium webdriver, HtmlUnitDriver is present in org.openqa.selenium.htmlunit package

Unlike Headless Firefox, Chrome, With HtmlUnitDriver, we just need to create an object for that class to create a headless browser. HTMLUnit is completely developed using java.

public static void main(String[] args) throws InterruptedException {
	// create instance for the HtmlUnitWebDriver
	HtmlUnitDriver driver = new HtmlUnitDriver();
	// get the title of the page
	System.out.println("Page title is - " + driver.getTitle());
	// get the title of the url
	System.out.println("Current Url : "+ driver.getCurrentUrl());
	// find the element
	// close the browser

The output of the HtmlUnitDriver browser

The PhantomJS Headless browser

Below was a comment by the developer of PhantomJS browser so that I will be skipping PhantomJS browser execution

I think people will switch to it (Google Chrome), eventually.
 Chrome is faster and more stable than PhantomJS.
 And it doesn’t eat memory like crazy.

I don’t see any future in developing PhantomJS.
Developing PhantomJS 2 and 2.5 as a single developer is a bloody hell.
Even with recently released 2.5 Beta version with new and shiny QtWebKit,
 I can’t physically support all 3 platforms at once
 (I even bought the Mac for that!).
 We have no support.�?

Vitaly Slobodin, the former maintainer of PhantomJS

Learn about UnknownServerException in Selenium

Fastest Headless browser

A headless browser is faster than the UI browser, but still, I wanted to test Which UI less browser is faster than others UI less browsers

Let's execute the above-created test case in headless Chrome, Headless Firefox, and HtmlUnit browser to compare the speed of execution

Even though Headless Chrome and Headless Firefox are faster, but from the above image the We can conclude that the HtmlUnit browser beats UI less chrome and UI less Firefox in terms of speed of execution.

Strings in Java & Selenium

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions