Monday 30 June 2014

HTMLUnit Driver Notes

HtmlUnit is a headless web browser written in Java. It allows high-level manipulation of websites from other Java code, including filling and submitting forms and clicking hyperlinks 
This is currently the fastest and most lightweight implementation of WebDriver. As the name suggests, this is based on HtmlUnit

If the purpose of the tests were to validate cross browser/ cross platform behavior, then this may not be the best tool for you. Even though HtmlUnit can simulate FireFox and Internet Explorer, I do not use it for my cross browser test cases i.e. test cases that I run on say IE, FF, Safari etc.

 One other aspect of HtmlUnit to keep in mind is that the ¬®browser¬® is in memory. So, you cannot see what htmlunit is working on (unless you view the page content through say a debugger). This makes htmlunit faster for static pages.

 

Pros

  • Fastest implementation of WebDriver
  • A pure Java solution and so it is platform independent.
  • Supports Javascript

Cons

  • Emulates other browser's JS behaviour (see below)

Javascript in the HtmlUnitDriver

None of the popular browsers uses the javascript engine used by HtmlUnit (Rhino). If you test javascript using HtmlUnit the results may differ significantly from those browsers. 

 ---------------------------------------------------------------------------------------------------------------------------------------------------------
Enabling Javascript

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


-------------------------------------------------------------------------------------------------------------------------------------------------------

Emulating a Specific Browser

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);
 
---------------------------------------------------------------------------
 
HTMLUnit acts a 'headless' browser - a browser without a renderer - which lets 
you test the functionality of a website without necessarily testing the 
visual aspects. 




 ------------------------------------------------------------------------------------------------------------------------------------------------------