Headless browsers are browsers that do not render the page on an interface. A header browser would fetch the content of the page and then download images, stylesheets, scripts, and so on, and process them just like a real browser.
The benefits of using a headless browser is that it is much faster. This is because the browser doesn't have a Graphical User Interface (GUI), and thus there's no need to wait for the display to actually render the output:
- PhantomJS (http://phantomjs.org/) uses the WebKit web browser engine, which is the same one that is used by Safari. It is arguably the most popular headless browser today. However, activity on its repository has almost come to a halt since the middle of 2016.
- SlimerJS (https://slimerjs.org/) uses the Gecko web browser engine, and SpiderMonkey as the JavaScript engine, which is the same as Firefox. SlimerJS is not a headless browser by default, as it uses the X11 display server on the test machine. However, you can integrate it with Xvfb (short for X virtual framebuffer), which is an in-memory display server that does not require a display. Since Firefox 56, you can also enable headless mode with the --headless flag.
- ZombieJS (http://zombie.js.org/) is a faster implementation of a headless browser because it does not use an actual web browser engine like PhantomJS or SlimerJS. Instead, it uses JSDOM, which is a pure-JavaScript implementation of the DOM and HTML. However, also because of this, the results may not be 100% accurate or as realistic as testing against an actual web browser engine.
- HtmlUnit (http://htmlunit.sourceforge.net/) is a "GUI-less browser for Java programs". It uses the Rhino JavaScript engine, which, like Selenium, is written in Java. From experience, HtmlUnit is the quickest headless browser but also the most error-prone. It's ideal for simple static pages that do not involve heavy JavaScript usage.
However, purely-headless browsers might be a thing of the past soon, as many "real" browsers now support Headless Mode. The following browsers have Headless Mode support:
- Chrome 59
- Firefox 55 (on Linux) and 56 (on macOS and Windows)
For those that don't, we can use Xvfb to substitute for the X11 display server, and run the real browser on a CI server. However, this will lose the performance benefit of running a headless browser.