2017-05-11 07:06:41 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var http = require('http');
|
|
|
|
var path = require('path');
|
|
|
|
var removeRecursive = require('rimraf').sync;
|
|
|
|
var Page = require('./Page');
|
|
|
|
var childProcess = require('child_process');
|
2017-05-15 03:05:41 +00:00
|
|
|
var Downloader = require('../utils/ChromiumDownloader');
|
2017-06-14 22:45:59 +00:00
|
|
|
var Connection = require('./Connection');
|
2017-05-11 07:06:41 +00:00
|
|
|
|
|
|
|
var CHROME_PROFILE_PATH = path.resolve(__dirname, '..', '.dev_profile');
|
|
|
|
var browserId = 0;
|
|
|
|
|
|
|
|
var DEFAULT_ARGS = [
|
2017-06-21 20:51:06 +00:00
|
|
|
'--disable-background-timer-throttling',
|
|
|
|
'--no-first-run',
|
2017-05-11 07:06:41 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
class Browser {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {(!Object|undefined)} options
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
constructor(options) {
|
|
|
|
options = options || {};
|
|
|
|
++browserId;
|
|
|
|
this._userDataDir = CHROME_PROFILE_PATH + browserId;
|
|
|
|
this._remoteDebuggingPort = 9227;
|
|
|
|
if (typeof options.remoteDebuggingPort === 'number')
|
|
|
|
this._remoteDebuggingPort = options.remoteDebuggingPort;
|
|
|
|
this._chromeArguments = DEFAULT_ARGS.concat([
|
|
|
|
`--user-data-dir=${this._userDataDir}`,
|
|
|
|
`--remote-debugging-port=${this._remoteDebuggingPort}`,
|
|
|
|
]);
|
|
|
|
if (typeof options.headless !== 'boolean' || options.headless) {
|
|
|
|
this._chromeArguments.push(...[
|
|
|
|
`--headless`,
|
|
|
|
`--disable-gpu`,
|
|
|
|
]);
|
2017-05-11 07:06:41 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
if (typeof options.executablePath === 'string') {
|
|
|
|
this._chromeExecutable = options.executablePath;
|
|
|
|
} else {
|
|
|
|
var chromiumRevision = require('../package.json').puppeteer.chromium_revision;
|
|
|
|
var revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
|
|
|
|
console.assert(revisionInfo, 'Chromium revision is not downloaded. Run npm install');
|
|
|
|
this._chromeExecutable = revisionInfo.executablePath;
|
|
|
|
}
|
|
|
|
if (Array.isArray(options.args))
|
|
|
|
this._chromeArguments.push(...options.args);
|
|
|
|
this._terminated = false;
|
|
|
|
this._chromeProcess = null;
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:36:04 +00:00
|
|
|
* @return {!Promise<!Page>}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
async newPage() {
|
|
|
|
await this._ensureChromeIsRunning();
|
|
|
|
if (!this._chromeProcess || this._terminated)
|
|
|
|
throw new Error('ERROR: this chrome instance is not alive any more!');
|
|
|
|
var client = await Connection.create(this._remoteDebuggingPort);
|
|
|
|
var page = await Page.create(client);
|
|
|
|
return page;
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:36:04 +00:00
|
|
|
* @param {!Page} page
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
async closePage(page) {
|
|
|
|
if (!this._chromeProcess || this._terminated)
|
|
|
|
throw new Error('ERROR: this chrome instance is not running');
|
|
|
|
await page.close();
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:36:04 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
async version() {
|
|
|
|
await this._ensureChromeIsRunning();
|
|
|
|
var version = await Connection.version(this._remoteDebuggingPort);
|
|
|
|
return version.Browser;
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
async _ensureChromeIsRunning() {
|
|
|
|
if (this._chromeProcess)
|
|
|
|
return;
|
|
|
|
this._chromeProcess = childProcess.spawn(this._chromeExecutable, this._chromeArguments, {});
|
|
|
|
var stderr = '';
|
|
|
|
this._chromeProcess.stderr.on('data', data => stderr += data.toString('utf8'));
|
|
|
|
// Cleanup as processes exit.
|
|
|
|
process.on('exit', () => this._chromeProcess.kill());
|
|
|
|
this._chromeProcess.on('exit', () => {
|
|
|
|
this._terminated = true;
|
|
|
|
removeRecursive(this._userDataDir);
|
|
|
|
});
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
await waitForChromeResponsive(this._remoteDebuggingPort, () => !this._terminated);
|
|
|
|
if (this._terminated)
|
|
|
|
throw new Error('Failed to launch chrome! ' + stderr);
|
|
|
|
}
|
2017-06-21 14:45:13 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
close() {
|
|
|
|
if (!this._chromeProcess)
|
|
|
|
return;
|
|
|
|
this._chromeProcess.kill();
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Browser;
|
|
|
|
|
2017-06-21 02:26:16 +00:00
|
|
|
function waitForChromeResponsive(remoteDebuggingPort, shouldWaitCallback) {
|
2017-06-21 20:51:06 +00:00
|
|
|
var fulfill;
|
|
|
|
var promise = new Promise(x => fulfill = x);
|
|
|
|
var options = {
|
|
|
|
method: 'GET',
|
|
|
|
host: 'localhost',
|
|
|
|
port: remoteDebuggingPort,
|
|
|
|
path: '/json/list'
|
|
|
|
};
|
|
|
|
var probeTimeout = 100;
|
|
|
|
sendRequest();
|
|
|
|
return promise;
|
2017-05-11 07:06:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
function sendRequest() {
|
|
|
|
var req = http.request(options, res => {
|
|
|
|
fulfill();
|
|
|
|
});
|
|
|
|
req.on('error', e => {
|
|
|
|
if (shouldWaitCallback())
|
|
|
|
setTimeout(sendRequest, probeTimeout);
|
|
|
|
else
|
|
|
|
fulfill();
|
|
|
|
});
|
|
|
|
req.end();
|
|
|
|
}
|
2017-05-11 07:06:41 +00:00
|
|
|
}
|