In this arena of multiple types of browsers, here comes a specialized browser – a headless browser, which is very much a web browser, but without a GUI. It can access web pages but is not seen by your naked eye. Its major functionality is to give the content of web pages to external programs and it very well understands web pages as similar as a browser, but of course, with a distinct difference that any two browsers would generally have.
Some of the well known headless browser drivers are HtmlUnit, Ghost, PhantomJS, ZombieJS etc.
HtmlUnitDriver – An Apt Tool for Headless Browser Testing
HtmlUnitDriver, based on HtmlUnit, is considered one of the most apt headless browser drivers. It is very similar to Chrome, Firefox and Internet Explorer (IE) but going by the concept of a headless browser, it does not have a graphical user interface (GUI) and hence its test execution cannot be seen on the screen. It acts similar to a virtual driver instance which holds the capability of accessing the web pages with ease and simplicity but is not able to show it before the end user. Considered to be most lightweight and fast, it is considered one of the best options to execute browser testing using Selenium.
Some of the salient features of an HtmlUnitDriver are as follows:
- It supports HTTPS and HTTP protocols, cookies and HTML responses
- There is support for proxy server available
- Availability of good JavaScript support
- Support for Get and Post
- Capability to undergo customization of the request headers being sent to the server
Key Benefits of using HtmlUnitDriver for Browser Testing
- Minimal visual interruption because of lack of graphical display
- Swifter execution as compared to real browsers because the simulated browser is a mere native code library hence faster to launch and doesn’t require much time in communication as well as drawing and operating things on your screen.
- Platform independent and supports concurrent test runs. Hence, most suitable for Load testing.
- Is a pure Java based solution using the JS engine
- The simulated browser is filled with key features depicting a rich client-side behaviour with quicker execution, as compared to other driver instances
- Is able to deal with HTTPS security, as well
- Possesses more dependability than a real browser. Simulated browsers are highly independent and hence its operation is quite smooth and dependable
- Provision to choose other browser versions to testing using HtmlUnitDriver
Challenges faced while using HtmlUnitDriver for headless browser testing
- Whenever a test fails or is interrupted, it becomes tough to debug since it is difficult to see it physically on the browser of your machine. Even if there are chances of debugging, the time frame to do the same is quite high
- Simulation of other browsers is possible, e.g. Firefox, IE etc. but there is no surety to which extent these real world browsers would be replicated
- JavaScript is disabled, by default and would have to be enabled when the script is being written
- For other browsers, emulation of JS behavior is difficult
Ways to use HtmlUnitDriver using Selenium
- Importing Class Libraries
import org.openqa.selenium.Htmlunit.HtmlUnitDriver
import com.gargoylesoftware.Htmlunit.BrowserVersion;
- Declarations
HtmlUnitDriver unitDriver= new HtmlUnitDriver();
- Enabling JS
HtmlUnitDriver unitDriver= new HtmlUnitDriver();
unitDriver.setJavascriptEnabled(true);
OR
HtmlUnitDriver unitDriver= new HtmlUnitDriver(true);
- Emulating a specific browser
HtmlUnitDriver unitDriver= new HtmlUnitDriver(BrowserVersion.CHROME)
OR
HtmlUnitDriver unitDriver= new HtmlUnitDriver(BrowserVersion.FIREFOX_24);
Software Testing Solution, your testing partner, offers a range of QA and Manual/automated software/browser testing services encompassing the entire software release lifecycle. With a stringent focus on quality and processes, we have been implementing several testing projects in a multitude of technologies.
Request a Free POC to test-drive our services.