Selenium Headless Browser Testing

A headless browser is a web-browser without a graphical user interface. This program is behave like a browser but does not show any visual browser interface or GUI.

Examples of Headless Drivers: 

  • HtmlUnit
  • Ghost
  • PhantomJS
  • Watir-webdriver

HTMLUnit Driver : HTMLUnit is a Headless browser which is written in java. So HTMLUnit Driver is a class in Selenium. It is same as Chrome, IE or any other usual driver, but it does not have GUI. So we cannot see the test execution on screen when we use HTMLUnit Driver . That’s why It is known as Headless Browser Driver too.

Advantages of HtmlUnit Driver:

  • It is a pure Java based solution using the JS engine
  • Since it is not using any GUI to test,  tests will run in background without any visual interruption and execution is faster
  • It Support for  HTTP and HTTPS protocols, cookies and HTML responses
  • There is support for proxy server available
  • HtmlUnit driver you can also select other browser versions 
  • It is platform independent and easier to run several tests concurrently.  Hence, most suitable for Load testing.

Limitations:

  • JavaScript browser behavior is not suppoted
  • Whenever a test fails or is interrupted, it becomes tough to debug since it is difficult to see it physically on the browser
  • It is perfect for small project like Smoke test or quick test but not applicable for regression in real life.  

Ways to Use HTMLUnit Driver with Selenium

Importing JAR and Class Libraries:

  • The HtmlUnitDriver is only a small wrapper around HtmlUnit itself. So you need all the dependencies of the selenium-htmlunit-driver also. To do that  you can download the whole package or JAR file from following link https://github.com/SeleniumHQ/htmlunit-driver/releases
  • Make sure you download the 1st JAR (htmlunit-driver-versionNumber-jar-with-dependencies.jar) file and import to your project.
  • Then import appropriate packages as below: 
  1. import org.openqa.selenium.Htmlunit.HtmlUnitDriver;
  2. import com.gargoylesoftware.Htmlunit.BrowserVersion;

 

Declarations

WebDriver driver = new HtmlUnitDriver();

Emulating a specific browser

WebDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME);

OR

WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_24);

HtmlUnit Driver automation Video

Share the Knowledge

You May Also Like

About the Author: codenbox

Leave a Reply

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