use chrome-launcher for handling the browser.

This commit is contained in:
Paul Irish 2017-06-19 16:49:21 -07:00
parent 3d3e8dd038
commit 1e56a30ff6
3 changed files with 897 additions and 88 deletions

View File

@ -1,29 +1,24 @@
/** /**
* Copyright 2017 Google Inc. All rights reserved. * Copyright 2017 Google Inc. All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
let http = require('http'); const chromeLauncher = require('chrome-launcher');
let path = require('path');
let removeRecursive = require('rimraf').sync;
let Page = require('./Page');
let childProcess = require('child_process');
let Downloader = require('../utils/ChromiumDownloader');
let Connection = require('./Connection');
let CHROME_PROFILE_PATH = path.resolve(__dirname, '..', '.dev_profile'); const Page = require('./Page');
let browserId = 0; const Downloader = require('../utils/ChromiumDownloader');
const Connection = require('./Connection');
let DEFAULT_ARGS = [ let DEFAULT_ARGS = [
'--disable-background-timer-throttling', '--disable-background-timer-throttling',
@ -32,19 +27,13 @@ let DEFAULT_ARGS = [
class Browser { class Browser {
/** /**
* @param {(!Object|undefined)} options * @param {(!Object|undefined)} options
*/ */
constructor(options) { constructor(options) {
options = options || {}; options = options || {};
++browserId;
this._userDataDir = CHROME_PROFILE_PATH + browserId;
this._remoteDebuggingPort = 9227;
if (typeof options.remoteDebuggingPort === 'number') if (typeof options.remoteDebuggingPort === 'number')
this._remoteDebuggingPort = options.remoteDebuggingPort; this._remoteDebuggingPort = options.remoteDebuggingPort;
this._chromeArguments = DEFAULT_ARGS.concat([ this._chromeArguments = DEFAULT_ARGS;
`--user-data-dir=${this._userDataDir}`,
`--remote-debugging-port=${this._remoteDebuggingPort}`,
]);
if (typeof options.headless !== 'boolean' || options.headless) { if (typeof options.headless !== 'boolean' || options.headless) {
this._chromeArguments.push(...[ this._chromeArguments.push(...[
`--headless`, `--headless`,
@ -54,8 +43,8 @@ class Browser {
if (typeof options.executablePath === 'string') { if (typeof options.executablePath === 'string') {
this._chromeExecutable = options.executablePath; this._chromeExecutable = options.executablePath;
} else { } else {
let chromiumRevision = require('../package.json').puppeteer.chromium_revision; const chromiumRevision = require('../package.json').puppeteer.chromium_revision;
let revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision); const revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
console.assert(revisionInfo, 'Chromium revision is not downloaded. Run npm install'); console.assert(revisionInfo, 'Chromium revision is not downloaded. Run npm install');
this._chromeExecutable = revisionInfo.executablePath; this._chromeExecutable = revisionInfo.executablePath;
} }
@ -66,8 +55,8 @@ class Browser {
} }
/** /**
* @return {!Promise<!Page>} * @return {!Promise<!Page>}
*/ */
async newPage() { async newPage() {
await this._ensureChromeIsRunning(); await this._ensureChromeIsRunning();
if (!this._chromeProcess || this._terminated) if (!this._chromeProcess || this._terminated)
@ -78,8 +67,8 @@ class Browser {
} }
/** /**
* @param {!Page} page * @param {!Page} page
*/ */
async closePage(page) { async closePage(page) {
if (!this._chromeProcess || this._terminated) if (!this._chromeProcess || this._terminated)
throw new Error('ERROR: this chrome instance is not running'); throw new Error('ERROR: this chrome instance is not running');
@ -87,8 +76,8 @@ class Browser {
} }
/** /**
* @return {string} * @return {string}
*/ */
async version() { async version() {
await this._ensureChromeIsRunning(); await this._ensureChromeIsRunning();
let version = await Connection.version(this._remoteDebuggingPort); let version = await Connection.version(this._remoteDebuggingPort);
@ -98,19 +87,17 @@ class Browser {
async _ensureChromeIsRunning() { async _ensureChromeIsRunning() {
if (this._chromeProcess) if (this._chromeProcess)
return; return;
this._chromeProcess = childProcess.spawn(this._chromeExecutable, this._chromeArguments, {}); this._chromeProcess = await chromeLauncher.launch({
let stderr = ''; chromeFlags: this._chromeArguments,
this._chromeProcess.stderr.on('data', data => stderr += data.toString('utf8')); chromePath: this._chromeExecutable,
port: this._remoteDebuggingPort
});
this._remoteDebuggingPort = this._chromeProcess.port;
// Cleanup as processes exit. // Cleanup as processes exit.
process.on('exit', () => this._chromeProcess.kill()); process.on('exit', () => this._chromeProcess.kill());
this._chromeProcess.on('exit', () => { // this._chromeProcess.on('exit', () => {
this._terminated = true; // this._terminated = true;
removeRecursive(this._userDataDir); // });
});
await waitForChromeResponsive(this._remoteDebuggingPort, () => !this._terminated);
if (this._terminated)
throw new Error('Failed to launch chrome! ' + stderr);
} }
close() { close() {
@ -121,30 +108,3 @@ class Browser {
} }
module.exports = Browser; module.exports = Browser;
function waitForChromeResponsive(remoteDebuggingPort, shouldWaitCallback) {
let fulfill;
let promise = new Promise(x => fulfill = x);
let options = {
method: 'GET',
host: 'localhost',
port: remoteDebuggingPort,
path: '/json/list'
};
let probeTimeout = 100;
sendRequest();
return promise;
function sendRequest() {
let req = http.request(options, res => {
fulfill();
});
req.on('error', e => {
if (shouldWaitCallback())
setTimeout(sendRequest, probeTimeout);
else
fulfill();
});
req.end();
}
}

View File

@ -16,6 +16,7 @@
"author": "The Chromium Authors", "author": "The Chromium Authors",
"license": "SEE LICENSE IN LICENSE", "license": "SEE LICENSE IN LICENSE",
"dependencies": { "dependencies": {
"chrome-launcher": "^0.1.3",
"extract-zip": "^1.6.5", "extract-zip": "^1.6.5",
"mime": "^1.3.4", "mime": "^1.3.4",
"progress": "^2.0.0", "progress": "^2.0.0",

868
yarn.lock

File diff suppressed because it is too large Load Diff