Execution of Selenium Test in headless mode

Monil Joshi
5 min readFeb 6, 2022

--

In previous blogs, we have seen some of the concepts related to the selenium web driver for UI automation. You can read it here Introduction to Selenium Web Driver.

In this article, we will see what it is to be executed a user interface test using Selenium Web Driver in headless mode.

When you start your UI test execution on the targeted browser first thing test does is launch the targeted browser on which test launches Application under test and continue for the rest of the test. But what if execution happens in the background without launching any browser upfront? You can work on another task peacefully without any disturbances from browser instances that get launched for every test.

So what is exactly without a browser and is it possible to execute it especially in the case of UI automation.

Yes. It is possible. In this case, we will have to execute our test automation in headless mode. Headless mode is executing tests on targeted browsers normally without any GUI. This execution will happen as any other background process.

Selenium supports execution in headless mode. All the supported browsers are having respective capabilities to execute tests in headless mode.

First of all, what is needed of executing the test in headless mode

  1. Multi-tasking: If your test cases are executing and at the same time you are contributing to automation development using the same machine each test will launch a new browser window will pop up on your screen which is enough to distract you from your automation task. So in case, you have 150–200 test executions randomly appearing window pop-ups will obviously make you not so comfortable to develop automation. In this case, if you execute the test in the headless mode you can focus on other tasks and test case execution will happen in the background.
  2. Reduces execution Time: Test execution takes lesser time in headless mode than normal execution with the launching of a browser as here page is not fully get rendered as normal execution fetch the html and CSS details after launching the actual browser.
  3. Reduce CPU usage and overall resource efficiency: Whenever we launch a browser, there is a sudden increase in CPU usage. When we execute the same test in headless mode CPU usage seems to be comparatively less.
  4. Helpful in CI/CD execution: Due to all the above reasons this is the preferred way to execute tests in CI/CD executions as mostly these executions are nightly executions and no one is not looking actually monitor complete execution.
    Another reason is, Companies dedicate a single machine to execute CI/CD pipeline which will be always up and running. If we execute headless mode using HTMLUnit driver we do not need to install any real browsers on that machine.
  5. Cross-platform Testing- Headless execution allows cross platform testing. These test cases can be executed on same browser on different operating systems (eg. Windows, Linux, macOS) without any extra setup. For example headless execution on Chrome on windows will be work same as headless execution on Chrome on Mac machine.
  6. Enhanced Security: It provides extra layer of security for execution as there is no visible GUI for browser window that can potentially expose sensitive information such as login creds, payment information.

We will see what are the different ways to execute headless execution.

  1. HtmlUnitDriver
  2. Ghost
  3. Watir-webdriver
  4. ZombieJS
  5. PhantomJS

Out of the above, we will see headless execution with HTMLUnit driver and how it will work with Real browser in this article.

HTMLUnitDriver: HTMLUnit is a Java based implementation of a web browser without GUI. You can check its git repository. Below are the advantages of using HTMLUnitDriver for headless execution

  1. It is considered to be the fastest and lightweight implementation of WebDriver.
  2. It makes execution faster compared to another browser as no installation is needed on the execution machine.
  3. Headless using HTMLUnitDriver was the first choice for execution using CI/CD due to point number 2.
  4. You can execute test on selected browser version using HTMLUnitDriver

Let's see how can we use HTMLUnitDriver with Selenium:

If you are using an older selenium version i.e 2.53 or below then no need to add any jar or dependency as it has built-in headless support for HTMLUnitDriver. As I am working on Gradle project I will be using the below Selenium dependency.

implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.53.1'

Copy below code

// Creating an instance of the HTMLUnitDriver
WebDriver driver = new HtmlUnitDriver();

// Navigate to Google
driver.get("http://www.google.com");

// Locate the element SearchBox using locator strategy name with value q
WebElement element = driver.findElement(By.name("q"));

// Enter a search query
element.sendKeys("Selenium");
System.out.println("Test Executed");

If you execute this Script you will not see the launching of the browser and further operation still your test will get executed. I am printing a message at the end for confirmation.

By default, HTMLUnitDriver will not support JavaScript code. If you are using any Javascript operation using JavaScriptExecutor in your script it will fail. For that, you have to enable javascript for driver instance by passing boolean value while creating it.

WebDriver driver = new HtmlUnitDriver(true);

You can also mention the browser Version if you want to execute your test on a specific browser version for example if you want to execute your test on Firefox version 38 you can add that as an argument.

WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38,
true);

But considering people will use the recent version of Selenium i.e. 3.159 I am getting an error to work with HTMLUnitDriver. So I will be using a real browser to execute Selenium test in headless mode.

I will show how can we execute Selenium test on headless mode on Chrome, and FireFox

Here I am using WebDriver manger to auto-download browser driver. You can read this article to understand it better Introduction to Drivers & Web Driver Manager in Selenium Web Driver.

To execute your test in headless mode using Chrome. We have to make use of a class called ChromeOptions and pass argument as headless. Then pass object of this ChromeOption class to the driver instance like below

WebDriver driver;WebDriverManager.chromedriver().setup();ChromeOptions chromeOptions = new ChromeOptions();chromeOptions.addArguments("--headless");driver = new ChromeDriver(chromeOptions);driver.get("http://www.google.com");WebElement element = driver.findElement(By.name("q"));element.sendKeys("Selenium");

If you are using Selenium 3.x there is dedicated method you can use us setHeadless() from ChromeOptions class and pass boolean parameter true.

WebDriver driver;WebDriverManager.chromedriver().setup();ChromeOptions chromeOptions = new ChromeOptions();chromeOptions.setHeadless(true);driver = new ChromeDriver(chromeOptions);driver.get("http://www.google.com");WebElement element = driver.findElement(By.name("q"));element.sendKeys("Selenium");

We can perform same operation using Firefox browser

WebDriver driver;WebDriverManager.firefoxdriver().setup();FirefoxOptions firefoxOptions = new FirefoxOptions();firefoxOptions.setHeadless(true);driver = new FirefoxDriver(firefoxOptions);driver.get("http://www.google.com");WebElement element = driver.findElement(By.name("q"));element.sendKeys("Selenium");

There are certain limitations for executing test on headless mode.

  1. It will not replicate exact user behaviour as in headless mode web page does not render completely with all UI dependencies which will be rendering while loading web page in real browser.
  2. In case failure we will have to write extra loggers or code to get logs or screenshots of failure. This will make framework volatile and debugging makes difficult. In this case execution of test cases in real browser with headed mode will be helpful as we can watch execution of test case and find the problem

This is about execution test in headless mode. Set your goal before deciding whether to execute on headless or headed mode.

Happy learning..!!

--

--

Monil Joshi

I am having an 8+ years of experience in software testing. Working with Web, Mobile and API technologies. Visit my GIT https://github.com/monilj