How to execute test in Headless browser with Selenium WebDriver?

We have already seen that Selenium WebDriver supports test execution in different web browsers such as Google Chrome, Mozilla Firefox, Internet Explorer, Safari, Opera, etc. There is a separate driver file available to execute tests in the respective browser and we first set the property of that browser with the help of the driver. Also, we can easily see the steps of test execution or test run in such a browser as these browsers are physically stored in our device. Today we will talk about something which is available but not visible and that bizarre thing in Selenium is- Headless browser. It is like Rahu and Ketu of India Astrology, who has separated head and the body.

Don’t Miss: Technique to verify tooltip text in Selenium

What is Headless browser in Selenium?

A headless browser is a physically non-existing web browser available for test automation with Selenium WebDriver. It does not have any Graphical User Interface (GUI), however, it executes the program based on the DOM structure. Hence, if any tests are executed with the headless browser so action performed as test steps can’t be seen.

Some of the famous headless browsers are listed as follows:

  • HtmlUnit
  • PhantomJS
  • NodeJS
  • ZombieJS
  • Waitr-WebDriver

Significances of using the headless browser in Selenium

There are some of the uses of the headless browser which ultimately improves the efficiency of the test execution. Those points are listed below:

  • It is used to run tests at the central server where there is no browser installed
  • Headless browser testing helps to create a spider or crawler program
  • It helps to simulate test execution on different browsers of the same machine
  • Headless test execution is faster

HtmlUnitDriver vs GhostDriver

  • HtmlUnitDriver and GhostDriver is the driver for HtmlUnit and PhantomJS headless browser respectively.
  • WebKit is the rendering engine for PhantomJS, whereas, Rhino engine is the rendering engine for HtmlDriver. These two techniques are for implementing JavaScript in its own way.
  • HtmlUnit helps to modify HTTP request, whereas, PhantomJS is stronger in working with JavaScript.
  • HtmlUnitDriver is faster and best fit for simple applications, whereas, PhantomJS is slower but can work with bigger applications.

Headless browser testing Selenium

Working with HtmlUnitDriver for Headless browser testing

HtmlUnitDriver is like any other instance of the WebDriver. It is fastest and lightweight headless browser in Selenium WebDriver.  Some of the features of HtmlUnitDriver includes:

  • It supports all the Selenium UI operations like click, sendkeys, etc.
  • This headless driver handles HTTP and HTTPS requests.
  • HtmlUnitDriver supports cookie and proxy handling.
  • It has terrific support for JavaScript, GET and POST data processing and Exception handling as well.

How to implement HtmlUnitDriver in Selenium?

The implementation of HtmlUnitDriver is same as the implementation of ChromeDriver or FirefoxDriver or any other driver. But, the slight difference is we don’t need to use system property set up. Let’s implement the HtmlUnitDriver by taking a sample program in which we just print the title.

How to enable JavaScript support with HtmlUnitDriver?

HtmlUnitDriver does not implement JavaScript by default so we need to enable it by calling the setJavascriptEnabled() method. There is an alternative way too. We can enable JavaScript even by passing true as an argument in HtmlUnitDriver.

Some of the tutorials on JavaScript are as follows:

Here are the two techniques:

Technique# 1:

Technique# 2:

How to use different version of the browser in Headless browser testing?

At the same time on the same machine, we can test our automation code in the different version of the browser heedlessly. The version could be Chrome, Firefox, etc. Let’s have a look at its implementation here.

The sample code is as follows:

This was all about headless browser testing in Selenium by using HtmlUnitDriver. You can raise your queries by using the comment form below and don’t miss to join our Facebook group for quick updates.

Join Inviul fb group

Leave a Reply