Launch HTMLUnitDriver & PhantomJS for Headless Browser Testing in Selenium:

 

What is a headless browser in Selenium?

  • The headless browser is a web browser, which has no graphical user interface.
  • Headless browsers offer automatic control over a web page in an environment like a popular web browser, but are executed through command line interfaces or network communications.

 

Types of Headless Driver’s:

  • HtmlUnit
  • PhantomJS
  • Ghost
  • ZombieJS

 

1. HTMLUnitDriver:

  • HTML UnitDriver is the lightest weight and fastest implementation browser for WebDriver, based on HtmlUnit, it is known as a headless browser driver, it is similar to a Chrome, IE, or FireFox driver.
  • Does not have a GUI, so the screen can not see test execution on screen.

 

Features of HTML Unit Driver:

  • Speed and performance improvements.
  • Allows to test browserless setup.
  • Support for HTTPS and HTTP protocols.
  • Helps you in multitask.
  • Help for cookies.
  • Excellent JavaScript Support.

 

Initialize HtmlUnitDriver:

 

Example of Launching HtmlUnitDriver:

Output: 

 

2. PhantomJS:

  • PhantomJS is a headless browser with Javascript APIs Headless Website is an optimal solution for manipulation, access and webpages and comes with standard DOM API.
  • PhantomJS gives you the advantage of capturing screenshots so that you can override the user-agent string header from the related server and copy the next browser.
  • The latest release of PhantomJS has integrated GhostDriver and there is no need to install it separately.
  • GhostDriver is a pure JavaScript implementation, which is the WebDrive Wire Protocol for PhantomJS.

 

Features of PhantomJS:

  • Able to Headless Testing.
  • Screen capture Capability.
  • Able to Network monitoring.
  • To run a unit test on the command line.
  • To prepare an employee manual for PDF from HTML.
  • Development is very easy with PhantomJS, when you develop UI, you can write code by selecting the HTML element.
  • Testers need to do manual testing, which seems easy with the PhantomJS.

 

Download PhantomJS:

 

Example of Launching PhantomJS: