Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for headless Chrome/ChromeCanary #111

Merged
merged 2 commits into from
May 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,34 @@ ChromeBrowser.prototype = {

ChromeBrowser.$inject = ['baseBrowserDecorator', 'args']

function headlessGetOptions (url, args, parent) {
return parent.call(this, url, args).concat(['--headless', '--disable-gpu', '--remote-debugging-port=9222'])
}

var ChromeHeadlessBrowser = function (baseBrowserDecorator, args) {
ChromeBrowser.apply(this, arguments)

var parentOptions = this._getOptions
this._getOptions = function (url) {
return headlessGetOptions.call(this, url, args, parentOptions)
}
}

ChromeHeadlessBrowser.prototype = {
name: 'ChromeHeadless',

DEFAULT_CMD: {
// Try chromium-browser before chromium to avoid conflict with the legacy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is out of date, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yes it is (although it is also out-of-date further up in the file where I copied this block from)

// chromium-bsu package previously known as 'chromium' in Debian and Ubuntu.
linux: getBin(['google-chrome', 'google-chrome-stable']),
darwin: getChromeDarwin('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'),
win32: getChromeExe('Chrome')
},
ENV_CMD: 'CHROME_BIN'
}

ChromeHeadlessBrowser.$inject = ['baseBrowserDecorator', 'args']

function canaryGetOptions (url, args, parent) {
// disable crankshaft optimizations, as it causes lot of memory leaks (as of Chrome 23.0)
var flags = args.flags || []
Expand Down Expand Up @@ -199,6 +227,28 @@ ChromeCanaryBrowser.prototype = {

ChromeCanaryBrowser.$inject = ['baseBrowserDecorator', 'args']

var ChromeCanaryHeadlessBrowser = function (baseBrowserDecorator, args) {
ChromeCanaryBrowser.apply(this, arguments)

var parentOptions = this._getOptions
this._getOptions = function (url) {
return headlessGetOptions.call(this, url, args, parentOptions)
}
}

ChromeCanaryHeadlessBrowser.prototype = {
name: 'ChromeCanaryHeadless',

DEFAULT_CMD: {
linux: getBin(['google-chrome-canary', 'google-chrome-unstable']),
darwin: getChromeDarwin('/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'),
win32: getChromeExe('Chrome SxS')
},
ENV_CMD: 'CHROME_CANARY_BIN'
}

ChromeCanaryHeadlessBrowser.$inject = ['baseBrowserDecorator', 'args']

ChromiumBrowser.prototype = {
name: 'Chromium',

Expand Down Expand Up @@ -237,13 +287,16 @@ DartiumBrowser.$inject = ['baseBrowserDecorator', 'args']
// PUBLISH DI MODULE
module.exports = {
'launcher:Chrome': ['type', ChromeBrowser],
'launcher:ChromeHeadless': ['type', ChromeHeadlessBrowser],
'launcher:ChromeCanary': ['type', ChromeCanaryBrowser],
'launcher:ChromeCanaryHeadless': ['type', ChromeCanaryHeadlessBrowser],
'launcher:Chromium': ['type', ChromiumBrowser],
'launcher:Dartium': ['type', DartiumBrowser]
}

module.exports.test = {
isJSFlags: isJSFlags,
sanitizeJSFlags: sanitizeJSFlags,
headlessGetOptions: headlessGetOptions,
canaryGetOptions: canaryGetOptions
}
17 changes: 17 additions & 0 deletions test/jsflags.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,20 @@ describe('canaryGetOptions', function () {
])
})
})

describe('headlessGetOptions', function () {
var headlessGetOptions = launcher.test.headlessGetOptions

it('should return the headless flags', function () {
var parent = sinon.stub().returns(['-incognito'])
var context = {}
var url = 'http://localhost:9876'
var args = {}
expect(headlessGetOptions.call(context, url, args, parent)).to.be.eql([
'-incognito',
'--headless',
'--disable-gpu',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curiosity: What are the pros of using incognito and disable-gpu?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--disable-gpu is currently needed with headless because of linking errors with osmesa.so. That will hopefully not be required once Chrome switches to their SwiftShader library as a software rendering engine. See https://bugs.chromium.org/p/chromium/issues/detail?id=617551 for details.

-incognito is just an example flag in the test, ensuring that it gets passed through.

'--remote-debugging-port=9222'
])
})
})