Headless testing in selenium WebDriver using HtmlUnitDriver

What is headless testing?
Headless testing is nothing but the testing which is carried out with the help of headless browsers.

What is headless browser?
We can guess it form the name, Headless – do not have a head, this means, during testing, we cannot see browser running but it will run internally.
Headless browser is a browser without GUI. So, the browser accesses the webpages internally and perform actions on them, but user cannot see that.
Some if the examples of headless drivers include – 
HtmlUnit
Ghost
PhantomJS
ZombieJS
We will talk about HtmlUnitDriverhere.
HtmlUnitDriver is a faster an most light weight version of implementation of WebDriver.
By using HtmlUnitDriver, we can select the browser (Chrome / IE/ firefox) versions. This means we can emulate HtmlUnitDriver to other browsers.
HtmlUnitDriver is platform independent.
Advantages of headless testing – 
Headless browser is faster because no GUI appear during testing.
We can use this in a situation where you have to prepare test data, or you have to pass contents from one page to another page simply.
Let us look at the example, how can we use HtmlUnitDriver for headless testing?

package samplePackage;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.htmlunit.HtmlUnitDriver;


public class HtmlUnitDriverClass {

public static void main(String[] args) {

//initialize HtmlUnitDriver wit variable driver.

WebDriver driver = new HtmlUnitDriver();

driver.navigate().to("https://learnaboutsoftwaretesting.blogspot.in");

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


}

}
Console Output: 
Title of the page is: About Software Testing . . .
When the above lines of code executes, no browser has been launched, but you can see, correct title of the page is been printed. what this mean is, browser has launched internally.

Testing different browsers on HtmlUnitDriver (headless testing)
We can test various browsers on HtmlUnitDriver for headless testing. Let us see  as below –
Select Mozilla Firefox  – 
                                HtmlUnitDriver driver = newHtmlUnitDriver(BrowserVersion.FIREFOX_38);
Select Chrome browser –
                                HtmlUnitDriver driver = newHtmlUnitDriver(BrowserVersion.CHROME);
Select IE browser – 
                                HtmlUnitDriver driver = newHtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER_11);
Hope this helps !!!!

Leave Comment

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

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories