Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium/WebdriverIO Chrome headless?

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
like image 286
mpen Avatar asked Feb 17 '17 16:02

mpen


3 Answers

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();
like image 55
Oliver Joseph Ash Avatar answered Oct 13 '22 04:10

Oliver Joseph Ash


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"]
    }
like image 39
Mridul Avatar answered Oct 13 '22 03:10

Mridul


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

like image 2
cvakiitho Avatar answered Oct 13 '22 04:10

cvakiitho