Jul 12, 2017 | Zabil Maliackal

Testing with headless chrome

Testing with headless chrome

The new headless feature in Chrome 59, makes it easy to run gauge with selenium on a CI/CD setup or Docker instances without having to use xfvb.


For Maven add the following dependencies to pom.xml.


For Gradle add dependencies to build.gradle

dependencies {
   compile 'org.seleniumhq.selenium:selenium-java:3.4.0'
    compile 'io.github.bonigarcia:webdrivermanager:1.7.1'

webdrivermanager is a library that automatically downloads and manages chromedriver.

Initialize WebDriver with arguments for headless mode and run tests.

public class DriverFactory {
   public static WebDriver getDriver() {

        ChromeOptions options = new ChromeOptions();

        return new ChromeDriver(options);


If you are using bundler add this to your Gemfile

source 'https://rubygems.org'

group :test do
    gem 'selenium-webdriver'
    gem 'test-unit'
    gem 'chromedriver-helper'

And initialize a driver

require 'selenium-webdriver'

module Driver
    def driver
        options = Selenium::WebDriver::Chrome::Options.new


        Selenium::WebDriver.for :chrome, options: options    



Gauge sets all of this via project templates!

For a maven selenium project run

$ gauge init java_maven_selenium
$ cd <projectdirectory>
$ headless=Y gauge run specs

Or for setting up a ruby project

$ gauge init ruby_selenium
$ headless=Y bundle exec gauge run specs

Screenshot on failure

By default, Gauge captures the screen display. In headless mode, the browser is not launched into a display, use getScreenShotAs method in WebDriver to capture the browser’s screenshot instead.

For Gauge, implement a custom screenshot handler.

CustomScreenGrabber implements ICustomScreenshotGrabber {
    public byte[] takeScreenshot() {
        // DriverFactory is a custom class to manage WebDriver instances
        // and may vary between projects.
        WebDriver driver = DriverFactory.getDriver();
        return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);

Have fun setting up tests in headless mode!

Gauge is a free and open source test automation framework that takes the pain out of acceptance testing. Download it or read documentation to get started!