2017-08-15 01:08:06 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
const path = require('path');
|
|
|
|
const removeRecursive = require('rimraf').sync;
|
|
|
|
const childProcess = require('child_process');
|
|
|
|
const Downloader = require('../utils/ChromiumDownloader');
|
|
|
|
const Connection = require('./Connection');
|
|
|
|
const Browser = require('./Browser');
|
|
|
|
const readline = require('readline');
|
|
|
|
|
|
|
|
const CHROME_PROFILE_PATH = path.resolve(__dirname, '..', '.dev_profile');
|
|
|
|
let browserId = 0;
|
|
|
|
|
|
|
|
const DEFAULT_ARGS = [
|
|
|
|
'--disable-background-networking',
|
|
|
|
'--disable-background-timer-throttling',
|
|
|
|
'--disable-client-side-phishing-detection',
|
|
|
|
'--disable-default-apps',
|
|
|
|
'--disable-hang-monitor',
|
|
|
|
'--disable-popup-blocking',
|
|
|
|
'--disable-prompt-on-repost',
|
|
|
|
'--disable-sync',
|
|
|
|
'--enable-automation',
|
|
|
|
'--metrics-recording-only',
|
|
|
|
'--no-first-run',
|
|
|
|
'--password-store=basic',
|
|
|
|
'--remote-debugging-port=0',
|
|
|
|
'--safebrowsing-disable-auto-update',
|
|
|
|
'--use-mock-keychain',
|
|
|
|
];
|
|
|
|
|
|
|
|
class Launcher {
|
|
|
|
/**
|
|
|
|
* @param {!Object} options
|
|
|
|
* @return {!Promise<!Browser>}
|
|
|
|
*/
|
|
|
|
static async launch(options) {
|
|
|
|
options = options || {};
|
|
|
|
++browserId;
|
|
|
|
let userDataDir = CHROME_PROFILE_PATH + browserId;
|
|
|
|
let chromeArguments = DEFAULT_ARGS.concat([
|
|
|
|
`--user-data-dir=${userDataDir}`,
|
|
|
|
]);
|
|
|
|
if (typeof options.headless !== 'boolean' || options.headless) {
|
|
|
|
chromeArguments.push(
|
|
|
|
`--headless`,
|
|
|
|
`--disable-gpu`,
|
|
|
|
`--hide-scrollbars`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let chromeExecutable = options.executablePath;
|
|
|
|
if (typeof chromeExecutable !== 'string') {
|
|
|
|
let chromiumRevision = require('../package.json').puppeteer.chromium_revision;
|
|
|
|
let revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
|
|
|
|
console.assert(revisionInfo, 'Chromium revision is not downloaded. Run npm install');
|
|
|
|
chromeExecutable = revisionInfo.executablePath;
|
|
|
|
}
|
|
|
|
if (Array.isArray(options.args))
|
|
|
|
chromeArguments.push(...options.args);
|
|
|
|
let chromeProcess = childProcess.spawn(chromeExecutable, chromeArguments, {});
|
|
|
|
if (options.dumpio) {
|
|
|
|
chromeProcess.stdout.pipe(process.stdout);
|
|
|
|
chromeProcess.stderr.pipe(process.stderr);
|
|
|
|
}
|
|
|
|
let stderr = '';
|
|
|
|
chromeProcess.stderr.on('data', data => stderr += data.toString('utf8'));
|
|
|
|
// Cleanup as processes exit.
|
|
|
|
const onProcessExit = () => chromeProcess.kill();
|
|
|
|
process.on('exit', onProcessExit);
|
|
|
|
let terminated = false;
|
|
|
|
chromeProcess.on('exit', () => {
|
|
|
|
terminated = true;
|
|
|
|
process.removeListener('exit', onProcessExit);
|
|
|
|
removeRecursive(userDataDir);
|
|
|
|
});
|
|
|
|
|
2017-08-16 08:10:55 +00:00
|
|
|
let browserWSEndpoint = await waitForWSEndpoint(chromeProcess);
|
2017-08-15 01:08:06 +00:00
|
|
|
if (terminated)
|
|
|
|
throw new Error('Failed to launch chrome! ' + stderr);
|
|
|
|
// Failed to connect to browser.
|
2017-08-16 08:10:55 +00:00
|
|
|
if (!browserWSEndpoint) {
|
2017-08-15 01:08:06 +00:00
|
|
|
chromeProcess.kill();
|
|
|
|
throw new Error('Failed to connect to chrome!');
|
|
|
|
}
|
|
|
|
|
|
|
|
let connectionDelay = options.slowMo || 0;
|
2017-08-16 08:10:55 +00:00
|
|
|
let connection = await Connection.create(browserWSEndpoint, connectionDelay);
|
2017-08-15 01:08:06 +00:00
|
|
|
return new Browser(connection, !!options.ignoreHTTPSErrors, () => chromeProcess.kill());
|
|
|
|
}
|
2017-08-15 21:29:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} options
|
|
|
|
* @return {!Promise<!Browser>}
|
|
|
|
*/
|
2017-08-16 08:10:55 +00:00
|
|
|
static async connect({browserWSEndpoint, ignoreHTTPSErrors = false}) {
|
|
|
|
let connection = await Connection.create(browserWSEndpoint);
|
2017-08-15 21:29:42 +00:00
|
|
|
return new Browser(connection, !!ignoreHTTPSErrors);
|
|
|
|
}
|
2017-08-15 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!ChildProcess} chromeProcess
|
2017-08-15 21:29:42 +00:00
|
|
|
* @return {!Promise<string>}
|
2017-08-15 01:08:06 +00:00
|
|
|
*/
|
2017-08-16 08:10:55 +00:00
|
|
|
function waitForWSEndpoint(chromeProcess) {
|
2017-08-15 01:08:06 +00:00
|
|
|
return new Promise(fulfill => {
|
|
|
|
const rl = readline.createInterface({ input: chromeProcess.stderr });
|
|
|
|
rl.on('line', onLine);
|
2017-08-15 21:29:42 +00:00
|
|
|
rl.once('close', () => fulfill(''));
|
2017-08-15 01:08:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} line
|
|
|
|
*/
|
|
|
|
function onLine(line) {
|
2017-08-15 21:29:42 +00:00
|
|
|
const match = line.match(/^DevTools listening on (ws:\/\/.*)$/);
|
2017-08-15 01:08:06 +00:00
|
|
|
if (!match)
|
|
|
|
return;
|
|
|
|
rl.removeListener('line', onLine);
|
2017-08-15 21:29:42 +00:00
|
|
|
fulfill(match[1]);
|
2017-08-15 01:08:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Launcher;
|