Headless Browsers: HTMLUnit vs PhantomJS

A headless browser is a web browser without a graphical user interface. Headless browsers provide automated control of a web page in an environment similar to popular web browsers, but are executed via a command-line interface or using network communication.

The most popular headless browsers are HTMLUnit and PhantomJS.

HtmlUnit Drive
HtmlUnitDriver is a java based implementation of a WebBrowser without a GUI. It is the fastest and most lightweight implementation of WebDriver. It is based on HtmlUnit. For any language binding other than java the Selenium Server is required to use this driver.

PhantomJS
PhantomJS is another headless testing tool utilizing GhostDriver in the backend. It uses WebKit as its javaScript engine, and has native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. The following shows how it works:


phantomjs

Headless Browser vs Real Browser Regarding Javascript Handling

Javascript plays an essential role in modern web on web’s dynamic behavior, and the rendering behavior can differ significantly using different javascript engine. First let’s take a look at what javascript engines are real world browsers using:

Real World Browser JavaScript Engine Comment
Chrome V8
Firefox SpiderMonkey
Safari WeKit
IE(Version 9 to 11) Chakra
MS Edge Chakra

What htmlUnitDriver and PhantomJS are using:

Headless Browser JavaScript Engine Comment
HTMLUnitDriver Rhino None of the popular real world browsers use Rhino.
HtmlUnitDriver is thus only good for simple, static pages, especially those without JavaScript.
PhantomJS WebKit WebKit powers Apple’s Safari web browser, and a fork of the project is used by Chromium-based browsers, such as Google Chrome or Opera.

If the web application under test is dynamic and contains a lot of javascript, then the web behavior can differ significantly in headless browsers from that in popular browsers, since each javascript engine has its own quirks. This will make your headless test result imprecise and senseless.

In short:

  • PhantomJS is a much better choice than HtmlUnitDriver, since it is using a much popular javascript engineer Webkit.
  • Headless browsers in general are only good for simple, static pages, especially those without JavaScript, or use it for the initial testing stage in the build/continuous integration server, especially when visible browsers are not available like in a unix/linux environment.

Does Headless Browser Run Faster?
There is a rumor out there stating that headless browser runs much faster. This is not true.

The loading time for html, JavaScript, images and CSS remains the same for a headless browser as that for a real browser, it just doesn’t have to render the pixels to a screen. And the time spent on rendering pixels is almost ignorable since Browser vendors have put a lot of effort to make their browsers and JavaScript engines super fast.

Avoid Headless if You Can.

Useful reading to understand how browser works:

https://www.html5rocks.com/en/tutorials/internals/howbrowserswork/

Leave a Reply

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