Headless Browser Testing in Selenium Web Driver


A headless browser is a browser simulation program that does not have a user interface (UI less).
Headless browser programs operate like any other browser, but do not display any UI. Selenium executes its' tests in the background.

There are several headless browsers available in the market, the following are the most popular ones:
  • Chrome
  • Firefox
  • HTMLUnit driver
  • PhantomJS


What is headless testing ?
Executing the web applications' UI tests without opening a browser’s user interface is called headless browser testing. Headless browser acts similar to a normal web browser.

Testers have full control over the web pages loaded into the headless browsers. Only difference is you will not see a graphical user interface.

When to use headless browser testing ?
We can use headless testing once the cross browser testing is completed and want to run regression test cases in subsequent releases and with continuous integration.

You have no option other than using headless testing when your machine does not have a GUI, for instance if you want to run your tests in unix.

It is recommended to use headless browser when tests are executed in parallel as User Interface based browsers consumes a lot of Memory / resources.

Disadvantage of headless browser testing
  • Headless browsers are a bad idea. They get you some testing, but nothing like what a real user will see, and they mask lots of problems that only real browsers encounter.
  • Hard to debug inconsistent failures on locating elements due to page loading too fast.
  • In Real browser as functions are performing in front of user and he can interact with it so he can easily detect where the tests goes fail. And can easily debug if anything goes wrong.
  • Headless browsers aren’t representing real users, as no user uses the your application without UI. 
  • Because it does not have UI, it may not report errors related with images.
  • Managing to take screenshot is very difficult in UI less browser.
Headless browser automation in Selenium Java
We can automate the headless browser in selenium, only automation can be performed on headless browser.
For users, there is no such thing called Headless or UI less browser as their eyes cannot see the UI less browser.
In this headless browser we can execute the tests created on UI browsers, so debugging occurs on UI browsers only.
Below are the browsers we are going to automate :

1. Chrome
2. Firefox
3. HtmlUnit browser

Headless Chrome in Selenium

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class HeadLessChrome {
public static void main(String[] args) {

//set the driver server exe path
System.setProperty("webdriver.chrome.driver", "C:/Drivers/chromedriver_win32/chromedriver.exe");

// set chrome as Headless
ChromeOptions options = new ChromeOptions();
options.setHeadless(true); //options.addArguments("--headless");

WebDriver driver = new ChromeDriver(options); //Instantiate Chrome Driver

driver.get("http://demo.nopcommerce.com/");

System.out.println("Title of the page:" + driver.getTitle());
System.out.println("URL of the page:" + driver.getCurrentUrl());
}
}

Headless Firefox  in Selenium

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class HeadLessFirefox {
public static void main(String[] args) {
//set the driver server exe path
System.setProperty("webdriver.gecko.driver", "C://Drivers/geckodriver-v0.23.0-win64/geckodriver.exe");

// set Firefox as Headless
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);

WebDriver driver=new FirefoxDriver(options); //Instantiate Firefox Driver

driver.get("http://demo.nopcommerce.com/");

System.out.println("Title of the page:" + driver.getTitle());
System.out.println("URL of the page:" + driver.getCurrentUrl());
}
}

HtmlUnitDriver in Selenium

HtmlUnitDriver is the built-in headless browser in selenium webdriver, HtmlUnitDriver is present in org.openqa.selenium.htmlunit package.
Unlike Headless Firefox, Chrome, With HtmlUnitDriver we just need to create a object for the class to create a headless browser.
HTMLUnit is completely developed using java.

You need to download drivers and add them to project build path.

Download Link: https://github.com/SeleniumHQ/htmlunit-driver/releases


import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class HeadLessHtmlUnitDriver {

public static void main(String[] args) {

// create instance for the HtmlUnitWebDriver
HtmlUnitDriver driver = new HtmlUnitDriver();

driver.get("http://demo.nopcommerce.com/");

System.out.println("Title of the page:" + driver.getTitle());
System.out.println("URL of the page:" + driver.getCurrentUrl());
driver.quit();
}

}

Followers