cleanup: Use ES6 default params (#447)

This commit is contained in:
Eric Bidelman 2017-08-21 16:32:39 -07:00 committed by Andrey Lushnikov
parent 271fd09379
commit 5d6d3e0a81
4 changed files with 25 additions and 34 deletions

View File

@ -31,17 +31,17 @@ class Keyboard {
* @param {{text: (string|undefined)}} options * @param {{text: (string|undefined)}} options
* @return {!Promise} * @return {!Promise}
*/ */
async down(key, options) { async down(key, options = {}) {
let {text} = options || {}; const text = options.text;
let autoRepeat = this._pressedKeys.has(key); const autoRepeat = this._pressedKeys.has(key);
this._pressedKeys.add(key); this._pressedKeys.add(key);
this._modifiers |= this._modifierBit(key); this._modifiers |= this._modifierBit(key);
await this._client.send('Input.dispatchKeyEvent', { await this._client.send('Input.dispatchKeyEvent', {
type: text ? 'keyDown' : 'rawKeyDown', type: text ? 'keyDown' : 'rawKeyDown',
modifiers: this._modifiers, modifiers: this._modifiers,
windowsVirtualKeyCode: codeForKey(key), windowsVirtualKeyCode: codeForKey(key),
key: key, key,
text: text, text,
unmodifiedText: text, unmodifiedText: text,
autoRepeat autoRepeat
}); });
@ -127,10 +127,10 @@ class Mouse {
* @param {number} y * @param {number} y
* @param {!Object=} options * @param {!Object=} options
*/ */
async click(x, y, options) { async click(x, y, options = {}) {
this.move(x, y); this.move(x, y);
this.down(options); this.down(options);
if (options && options.delay) if (typeof options.delay === 'number')
await new Promise(f => setTimeout(f, options.delay)); await new Promise(f => setTimeout(f, options.delay));
await this.up(options); await this.up(options);
} }
@ -138,9 +138,7 @@ class Mouse {
/** /**
* @param {!Object=} options * @param {!Object=} options
*/ */
async down(options) { async down(options = {}) {
if (!options)
options = {};
this._button = (options.button || 'left'); this._button = (options.button || 'left');
await this._client.send('Input.dispatchMouseEvent', { await this._client.send('Input.dispatchMouseEvent', {
type: 'mousePressed', type: 'mousePressed',
@ -155,9 +153,7 @@ class Mouse {
/** /**
* @param {!Object=} options * @param {!Object=} options
*/ */
async up(options) { async up(options = {}) {
if (!options)
options = {};
this._button = 'none'; this._button = 'none';
await this._client.send('Input.dispatchMouseEvent', { await this._client.send('Input.dispatchMouseEvent', {
type: 'mouseReleased', type: 'mouseReleased',

View File

@ -47,55 +47,54 @@ const DEFAULT_ARGS = [
class Launcher { class Launcher {
/** /**
* @param {!Object} options * @param {!Object=} options
* @return {!Promise<!Browser>} * @return {!Promise<!Browser>}
*/ */
static async launch(options) { static async launch(options = {}) {
options = options || {}; const userDataDir = [
let userDataDir = [
CHROME_PROFILE_PATH, CHROME_PROFILE_PATH,
process.pid, process.pid,
++browserId, ++browserId,
crypto.randomBytes(8 / 2).toString('hex') // add random salt 8 characters long. crypto.randomBytes(8 / 2).toString('hex') // add random salt 8 characters long.
].join('-'); ].join('-');
let chromeArguments = DEFAULT_ARGS.concat([ const chromeArguments = DEFAULT_ARGS.concat([
`--user-data-dir=${userDataDir}`, `--user-data-dir=${userDataDir}`,
]); ]);
if (typeof options.headless !== 'boolean' || options.headless) { if (typeof options.headless !== 'boolean' || options.headless) {
chromeArguments.push( chromeArguments.push(
`--headless`, '--headless',
`--disable-gpu`, '--disable-gpu',
`--hide-scrollbars`, '--hide-scrollbars',
'--mute-audio' '--mute-audio'
); );
} }
let chromeExecutable = options.executablePath; let chromeExecutable = options.executablePath;
if (typeof chromeExecutable !== 'string') { if (typeof chromeExecutable !== 'string') {
let revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), ChromiumRevision); const revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), ChromiumRevision);
console.assert(revisionInfo.downloaded, `Chromium revision is not downloaded. Run "npm install"`); console.assert(revisionInfo.downloaded, `Chromium revision is not downloaded. Run "npm install"`);
chromeExecutable = revisionInfo.executablePath; chromeExecutable = revisionInfo.executablePath;
} }
if (Array.isArray(options.args)) if (Array.isArray(options.args))
chromeArguments.push(...options.args); chromeArguments.push(...options.args);
let chromeProcess = childProcess.spawn(chromeExecutable, chromeArguments, {}); const chromeProcess = childProcess.spawn(chromeExecutable, chromeArguments, {});
if (options.dumpio) { if (options.dumpio) {
chromeProcess.stdout.pipe(process.stdout); chromeProcess.stdout.pipe(process.stdout);
chromeProcess.stderr.pipe(process.stderr); chromeProcess.stderr.pipe(process.stderr);
} }
// Cleanup as processes exit. // Cleanup as processes exit.
let listeners = [ const listeners = [
helper.addEventListener(process, 'exit', killChromeAndCleanup), helper.addEventListener(process, 'exit', killChromeAndCleanup),
helper.addEventListener(chromeProcess, 'exit', killChromeAndCleanup), helper.addEventListener(chromeProcess, 'exit', killChromeAndCleanup),
]; ];
if (options.handleSIGINT !== false) if (options.handleSIGINT !== false)
listeners.push(helper.addEventListener(process, 'SIGINT', killChromeAndCleanup)); listeners.push(helper.addEventListener(process, 'SIGINT', killChromeAndCleanup));
try { try {
let connectionDelay = options.slowMo || 0; const connectionDelay = options.slowMo || 0;
let browserWSEndpoint = await waitForWSEndpoint(chromeProcess, options.timeout || 30 * 1000); const browserWSEndpoint = await waitForWSEndpoint(chromeProcess, options.timeout || 30 * 1000);
let connection = await Connection.create(browserWSEndpoint, connectionDelay); const connection = await Connection.create(browserWSEndpoint, connectionDelay);
return new Browser(connection, !!options.ignoreHTTPSErrors, killChromeAndCleanup); return new Browser(connection, !!options.ignoreHTTPSErrors, killChromeAndCleanup);
} catch (e) { } catch (e) {
killChromeAndCleanup(); killChromeAndCleanup();

View File

@ -427,8 +427,7 @@ class Page extends EventEmitter {
* @param {!Object=} options * @param {!Object=} options
* @return {!Promise<!Buffer>} * @return {!Promise<!Buffer>}
*/ */
async screenshot(options) { async screenshot(options = {}) {
options = options || {};
let screenshotType = null; let screenshotType = null;
if (options.path) { if (options.path) {
let mimeType = mime.lookup(options.path); let mimeType = mime.lookup(options.path);
@ -506,9 +505,7 @@ class Page extends EventEmitter {
* @param {!Object=} options * @param {!Object=} options
* @return {!Promise<!Buffer>} * @return {!Promise<!Buffer>}
*/ */
async pdf(options) { async pdf(options = {}) {
options = options || {};
let scale = options.scale || 1; let scale = options.scale || 1;
let displayHeaderFooter = !!options.displayHeaderFooter; let displayHeaderFooter = !!options.displayHeaderFooter;
let printBackground = !!options.printBackground; let printBackground = !!options.printBackground;

View File

@ -28,11 +28,10 @@ class WebPage {
* @param {string} scriptPath * @param {string} scriptPath
* @param {!Object=} options * @param {!Object=} options
*/ */
constructor(browser, scriptPath, options) { constructor(browser, scriptPath, options = {}) {
this._page = await(browser.newPage()); this._page = await(browser.newPage());
this.settings = new WebPageSettings(this._page); this.settings = new WebPageSettings(this._page);
options = options || {};
options.settings = options.settings || {}; options.settings = options.settings || {};
if (options.settings.userAgent) if (options.settings.userAgent)
this.settings.userAgent = options.settings.userAgent; this.settings.userAgent = options.settings.userAgent;