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:

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

 

 

 

 

 

Print Friendly, PDF & Email
How to capture the screen shot in selenium
Verify toolTip using Selenium Webdriver
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

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