Using HtmlUnitDriver for executing Selenium Automation scripts on a headless browser

What is Headless Browser ?

Headless browser is a web browser without Graphical User Interface.

What are the different browsers which have Graphical User Interface ?

Firefox, Chrome, Internet Explorer, Safari and Opera browser have Graphical User Interface.

In Selenium, while executing the automation scripts on any of the above browser, we can visually see what actually is happening inside the browser, as these browsers have Graphical User Interface.

Unlike the above browser, headless browsers wont have any Graphical User Interface.

What is HtmlUnitDriver ?

HtmlUnitDriver is one of the Selenium Drivers, which will enable to run our automation scripts on a headless browser. While the automation scripts are running on the headless browser, we cannot see what is happening inside the headless browser, as they dont have Graphical User Interface and hence are not completely visible to our eyes.

In order to run the automation scripts on Chrome browser, we need to use chromedriver.exe.
In order to run the automation scripts on Internet Explorer browser, we need to use iedriverserver.exe
In order to run Selenium 3 scripts on Firefox browser, we need to use geckodriver.exe

Similarly, in order to run the automation scripts on headless browser having no GUI, we can use any of these below drivers for Selenium:

  • HtmlUnitDriver
  • Ghost Driver
  • PhantomJS Driver
  • ZombieJS Driver
  • Watir-WebDriver

Hence HtmlUnitDriver is one of the drivers designed for running Selenium scripts on a headless browser having no GUI.

Practically executing the Selenium code using HtmlUnitDriver's invisible browser:

1. Create a new Java Project in Eclipse IDE as shown below:

2. Create 'libraries' folder inside the project as shown below:

3. Download Selenium 2.53.1 Jar files from here as shown below:

4. Extract the downloaded Zip file and copy all the JAR files from the extracted folder into the 'libraries' folder in Eclipse IDE as shown below:

5. Browse and download HtmlUnitDriver as shown below:

6. In the resultant page, click on the 'html unit driver jar file' of latest version as shown below and download:

7. Copy the downloaded jar file and paste it into the 'libraries' folder as shown below:

8. Configure the Jar files from the Project Properties > Libraries tab > Add Jars > Select jars under 'libraries' folder of project > Click 'OK' button followed by 'Apply' button as shown below:

9. Observe that all the Jar files in the 'libraries' folder got configured as shown below:

10. Create a new Java class in the Project as shown below:

11. Create a driver object for HtmlUnitDriver class as shown below:

12. Hover the mouse over the above errors and resolve them as shown below:

13. Observe that errors got resolved and write the remaining selenium code as shown below:

14. Execute the above Selenium code and observe that code will be executed in an invisible browser and hence wont be visible as executed. As a proof that it got executed successfully, no exceptions got displayed in the below Eclipse IDE console after execution of code:

15. Hence the invisible browser has performed the below steps, but nothing was visible:

  • Invisible browser got launched
  • Navigated to
  • Clicked on Selenium143 link
  • Invisible browser got closed

16. In order to confirm that, the Selenium code got executed in an invisible way, I will write down the below print statement at the end of the Selenium code as shown below:

17. Execute the above code and observe that 'End of the code' message will get displayed in Eclipse IDE console after Selenium code execution, by proving that the Selenium code got executed in an invisible way on invisible browser.


Hence HtmlUnitDriver is used in Selenium Automation for executing the automation scripts on an headless browser in an invisible way.

No comments: