23

Is it possible to do automated browser testing with Selenium/WebdriverIO using Chrome in headless mode?

Supposedly Chrome --headless is a thing now, but I can't get their example working. I was hoping Selenium had an option for this?


I'm initializing WebdriverIO like so:

const WebdriverIO = require('webdriverio');

let driver = WebdriverIO.remote({
    desiredCapabilities: {
        browserName: browser, // "chrome" or "firefox"
    },
});

And I'm starting Selenium using selenium-standalone:

selenium-standalone start > /dev/null 2>&1
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • `puppeteer` seems like a better choice these days. It's headless by default and runs much faster. – mpen Sep 06 '19 at 17:59

6 Answers6

27

WebdriverIO

Here is a working example with WebdriverIO: https://github.com/OliverJAsh/webdriverio-chrome-headless/blob/5f231990310023f63f9ea8581567e0d56e2d53ea/src/index.ts

The basic idea:

 import * as webdriverio from 'webdriverio';

// Headless is supported in Chrome >= 58. Not currently stable, so using dev
// build.
const CHROME_BIN_PATH = '/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome';

const options = {
    desiredCapabilities: {
        browserName: 'chrome',
        chromeOptions: {
            binary: CHROME_BIN_PATH,
            args: [
                'headless',
                // Use --disable-gpu to avoid an error from a missing Mesa
                // library, as per
                // https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
                'disable-gpu',
            ],
        },
    },
};
webdriverio
    .remote(options)
    .init()
    .url('http://www.google.com')
    .getTitle().then(title => {
        console.log({ title });
    })
    .end();

WebDriverJS

Here is a working example with WebDriverJs (the official JavaScript client to WebDriver): https://github.com/OliverJAsh/webdriverjs-chrome-headless/blob/554ea2f150e962257119703c2473753b90842087/src/index.ts

The basic idea:

import * as webdriver from 'selenium-webdriver';
import * as chromeDriver from 'selenium-webdriver/chrome';

// Headless is supported in Chrome >= 58. Not currently stable, so using dev
// build.
const CHROME_BIN_PATH = '/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome';

const options = new chromeDriver.Options();
options.setChromeBinaryPath(CHROME_BIN_PATH);
options.addArguments(
    'headless',
    // Use --disable-gpu to avoid an error from a missing Mesa library, as per
    // https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md
    'disable-gpu',
);

const driver = new webdriver.Builder()
    .forBrowser('chrome')
    .setChromeOptions(options)
    .build();
oligofren
  • 20,744
  • 16
  • 93
  • 180
Oliver Joseph Ash
  • 3,138
  • 2
  • 27
  • 47
  • @OliverJosephAsh when running the webdriverIO example nothing is output. the script seems to exit too quick IMO for it to be able to connect to anything, but no error is output. ideas on how to debug this? – oligofren Jun 19 '17 at 12:32
  • Found out that when deleting the line setting the port everything started working. Updated the example code so that others don't fall into the same trap. – oligofren Jun 19 '17 at 12:38
  • 1
    Disable gpu is no longer required. – Tally Barak Oct 28 '17 at 16:21
20

You can use capabilities in wdio.conf.js file

capabilities: [{

    maxInstances: 1,
    browserName: 'chrome',
    'goog:chromeOptions': { 
         args: ["--headless", "user-agent=...","--disable-gpu","--window-size=1440,735"]
    }
Mridul
  • 258
  • 3
  • 9
2

I did not try this myself yet, but you can download --headless build from this docker image:

https://hub.docker.com/r/justinribeiro/chrome-headless/

or build it yourself (this takes few hours, and you need a lot of RAM :) ) http://www.zackarychapple.guru/chrome/2016/08/24/chrome-headless.html

Then you should be able to just specify --headless to your chrome launch script, and use chromedriver, acording to this question in dev mailing list: https://groups.google.com/a/chromium.org/forum/#!topic/headless-dev/aAGFq8n_s6g

cvakiitho
  • 1,377
  • 13
  • 27
  • To what launch script? The `webdriverio.remote` options? Or `selenium-standalone start`? Nowhere do I start Chrome driver myself. – mpen Feb 22 '17 at 18:15
  • I believe that it has to be chrome launch script, so /usr/bin/google-chrome probably. – cvakiitho Feb 23 '17 at 09:31
2

You can add capabilities to your driver by adding the chromeOptions which sets the arguments as an array of String '--headless'.

 capabilities: [{

        maxInstances: 1,

        browserName: 'chrome',
        chromeOptions: {
            args: ['--headless'],
        },

    }],
Biswajeet gope
  • 321
  • 3
  • 17
0

Besides HTML Unit driver, another approach that helps to use webdriver in non Gui mode is to use XVirtual frame buffer for Linux. Using it you may utilize both Chrome and Firefox drivers. The whole solution, that includes Jenkins, Selenium Firefox driver and Blazemeter with using XVirtual frame buffer on Linux is described here: Headless Execution of Selenium Tests in Jenkins. Of course you may use Chrome driver instead.

-1

You can use HtmlUnitDriver() to achieve headless browser test with Selenium.

driver = new HtmlUnitDriver();
driver.get(URL); 
String title =  driver.getTitle();
System.out.println(title);

But i understand you want specific headless browser test with chrome, .....let me try and get back to you.

Dharam
  • 134
  • 8