Headless Testing | HtmlUnitDriver

How to perform Headless Testing using HtmlUnitDriver in Selenium?

Basically, headless testing is, running a script in the background without any visual of the browser. In simple words, this process doesn’t need any browser. This method can be used on the machines which don’t have any browser installed on them.

This technique has been used for simple websites to get data or input from them without interfering to the user in the quick and easy way, as a user doesn’t have to wait for launching the browser or loading the URL etc.
Steps to Performed:
  1. Create an object of HtmlUnitDriver.
  2. Launch above mentioned URL using the driver.
  3. Getting Window Title after launching the URL.
  4. Getting Post Content (Website Content)
Selenium CODE:
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.annotations.Test;

public class HtmlUnitExample
@Test(description="Headless Testing using HtmlUnitDriver")
public void getPostContent() throws InterruptedException, IOException
// To turn off all the HTMLUnit warnings from console output.
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

System.out.println("Headless Testing using HtmlUnitDriver Execution Start");

WebDriver driver=new HtmlUnitDriver();                            // Step #1
driver.get("https://seleniumqna.blogspot.in/");                   // Step #2
System.out.println("Window Title : "+driver.getTitle());          // Step #3
By locator_post=By.xpath("//*[contains(text(),'Question:')]/../..");

List<WebElement> postList=driver.findElements(locator_post);
for (WebElement postData : postList)
System.out.println(postData.getText());                            // Step #4



1. We can’t use this technique for complex websites. 
2. HtmlUnitDriver doesn’t support screenshot functionality. The PhantomJS Driver supports screenshots functionality as well. Will share the POST for the same very soon.
3. Debugging of the script is also too complex as compared to non-headless technique.

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 10
  • Page views today : 10
  • Total visitors : 50,683
  • Total page view: 69,804

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.