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

View File

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

View File

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

View File

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