Headless Browsers in Selenium Webdriver

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
  • PhantomJS
  • SlimerJS
  • TrifleJS

What is headless testing ?

Executing the web applications' UI tests without opening a browser’s user interface is called headless browser testing. Headless browser acts similar to a normal web browser.

Testers have full control over the web pages loaded into the headless browsers. 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 headless browser when tests are executed in parallel as User Interface based browsers consumes a lot of Memory / resources. So headless browsers can be used for server side performance testing too.

When developers pushes new functionality code in to QA, we should be able to test the smoke tests as quick as possible because if any Show stopper is there, then we can revert the development code.

Create Custom By class Locator in Selenium

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.

You're infinitely better off using a "headed" browser (i.e., anything but HTMLUnit) on a headless environment (e.g., Windows, or Linux with XVFB).

Hard to debug inconsistent failures on locating elements due to page loading too fast

In Real browser as functions are performing in front of user and he can interact with it so he can easily detect where the tests goes fail. And can easily debug if anything goes wrong.

Unintended interactions (losing the benefit of automated UI testing vs integration or unit testing) due to speed/headless state of browser

Code for non-headless drivers will not always work when driver is switched to htmlunit. I.e. switching between ChromeDriver and FirefoxDriver is usually pretty consistent in success rate with same code, but switching to HtmlUnit might fail the testcases

Headless browsers aren’t representing real users, as no user uses the your application without UI. Because it doesnot have UI, it may not report errors related with images.

Headless browsers aren’t too faster than real browsers, because most of the time that it takes to load a page is depends on the actual network speed, html, JavaScript, images and CSS, it just doesn’t have to color your screen.

Managing to take screenshot is very difficult in UIless browser

Take Page & Element Screenshot, Compare Screenshots in selenium

Headless browser automation in Selenium Java

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

For users, there is no such thing called 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:
1. Chrome
2. Firefox
3. HtmlUnit browser
4. PhantomJS

Default Constructor in selenium and Java

Headless Chrome in Selenium

Headless Chrome is supported by Chrome 59+ versions, Chrome 59+ versions combines the all the latest features available in Chromium and Blink rendering engine.

With Selenium webdriver we can achieve 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 the chrome to lose the UI, so the user will have Headless chrome browser.

								// set chrome as Headless

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


4. Now create the Object for ChromeDriver but donot 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).

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

Output of Headless chrome browser headless-chrome-success-chromeoptions

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 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 the Firefox as headless browser in selenium Webdriver, For firefox also we have use setHeadless method to make the Firefox as 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

Output of Headless Firefox : firefox-headless-browser

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 a object for tha 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

Output of HtmlUnitDriver browser htmlunitdriver-selenium-headless

PhantomJS Headless browser

Below was comment by the developer of PhantomJS browser, so 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

Headless browser are 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 testcase in headless Chrome, Headless Firefox and HtmlUnit browser to compare the speed of execution

Even thought Headless Chrome and Headless Firefox are faster, but from above image the We can conclude that HtmlUnit browser beats UI less chrome and UI less Firefox interms of Speed of execution.

Handle dropdowns in selenium

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions