Delivered by FeedBurner

What Is Headless/htmlunitdriver? How To Execute WebDriver Test In It?

What Is HtmlUnit Driver or Headless browser driver?
HtmlUnit Driver or mostly It Is known as Headless browser driver Is based on HtmlUnit. HtmlUnit Driver Is same as Firefox or Chrome or IE driver Instance but HtmlUnit driver do not have GUI so you can not see your test execution on your screen. It Is something like virtual browser Instance. Generally we are using Firefox driver or chrome driver or IE driver Instance to run our
WebDriver automation tests. But from all these driver Instances, Headless driver Instance Is most lightweight and fastest version to execute test using selenium.

Also you can learn how to execute webdriver test in FIREFOX, CHROME and IE browsers.

Advantages of using HtmlUnit Driver
There are few advantages as bellow of using HtmlUnit driver.
  • It Is not using any GUI to test so your tests will run In background without any visual Interruption.
  • Execution Is faster compared to all other driver Instances.
  • It Is platform Independent as It Is pure Java solution. It Is using Rhino javascript engine.
  • You can also select other browser versions to run your tests through HtmlUnit driver.
WebDriver test Example using HtmlUnit Driver
I have created simple Google search example which will print page title, search on google and then print all google search result strings In console. 

When you run bellow test, You will notice that any browser will not open, test will be executed virtually and you will see results directly In your eclipse execution console.

package Testing_Pack;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class htmlDriver {
 HtmlUnitDriver driver;
 String pagetitle;

 public void setup() throws Exception {
  //Initializing HtmlUnitDriver.
  driver = new HtmlUnitDriver();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  //Opening URL In HtmlUnitDriver.

 public void tearDown() throws Exception {
  //Closing HtmlUnitDriver.

 public void googleSearch() {
  //Get and print page title before search.
  pagetitle = driver.getTitle();
  System.out.println("Page title before search : "+pagetitle);
  //Search with Hello World on google.
  WebElement Searchbox = driver.findElement(By.xpath("//input[@name='q']"));
  Searchbox.sendKeys("Hello World");
  //Get and print page title after search.
  pagetitle = driver.getTitle();
  System.out.println("Page title after search : "+pagetitle);
  //Get list of search result strings.
  List<WebElement> allSearchResults=driver.findElements(By.cssSelector("ol li h3>a"));

  //Iterate the above list to get all the search titles & links from that page.
  for(WebElement eachResult : allSearchResults) {
  System.out.println("Title : "+eachResult.getText()+", Link : "+eachResult.getAttribute("href"));

When execution completed, You will see result In console as bellow. There will be some warning messages like "com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error" In console. We will see how to remove those warnings In my next post.

1 comment: