Reputation: 282895
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
Upvotes: 23
Views: 24530
Reputation: 266
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"]
}
Upvotes: 20
Reputation: 331
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'],
},
}],
Upvotes: 2
Reputation: 2741
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();
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();
Upvotes: 27
Reputation: 1396
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
Upvotes: 2
Reputation: 64
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.
Upvotes: 0
Reputation: 134
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.
Upvotes: -1