chore: verify launch options (#2530)

This commit is contained in:
Pavel Feldman 2020-06-10 20:48:54 -07:00 committed by GitHub
parent de893c652e
commit 17433d1881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 17 deletions

View File

@ -53,11 +53,6 @@ export type LaunchOptionsBase = {
downloadsPath?: string,
};
export function processBrowserArgOptions(options: LaunchOptionsBase): { devtools: boolean, headless: boolean } {
const { devtools = false, headless = !devtools } = options;
return { devtools, headless };
}
type ConnectOptions = {
wsEndpoint: string,
slowMo?: number,
@ -109,6 +104,7 @@ export abstract class BrowserTypeBase implements BrowserType {
async launch(options: LaunchOptions = {}): Promise<Browser> {
assert(!(options as any).userDataDir, 'userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistentContext` instead');
assert(!(options as any).port, 'Cannot specify a port without launching as a server.');
options = validateLaunchOptions(options);
const logger = new InnerLogger(options.logger);
const browser = await runAbortableTask(progress => this._innerLaunch(progress, options, logger, undefined), logger, TimeoutSettings.timeout(options));
return browser;
@ -116,6 +112,7 @@ export abstract class BrowserTypeBase implements BrowserType {
async launchPersistentContext(userDataDir: string, options: LaunchOptions & PersistentContextOptions = {}): Promise<BrowserContext> {
assert(!(options as any).port, 'Cannot specify a port without launching as a server.');
options = validateLaunchOptions(options);
const persistent = validateBrowserContextOptions(options);
const logger = new InnerLogger(options.logger);
const browser = await runAbortableTask(progress => this._innerLaunch(progress, options, logger, persistent, userDataDir), logger, TimeoutSettings.timeout(options));
@ -130,7 +127,7 @@ export abstract class BrowserTypeBase implements BrowserType {
const browserOptions: BrowserOptions = {
slowMo: options.slowMo,
persistent,
headful: !processBrowserArgOptions(options).headless,
headful: !options.headless,
logger,
downloadsPath,
ownedServer: browserServer,
@ -147,8 +144,9 @@ export abstract class BrowserTypeBase implements BrowserType {
async launchServer(options: LaunchServerOptions = {}): Promise<BrowserServer> {
assert(!(options as any).userDataDir, 'userDataDir option is not supported in `browserType.launchServer`. Use `browserType.launchPersistentContext` instead');
const { port = 0 } = options;
options = validateLaunchOptions(options);
const logger = new InnerLogger(options.logger);
const { port = 0 } = options;
return runAbortableTask(async progress => {
const { browserServer, transport } = await this._launchServer(progress, options, false, logger);
browserServer._webSocketServer = this._startWebSocketServer(transport, logger, port);
@ -260,3 +258,8 @@ function copyTestHooks(from: object, to: object) {
(to as any)[key] = value;
}
}
function validateLaunchOptions<Options extends LaunchOptionsBase>(options: Options): Options {
const { devtools = false, headless = !devtools } = options;
return { ...options, devtools, headless };
}

View File

@ -21,7 +21,7 @@ import { CRBrowser } from '../chromium/crBrowser';
import * as ws from 'ws';
import { Env } from './processLauncher';
import { kBrowserCloseMessageId } from '../chromium/crConnection';
import { LaunchOptionsBase, BrowserTypeBase, processBrowserArgOptions } from './browserType';
import { LaunchOptionsBase, BrowserTypeBase } from './browserType';
import { ConnectionTransport, ProtocolRequest, ProtocolResponse } from '../transport';
import { InnerLogger } from '../logger';
import { BrowserDescriptor } from '../install/browserPaths';
@ -78,7 +78,6 @@ export class Chromium extends BrowserTypeBase {
}
_defaultArgs(options: LaunchOptionsBase, isPersistent: boolean, userDataDir: string): string[] {
const { devtools, headless } = processBrowserArgOptions(options);
const { args = [], proxy } = options;
const userDataDirArg = args.find(arg => arg.startsWith('--user-data-dir'));
if (userDataDirArg)
@ -93,9 +92,9 @@ export class Chromium extends BrowserTypeBase {
chromeArguments.push('--remote-debugging-port=' + this._debugPort);
else
chromeArguments.push('--remote-debugging-pipe');
if (devtools)
if (options.devtools)
chromeArguments.push('--auto-open-devtools-for-tabs');
if (headless) {
if (options.headless) {
chromeArguments.push(
'--headless',
'--hide-scrollbars',

View File

@ -21,7 +21,7 @@ import * as path from 'path';
import * as ws from 'ws';
import { FFBrowser } from '../firefox/ffBrowser';
import { kBrowserCloseMessageId } from '../firefox/ffConnection';
import { LaunchOptionsBase, BrowserTypeBase, processBrowserArgOptions, FirefoxUserPrefsOptions } from './browserType';
import { LaunchOptionsBase, BrowserTypeBase, FirefoxUserPrefsOptions } from './browserType';
import { Env } from './processLauncher';
import { ConnectionTransport, ProtocolResponse, ProtocolRequest } from '../transport';
import { InnerLogger } from '../logger';
@ -57,8 +57,7 @@ export class Firefox extends BrowserTypeBase {
}
_defaultArgs(options: LaunchOptionsBase & FirefoxUserPrefsOptions, isPersistent: boolean, userDataDir: string): string[] {
const { devtools, headless } = processBrowserArgOptions(options);
const { args = [], proxy } = options;
const { args = [], proxy, devtools, headless } = options;
if (devtools)
console.warn('devtools parameter is not supported as a launch argument in Firefox. You can launch the devtools window manually.');
const userDataDirArg = args.find(arg => arg.startsWith('-profile') || arg.startsWith('--profile'));

View File

@ -19,7 +19,7 @@ import { WKBrowser } from '../webkit/wkBrowser';
import { Env } from './processLauncher';
import * as path from 'path';
import { kBrowserCloseMessageId } from '../webkit/wkConnection';
import { LaunchOptionsBase, BrowserTypeBase, processBrowserArgOptions } from './browserType';
import { LaunchOptionsBase, BrowserTypeBase } from './browserType';
import { ConnectionTransport, ProtocolResponse, ProtocolRequest } from '../transport';
import * as ws from 'ws';
import { InnerLogger } from '../logger';
@ -50,8 +50,7 @@ export class WebKit extends BrowserTypeBase {
}
_defaultArgs(options: LaunchOptionsBase, isPersistent: boolean, userDataDir: string): string[] {
const { devtools, headless } = processBrowserArgOptions(options);
const { args = [], proxy } = options;
const { args = [], proxy, devtools, headless } = options;
if (devtools)
console.warn('devtools parameter as a launch argument in WebKit is not supported. Also starting Web Inspector manually will terminate the execution in WebKit.');
const userDataDirArg = args.find(arg => arg.startsWith('--user-data-dir='));