feat(Launcher): introduce appMode option

The patch introduces a new appMode option to the launcher. This is an experimental
option, thus we don't document it.
This commit is contained in:
Pavel Feldman 2017-09-29 15:16:32 -07:00 committed by Andrey Lushnikov
parent 1c292e9253
commit a6cf8237b8
3 changed files with 25 additions and 15 deletions

View File

@ -21,12 +21,13 @@ const EventEmitter = require('events');
class Browser extends EventEmitter { class Browser extends EventEmitter {
/** /**
* @param {!Connection} connection * @param {!Connection} connection
* @param {boolean} ignoreHTTPSErrors * @param {!Object=} options
* @param {function():Promise=} closeCallback * @param {function():Promise=} closeCallback
*/ */
constructor(connection, ignoreHTTPSErrors, closeCallback) { constructor(connection, options = {}, closeCallback) {
super(); super();
this._ignoreHTTPSErrors = ignoreHTTPSErrors; this._ignoreHTTPSErrors = !!options.ignoreHTTPSErrors;
this._appMode = !!options.appMode;
this._screenshotTaskQueue = new TaskQueue(); this._screenshotTaskQueue = new TaskQueue();
this._connection = connection; this._connection = connection;
this._closeCallback = closeCallback || new Function(); this._closeCallback = closeCallback || new Function();
@ -45,7 +46,7 @@ class Browser extends EventEmitter {
async newPage() { async newPage() {
const {targetId} = await this._connection.send('Target.createTarget', {url: 'about:blank'}); const {targetId} = await this._connection.send('Target.createTarget', {url: 'about:blank'});
const client = await this._connection.createSession(targetId); const client = await this._connection.createSession(targetId);
return await Page.create(client, this._ignoreHTTPSErrors, this._screenshotTaskQueue); return await Page.create(client, this._ignoreHTTPSErrors, this._appMode, this._screenshotTaskQueue);
} }
/** /**

View File

@ -40,13 +40,15 @@ const DEFAULT_ARGS = [
'--disable-prompt-on-repost', '--disable-prompt-on-repost',
'--disable-sync', '--disable-sync',
'--disable-translate', '--disable-translate',
'--enable-automation',
'--enable-devtools-experiments',
'--metrics-recording-only', '--metrics-recording-only',
'--no-first-run', '--no-first-run',
'--password-store=basic',
'--remote-debugging-port=0', '--remote-debugging-port=0',
'--safebrowsing-disable-auto-update', '--safebrowsing-disable-auto-update',
];
const AUTOMATION_ARGS = [
'--enable-automation',
'--password-store=basic',
'--use-mock-keychain', '--use-mock-keychain',
]; ];
@ -56,9 +58,14 @@ class Launcher {
* @return {!Promise<!Browser>} * @return {!Promise<!Browser>}
*/ */
static async launch(options) { static async launch(options) {
options = options || {}; options = Object.assign({}, options || {});
let temporaryUserDataDir = null; let temporaryUserDataDir = null;
const chromeArguments = [].concat(DEFAULT_ARGS); const chromeArguments = [].concat(DEFAULT_ARGS);
if (options.appMode)
options.headless = false;
else
chromeArguments.push(...AUTOMATION_ARGS);
if (!options.args || !options.args.some(arg => arg.startsWith('--user-data-dir'))) { if (!options.args || !options.args.some(arg => arg.startsWith('--user-data-dir'))) {
if (!options.userDataDir) if (!options.userDataDir)
temporaryUserDataDir = fs.mkdtempSync(CHROME_PROFILE_PATH); temporaryUserDataDir = fs.mkdtempSync(CHROME_PROFILE_PATH);
@ -105,7 +112,7 @@ class Launcher {
const connectionDelay = options.slowMo || 0; const connectionDelay = options.slowMo || 0;
const browserWSEndpoint = await waitForWSEndpoint(chromeProcess, options.timeout || 30 * 1000); const browserWSEndpoint = await waitForWSEndpoint(chromeProcess, options.timeout || 30 * 1000);
const connection = await Connection.create(browserWSEndpoint, connectionDelay); const connection = await Connection.create(browserWSEndpoint, connectionDelay);
return new Browser(connection, !!options.ignoreHTTPSErrors, killChrome); return new Browser(connection, options, killChrome);
} catch (e) { } catch (e) {
killChrome(); killChrome();
throw e; throw e;
@ -150,12 +157,12 @@ class Launcher {
} }
/** /**
* @param {string} options * @param {!Object=} options
* @return {!Promise<!Browser>} * @return {!Promise<!Browser>}
*/ */
static async connect({browserWSEndpoint, ignoreHTTPSErrors = false}) { static async connect(options = {}) {
const connection = await Connection.create(browserWSEndpoint); const connection = await Connection.create(options.browserWSEndpoint);
return new Browser(connection, !!ignoreHTTPSErrors); return new Browser(connection, options);
} }
} }

View File

@ -31,10 +31,11 @@ class Page extends EventEmitter {
* @param {!Session} client * @param {!Session} client
* @param {string} sessionId * @param {string} sessionId
* @param {boolean} ignoreHTTPSErrors * @param {boolean} ignoreHTTPSErrors
* @param {boolean} appMode
* @param {!TaskQueue} screenshotTaskQueue * @param {!TaskQueue} screenshotTaskQueue
* @return {!Promise<!Page>} * @return {!Promise<!Page>}
*/ */
static async create(client, ignoreHTTPSErrors, screenshotTaskQueue) { static async create(client, ignoreHTTPSErrors, appMode, screenshotTaskQueue) {
await Promise.all([ await Promise.all([
client.send('Network.enable', {}), client.send('Network.enable', {}),
client.send('Page.enable', {}), client.send('Page.enable', {}),
@ -46,7 +47,8 @@ class Page extends EventEmitter {
const page = new Page(client, ignoreHTTPSErrors, screenshotTaskQueue); const page = new Page(client, ignoreHTTPSErrors, screenshotTaskQueue);
await page.goto('about:blank'); await page.goto('about:blank');
// Initialize default page size. // Initialize default page size.
await page.setViewport({width: 800, height: 600}); if (!appMode)
await page.setViewport({width: 800, height: 600});
return page; return page;
} }