From dc161df063d00481b3a8fd3234cea5aacaf6625c Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 27 Feb 2020 14:09:24 -0800 Subject: [PATCH] fix(launch): throw upon page argument when non-persistent (#1144) --- src/server/chromium.ts | 2 ++ src/server/firefox.ts | 14 ++++++++------ src/server/webkit.ts | 2 ++ test/launcher.spec.js | 6 ++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/server/chromium.ts b/src/server/chromium.ts index d03b22d66d..16bc78ba65 100644 --- a/src/server/chromium.ts +++ b/src/server/chromium.ts @@ -178,6 +178,8 @@ export class Chromium implements BrowserType { throw new Error('Pass userDataDir parameter instead of specifying --user-data-dir argument'); if (args.find(arg => arg.startsWith('--remote-debugging-'))) throw new Error('Playwright manages remote debugging connection itself.'); + if (launchType !== 'persistent' && args.find(arg => !arg.startsWith('-'))) + throw new Error('Arguments can not specify page to be opened'); const chromeArguments = [...DEFAULT_ARGS]; chromeArguments.push(`--user-data-dir=${userDataDir}`); diff --git a/src/server/firefox.ts b/src/server/firefox.ts index 0de94a8d0a..adbd6601b9 100644 --- a/src/server/firefox.ts +++ b/src/server/firefox.ts @@ -85,7 +85,7 @@ export class Firefox implements BrowserType { return browserContext; } - private async _launchServer(options: LaunchOptions = {}, connectionType: LaunchType, userDataDir?: string, port?: number): Promise<{ browserServer: BrowserServer, transport?: ConnectionTransport }> { + private async _launchServer(options: LaunchOptions = {}, launchType: LaunchType, userDataDir?: string, port?: number): Promise<{ browserServer: BrowserServer, transport?: ConnectionTransport }> { const { ignoreDefaultArgs = false, args = [], @@ -107,9 +107,9 @@ export class Firefox implements BrowserType { } if (!ignoreDefaultArgs) - firefoxArguments.push(...this._defaultArgs(options, userDataDir!, port || 0)); + firefoxArguments.push(...this._defaultArgs(options, launchType, userDataDir!, port || 0)); else if (Array.isArray(ignoreDefaultArgs)) - firefoxArguments.push(...this._defaultArgs(options, userDataDir!, port || 0).filter(arg => !ignoreDefaultArgs.includes(arg))); + firefoxArguments.push(...this._defaultArgs(options, launchType, userDataDir!, port || 0).filter(arg => !ignoreDefaultArgs.includes(arg))); else firefoxArguments.push(...args); @@ -155,8 +155,8 @@ export class Firefox implements BrowserType { const timeoutError = new TimeoutError(`Timed out after ${timeout} ms while trying to connect to Firefox!`); const match = await waitForLine(launchedProcess, launchedProcess.stdout, /^Juggler listening on (ws:\/\/.*)$/, timeout, timeoutError); const browserWSEndpoint = match[1]; - browserServer = new BrowserServer(launchedProcess, gracefullyClose, connectionType === 'server' ? browserWSEndpoint : null); - return { browserServer, transport: connectionType === 'server' ? undefined : new platform.WebSocketTransport(browserWSEndpoint) }; + browserServer = new BrowserServer(launchedProcess, gracefullyClose, launchType === 'server' ? browserWSEndpoint : null); + return { browserServer, transport: launchType === 'server' ? undefined : new platform.WebSocketTransport(browserWSEndpoint) }; } async connect(options: ConnectOptions): Promise { @@ -176,7 +176,7 @@ export class Firefox implements BrowserType { return { TimeoutError }; } - private _defaultArgs(options: BrowserArgOptions = {}, userDataDir: string, port: number): string[] { + private _defaultArgs(options: BrowserArgOptions = {}, launchType: LaunchType, userDataDir: string, port: number): string[] { const { devtools = false, headless = !devtools, @@ -189,6 +189,8 @@ export class Firefox implements BrowserType { throw new Error('Pass userDataDir parameter instead of specifying -profile argument'); if (args.find(arg => arg.startsWith('-juggler'))) throw new Error('Use the port parameter instead of -juggler argument'); + if (launchType !== 'persistent' && args.find(arg => !arg.startsWith('-'))) + throw new Error('Arguments can not specify page to be opened'); const firefoxArguments = ['-no-remote']; if (headless) { diff --git a/src/server/webkit.ts b/src/server/webkit.ts index 414aceb591..b93e380b35 100644 --- a/src/server/webkit.ts +++ b/src/server/webkit.ts @@ -181,6 +181,8 @@ export class WebKit implements BrowserType { const userDataDirArg = args.find(arg => arg.startsWith('--user-data-dir=')); if (userDataDirArg) throw new Error('Pass userDataDir parameter instead of specifying --user-data-dir argument'); + if (launchType !== 'persistent' && args.find(arg => !arg.startsWith('-'))) + throw new Error('Arguments can not specify page to be opened'); const webkitArguments = ['--inspector-pipe']; if (headless) webkitArguments.push('--headless'); diff --git a/test/launcher.spec.js b/test/launcher.spec.js index fe81d51d9b..ac81ad3281 100644 --- a/test/launcher.spec.js +++ b/test/launcher.spec.js @@ -45,6 +45,12 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p await playwright.launch(options).catch(e => waitError = e); expect(waitError.message).toContain('launchPersistent'); }); + it('should throw if page argument is passed', async() => { + let waitError = null; + const options = Object.assign({}, defaultBrowserOptions, { args: ['http://example.com'] }); + await playwright.launch(options).catch(e => waitError = e); + expect(waitError.message).toContain('can not specify page'); + }); it('should reject if executable path is invalid', async({server}) => { let waitError = null; const options = Object.assign({}, defaultBrowserOptions, {executablePath: 'random-invalid-path'});