Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
HTMLUnit Driver - Testingpool

HTMLUnit Driver

HTMLUnit Driver is currently the fastest and most light weight implementation of WebDriver. It is based on HTMLUnit (This is a Java framework for testing webapps.) It is also known as Headless Browser Driver.

HtmlUnit is a java based implementation of a WebBrowser without a GUI. It has good implementation of DOM(Document object Model) and has support of using javascript, but it is no different from any other browser.It is same as Chrome, IE, or FireFox driver, but it does not have GUI so one cannot see the test execution on screen. Test execution will run behind the scene.

Enabling Javascrip:

It can be done by setting the method javaScriptEnabled() to true.

HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);

OR

HtmlUnitDriver driver = new HtmlUnitDriver(true);

Emulating a Specific Browser:

There are two more constructor of HTMLUnit Driver which takes specific browser. One can take direct browser version and other one can take by using capabilities.

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);

and

HtmlUnitDriver driver = new HtmlUnitDriver(capabilities);

Features of HTMLUnit Driver:

  • Support for the HTTP and HTTPS protocols
  • Support for Cookies
  • Support for submit methods POST and GET (as well as HEAD, DELETE, …)
  • Proxy server support
  • Excellent JavaScript support
  • Ability to customize the request headers being sent to the server
  • Support for HTML responses like submitting forms,walking the DOM model of the HTML document,clicking links,provides easy access to all information contained inside HTML pages.
  • Support for basic and NTLM authentication
  • Ability to specify whether failing responses from the server should throw exceptions or should be returned as pages of the appropriate type (based on content type)

Example for using HTMLUnit Driver:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class HTMLBrowserEx {

	public static void main(String[] args) {
		
		
		WebDriver driver = new HtmlUnitDriver();
		driver.get("http://gmail.com");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		String title = driver.getTitle();
		
		System.out.println("The browser title : "+title);
		driver.close();
	}
}

Output:

The browser title : Gmail

Pros of HTMLUnit Driver:

  • Since it GUI less execution, so it is fastest implementation of Webdriver
  • It is a pure java solution , that’s why it is platform independent
  • Supports javascript
  • You can specify the desired browser with a specific version

Cons of HTMLUnit Driver:

  • It cannot emulate other browsers JavaScript behavior.

Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

 

 

 

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...