Automation, Automation Framework, Browser, Functional Testing, Headless browser, Selenium, System Testing, Testing, Web Automation, Web Testing

How to run Selenium tests in headless mode.

Quick, pace, speed are some synonyms which describes us. We , humans, wants pace in each and every act of ours. We want to speed up things as much as we can do. So we try experimenting things as in we try to search for betterment of work, as we are growing in automating things, replacing human acts to programs or machinery.

If I talk about selenium, then the major time which is spent in framework is at execution. And if I go one step more deeper then the initial point where the execution takes time is when it is trying to open browser be it Chrome, IE or any other. So just to wipe out this pain point there is one such browser which works in headless mode, and is compatible with selenium as well.

How can we use it?
Lets talk in code.
private WebDriver driver;
private String baseUrl = "";
public void setUp() throws Exception {
System.out.println("Begin Setup");
driver = new HtmlUnitDriver();

What this piece of code will do is, it will create a webdriver instance for you. And as mentioned the instance would be of HTML UNIT DRIVER.

Lets understand this line-by-line.

private WebDriver driver;

This is when you are making a driver instance.

private String baseUrl = “;

Here you are calling a web URL, which would act as the base URL for your whole class.

driver = new HtmlUnitDriver();

This is when you are mentioning the name of the browser you want to work with. And other functionality will work as is.


Main advantage is you need not to install any driver for this in your OS. It works faster than the usual browsers like Chrome, IE, FF, etc.


2 thoughts on “How to run Selenium tests in headless mode.”

    1. Hi Shailendra,
      HTML driver is not that different from any other browser in terms of usage, so if you want to click, or do some other activity then you need to follow the below code:-
      public static void main(final String[] args) {
      // Create a new instance of the html unit driver
      final WebDriver driver = new HtmlUnitDriver();

      // Add a URl you want to visit Google
      // Find the text input element by its name
      final WebElement element = driver.findElement(“q”));
      element.submit(); //use submit; //use click


Leave a Reply


You are commenting using your account. Log Out /  Change )

Google+ photo


Twitter picture


Facebook photo


Connecting to %s