Headless Browser Introduction and Examples

| By Webner

Headless Browser Testing With Selenium Webdriver

Headless is a browser which does not have a GUI. A headless browser runs a testing script even though when there is no browser installed on our system such as we need the latest version of Firefox Browser to test website but the latest version is not available in the system then we can take help of the headless browser because most of the headless browsers support all browser versions. When the script is running then we do not see the browser but we will get the result of a script in the console.

Major reasons for using a headless browser for testing is performance. It helps to run tests faster in a real browser environment.

Using Headless Testing, we can generate screenshots and PDFs of websites, the content of websites, automate form submission and other tasks.

Examples of headless browser tools:

1. PhantomJS
2. Nightmare
3. Headless Chrome
4. Puppeteer
5. HtmlUnitDriver
6. Ghost

we are using the HtmlUnitDriver headless browser tool to run a testing script in the selenium web driver. HtmlUnitDriver is currently the fastest and most lightweight implementation of WebDriver.

Here’s an example on HtmlUnitDriver in Selenium WebDriver:

What we will do:
In this example we are matching the actual title of page with expected title of page using the headless browser with Firefox latest version.

1. Open the url in the headless browser.
2. Keep the expected title of the current page in a variable
3. Compare the actual title with expected title.
4. Show the result.
5. Exit the headless browser.


package Testing;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.BrowserVersion;

public class HeadlessTesting {
 public static WebDriver driver;
 public void openBrowser() throws IOException {
  //Call the headless Browser
  driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_52);

  //To hide the warning logs from the console
  Logger logger = Logger.getLogger("");

  //Open the Url

  //Expected Title of page
  String actualtitle = "Webner | Insurance, eLearning and Salesforce Software Development";

  //Actual Title of page
  String expectedtitle = driver.getTitle();
  System.out.println("Title of current page :  " + expectedtitle);

  //Compare the actual title with expected title of page	
  if (actualtitle.equals(expectedtitle)) {
   System.out.println("Page Title matches");
  } else {
   System.out.println("Page Title does not match");
 public static void main(String[] args) throws IOException {
  HeadlessTesting call = new HeadlessTesting();

Headless Browser

Leave a Reply

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