Headless WebDriver Testing using HtmlUnitDriver

In this tutorial, we will see what headless testing is and how to implement it using HTMLUnitDriver.


Headless Browser

A headless browser is one which doesn’t have a head(duh!) or in other words a GUI. It doesn’t open up any browser or interrupt any activities on the running system.

This is preferred if you are running the tests in a system where you don’t have any browsers installed or on a headless server. Some other scenarios I can think of is for personal uses: like if you want to open some web pages and extract data from it, or in the case of bloggers – if you want to automatically share a blog post to social networking sites after you have published the post.

Since we can simulate any browser using HTMLUnitDriver, this can also be used for testing across various browsers on a server which doesn’t have any/all of them.

There are a few ways of implementing headless mode for Selenium WebDriver

  • HtmlUnitDriver

HtmlUnitDriver is the fastest and most lightweight implementation of WebDriver. It is based on HtmlUnit which is a java based implementation of a browser without a GUI.

  • GhostDriver

PhantomJS is a headless browser (similar to HtmlUnit above) and GhostDriver is an implementation of the WebDriver Wire Protocol for PhantomJS.

In this tutorial, we focus on HTMLUnitDriver. If you are interested in GhostDriver, check out this article by AssertSelenium.


As mentioned above, it’s the fastest and most lightweight implementation of WebDriver.

But one of its major cons is its Javascript implementation. From the WebDriver Github page.

None of the popular browsers uses the javascript engine used by HtmlUnit (Rhino). If you test javascript using HtmlUnit the results may differ significantly from those browsers.

Because of this, you cannot be sure that the javascript part of your application works fine in all browsers just because the test passed headlessly. Generally, you can use it for the basic sanity tests.

SEE ALSO:   How to Handle Alerts and Frames in WebDriver

Now, let’s go into the fun part.

The best way to create a headless webdriver script is to create the script using any browser implementation – FirefoxDriver or ChromeDriver etc. This way you can make sure that the script works fine.

Once that is done, you can make the change in initialization part to convert it to HtmlUnitDriver. Please note that if you application behaves differently for GUI or headless mode, you have to make the appropriate changes.

For running webdriver in headless mode, we have to initialize as shown below.

Even though the driver object is not of WebDriver interface, all its operations are available.

We can also emulate a specific browser as below.

You have a list of browser options available


We can also set DesiredCapabilities as shown below.

Further Reading:

You may also like...

2 Responses

  1. Sandeep Singh says:

    Is this code relevant for Selenium WebDriver 3.0 as well?

Leave a Reply

Your email address will not be published. Required fields are marked *