5 Best Headless Browsers for Testing

A headless browser is like a normal web browser, but without GUI (Graphical User Interface). It is still packaged with common components such as networking component, JavaScript interpreter, and rendering engines. 

The main purpose of using headless browser is to test websites. You can use headless browser to make bots as well.

What are the best headless browsers?

Most popular ones are based on popular web browser engines such as Webkit and Gecko.

PhantomJS – PhantomJS is a headless WebKit scriptable with a JavaScript API. It has full DOM and CSS parsing, JSON, canvas, and SVG support. It can access and manipulate webpages with the standard DOM API, or with other Javascript libraries like jQuery.

var page = require('webpage').create();
var url = 'http://www.tldevtech.com/';
page.open(url, function (status) {
  phantom.exit();
});

CasperJS – This browser is developed based on PhantomJS. CasperJS allows you to build full navigation scenarios and to run full featured tests on any websites.

var casper = require('casper').create();
casper.start('http://tldevtech.com/');

casper.then(function() {
    this.echo(this.getTitle());
});

casper.run();

ZombieJS – Zombie.js is a small framework for testing client-side JavaScript code. The browser is built on node.js, making it very easy to integrate with your project. And it can work with any testing frameworks such as Mocha.

const Browser = require('zombie');
Browser.localhost('tldevtech.com', 3000);

Headless Chromium – Headless Chromium allows running Chromium in a headless/server environment.

const CDP = require('chrome-remote-interface');

CDP((client) => {
  // Extract used DevTools domains.
  const {Page, Runtime} = client;

  // Enable events on domains we are interested in.
  Promise.all([
    Page.enable()
  ]).then(() => {
    return Page.navigate({url: 'http://www.tldevtech.com'});
  });

  // Evaluate outerHTML after page has loaded.
  Page.loadEventFired(() => {
    Runtime.evaluate({expression: 'document.body.outerHTML'}).then((result) => {
      console.log(result.result.value);
      client.close();
    });
  });
}).on('error', (err) => {
  console.error('Cannot connect to browser:', err);
});

Selenium – It helps automating web browsers for testing purposes across multiple platforms. Selenium supports JavaScript, Java, C#, Haskell, Objective-C, Python, Ruby, Perl, PHP, and R.