2020-01-24 14:49:47 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-02-11 06:36:15 -08:00
|
|
|
import fs from 'fs';
|
2020-05-22 07:03:42 -07:00
|
|
|
import * as os from 'os';
|
2021-02-11 06:36:15 -08:00
|
|
|
import path from 'path';
|
2020-05-22 07:03:42 -07:00
|
|
|
import * as util from 'util';
|
2020-08-28 14:17:16 -07:00
|
|
|
import { BrowserContext, normalizeProxySettings, validateBrowserContextOptions } from './browserContext';
|
2021-02-08 16:02:49 -08:00
|
|
|
import * as registry from '../utils/registry';
|
2020-11-05 14:15:25 -08:00
|
|
|
import { ConnectionTransport } from './transport';
|
2021-01-29 16:00:56 -08:00
|
|
|
import { BrowserOptions, Browser, BrowserProcess, PlaywrightOptions } from './browser';
|
2020-11-05 14:15:25 -08:00
|
|
|
import { launchProcess, Env, envArrayToObject } from './processLauncher';
|
2020-05-22 07:03:42 -07:00
|
|
|
import { PipeTransport } from './pipeTransport';
|
2020-09-10 15:34:13 -07:00
|
|
|
import { Progress, ProgressController } from './progress';
|
2020-08-24 06:51:51 -07:00
|
|
|
import * as types from './types';
|
2020-08-22 07:07:13 -07:00
|
|
|
import { TimeoutSettings } from '../utils/timeoutSettings';
|
2020-07-24 16:14:14 -07:00
|
|
|
import { validateHostRequirements } from './validateDependencies';
|
2020-09-14 14:43:39 -07:00
|
|
|
import { isDebugMode } from '../utils/utils';
|
2020-11-11 15:12:10 -08:00
|
|
|
import { helper } from './helper';
|
2020-12-08 09:35:28 -08:00
|
|
|
import { RecentLogsCollector } from '../utils/debugLogger';
|
2021-02-09 14:44:48 -08:00
|
|
|
import { CallMetadata, SdkObject } from './instrumentation';
|
2020-01-24 14:49:47 -08:00
|
|
|
|
2020-06-08 21:45:35 -07:00
|
|
|
const mkdirAsync = util.promisify(fs.mkdir);
|
2020-05-22 07:03:42 -07:00
|
|
|
const mkdtempAsync = util.promisify(fs.mkdtemp);
|
2020-07-29 23:16:24 -07:00
|
|
|
const existsAsync = (path: string): Promise<boolean> => new Promise(resolve => fs.stat(path, err => resolve(!err)));
|
2020-05-22 16:06:00 -07:00
|
|
|
const DOWNLOADS_FOLDER = path.join(os.tmpdir(), 'playwright_downloads-');
|
2020-05-22 07:03:42 -07:00
|
|
|
|
2021-02-09 09:00:00 -08:00
|
|
|
export abstract class BrowserType extends SdkObject {
|
2021-02-08 16:02:49 -08:00
|
|
|
private _name: registry.BrowserName;
|
|
|
|
readonly _registry: registry.Registry;
|
2021-01-29 16:00:56 -08:00
|
|
|
readonly _playwrightOptions: PlaywrightOptions;
|
2020-04-28 17:06:01 -07:00
|
|
|
|
2021-02-08 16:02:49 -08:00
|
|
|
constructor(browserName: registry.BrowserName, playwrightOptions: PlaywrightOptions) {
|
2021-02-09 09:00:00 -08:00
|
|
|
super(playwrightOptions.rootSdkObject);
|
|
|
|
this.attribution.browserType = this;
|
2021-01-29 16:00:56 -08:00
|
|
|
this._playwrightOptions = playwrightOptions;
|
2021-02-08 16:02:49 -08:00
|
|
|
this._name = browserName;
|
|
|
|
this._registry = playwrightOptions.registry;
|
2020-04-28 17:06:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
executablePath(): string {
|
2021-02-08 16:02:49 -08:00
|
|
|
return this._registry.executablePath(this._name) || '';
|
2020-04-28 17:06:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
name(): string {
|
|
|
|
return this._name;
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
async launch(metadata: CallMetadata, options: types.LaunchOptions, protocolLogger?: types.ProtocolLogger): Promise<Browser> {
|
2020-06-10 20:48:54 -07:00
|
|
|
options = validateLaunchOptions(options);
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-09-10 15:34:13 -07:00
|
|
|
controller.setLogName('browser');
|
2020-09-17 09:32:54 -07:00
|
|
|
const browser = await controller.run(progress => {
|
2021-02-01 15:23:57 -08:00
|
|
|
return this._innerLaunchWithRetries(progress, options, undefined, helper.debugProtocolLogger(protocolLogger)).catch(e => { throw this._rewriteStartupError(e); });
|
2020-09-17 09:32:54 -07:00
|
|
|
}, TimeoutSettings.timeout(options));
|
2020-05-29 14:39:34 -07:00
|
|
|
return browser;
|
2020-05-20 16:30:04 -07:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:46:54 -08:00
|
|
|
async launchPersistentContext(metadata: CallMetadata, userDataDir: string, options: types.LaunchPersistentOptions): Promise<BrowserContext> {
|
2020-06-10 20:48:54 -07:00
|
|
|
options = validateLaunchOptions(options);
|
2021-02-09 14:44:48 -08:00
|
|
|
const controller = new ProgressController(metadata, this);
|
2020-08-18 09:37:40 -07:00
|
|
|
const persistent: types.BrowserContextOptions = options;
|
2020-09-10 15:34:13 -07:00
|
|
|
controller.setLogName('browser');
|
2020-09-17 09:32:54 -07:00
|
|
|
const browser = await controller.run(progress => {
|
2021-02-01 15:23:57 -08:00
|
|
|
return this._innerLaunchWithRetries(progress, options, persistent, helper.debugProtocolLogger(), userDataDir).catch(e => { throw this._rewriteStartupError(e); });
|
2020-09-17 09:32:54 -07:00
|
|
|
}, TimeoutSettings.timeout(options));
|
2020-05-29 14:39:34 -07:00
|
|
|
return browser._defaultContext!;
|
2020-05-21 09:43:10 -07:00
|
|
|
}
|
2020-05-20 16:30:04 -07:00
|
|
|
|
2021-02-01 15:23:57 -08:00
|
|
|
async _innerLaunchWithRetries(progress: Progress, options: types.LaunchOptions, persistent: types.BrowserContextOptions | undefined, protocolLogger: types.ProtocolLogger, userDataDir?: string): Promise<Browser> {
|
|
|
|
try {
|
|
|
|
return this._innerLaunch(progress, options, persistent, protocolLogger, userDataDir);
|
|
|
|
} catch (error) {
|
|
|
|
// @see https://github.com/microsoft/playwright/issues/5214
|
|
|
|
const errorMessage = typeof error === 'object' && typeof error.message === 'string' ? error.message : '';
|
|
|
|
if (errorMessage.includes('Inconsistency detected by ld.so')) {
|
|
|
|
progress.log(`<restarting browser due to hitting race condition in glibc>`);
|
|
|
|
return this._innerLaunch(progress, options, persistent, protocolLogger, userDataDir);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 15:12:10 -08:00
|
|
|
async _innerLaunch(progress: Progress, options: types.LaunchOptions, persistent: types.BrowserContextOptions | undefined, protocolLogger: types.ProtocolLogger, userDataDir?: string): Promise<Browser> {
|
2020-08-28 14:17:16 -07:00
|
|
|
options.proxy = options.proxy ? normalizeProxySettings(options.proxy) : undefined;
|
2020-12-08 09:35:28 -08:00
|
|
|
const browserLogsCollector = new RecentLogsCollector();
|
|
|
|
const { browserProcess, downloadsPath, transport } = await this._launchProcess(progress, options, !!persistent, browserLogsCollector, userDataDir);
|
2020-05-29 14:39:34 -07:00
|
|
|
if ((options as any).__testHookBeforeCreateBrowser)
|
|
|
|
await (options as any).__testHookBeforeCreateBrowser();
|
|
|
|
const browserOptions: BrowserOptions = {
|
2021-01-29 16:00:56 -08:00
|
|
|
...this._playwrightOptions,
|
2020-07-08 21:36:03 -07:00
|
|
|
name: this._name,
|
2021-01-13 12:08:14 -08:00
|
|
|
isChromium: this._name === 'chromium',
|
2020-05-29 14:39:34 -07:00
|
|
|
slowMo: options.slowMo,
|
|
|
|
persistent,
|
2020-06-10 20:48:54 -07:00
|
|
|
headful: !options.headless,
|
2020-05-29 14:39:34 -07:00
|
|
|
downloadsPath,
|
2020-08-14 13:19:12 -07:00
|
|
|
browserProcess,
|
2020-06-05 13:50:15 -07:00
|
|
|
proxy: options.proxy,
|
2020-11-11 15:12:10 -08:00
|
|
|
protocolLogger,
|
2020-12-08 09:35:28 -08:00
|
|
|
browserLogsCollector,
|
2020-05-29 14:39:34 -07:00
|
|
|
};
|
2020-09-18 17:36:43 -07:00
|
|
|
if (persistent)
|
|
|
|
validateBrowserContextOptions(persistent, browserOptions);
|
2020-05-29 14:39:34 -07:00
|
|
|
copyTestHooks(options, browserOptions);
|
2020-05-22 16:06:00 -07:00
|
|
|
const browser = await this._connectToTransport(transport, browserOptions);
|
|
|
|
// We assume no control when using custom arguments, and do not prepare the default context in that case.
|
2020-08-18 09:37:40 -07:00
|
|
|
if (persistent && !options.ignoreAllDefaultArgs)
|
2020-08-17 14:36:51 -07:00
|
|
|
await browser._defaultContext!._loadDefaultContext(progress);
|
2020-05-21 09:43:10 -07:00
|
|
|
return browser;
|
2020-05-20 16:30:04 -07:00
|
|
|
}
|
|
|
|
|
2020-12-08 09:35:28 -08:00
|
|
|
private async _launchProcess(progress: Progress, options: types.LaunchOptions, isPersistent: boolean, browserLogsCollector: RecentLogsCollector, userDataDir?: string): Promise<{ browserProcess: BrowserProcess, downloadsPath: string, transport: ConnectionTransport }> {
|
2020-05-22 07:03:42 -07:00
|
|
|
const {
|
2020-08-18 09:37:40 -07:00
|
|
|
ignoreDefaultArgs,
|
|
|
|
ignoreAllDefaultArgs,
|
2020-05-22 07:03:42 -07:00
|
|
|
args = [],
|
|
|
|
executablePath = null,
|
|
|
|
handleSIGINT = true,
|
|
|
|
handleSIGTERM = true,
|
|
|
|
handleSIGHUP = true,
|
|
|
|
} = options;
|
|
|
|
|
2020-08-18 09:37:40 -07:00
|
|
|
const env = options.env ? envArrayToObject(options.env) : process.env;
|
|
|
|
|
2020-06-08 21:45:35 -07:00
|
|
|
const tempDirectories = [];
|
2020-09-04 22:37:38 -07:00
|
|
|
const ensurePath = async (tmpPrefix: string, pathFromOptions?: string) => {
|
|
|
|
let dir;
|
|
|
|
if (pathFromOptions) {
|
|
|
|
dir = pathFromOptions;
|
|
|
|
await mkdirAsync(pathFromOptions, { recursive: true });
|
|
|
|
} else {
|
|
|
|
dir = await mkdtempAsync(tmpPrefix);
|
|
|
|
tempDirectories.push(dir);
|
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
};
|
2020-10-01 11:06:19 -07:00
|
|
|
// TODO: add downloadsPath to newContext().
|
2020-09-04 22:37:38 -07:00
|
|
|
const downloadsPath = await ensurePath(DOWNLOADS_FOLDER, options.downloadsPath);
|
2020-06-08 21:45:35 -07:00
|
|
|
|
2020-05-22 07:03:42 -07:00
|
|
|
if (!userDataDir) {
|
|
|
|
userDataDir = await mkdtempAsync(path.join(os.tmpdir(), `playwright_${this._name}dev_profile-`));
|
2020-05-22 16:06:00 -07:00
|
|
|
tempDirectories.push(userDataDir);
|
2020-05-22 07:03:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const browserArguments = [];
|
2020-08-18 09:37:40 -07:00
|
|
|
if (ignoreAllDefaultArgs)
|
|
|
|
browserArguments.push(...args);
|
|
|
|
else if (ignoreDefaultArgs)
|
2020-05-22 16:06:00 -07:00
|
|
|
browserArguments.push(...this._defaultArgs(options, isPersistent, userDataDir).filter(arg => ignoreDefaultArgs.indexOf(arg) === -1));
|
2020-05-22 07:03:42 -07:00
|
|
|
else
|
2020-08-18 09:37:40 -07:00
|
|
|
browserArguments.push(...this._defaultArgs(options, isPersistent, userDataDir));
|
2020-05-22 07:03:42 -07:00
|
|
|
|
|
|
|
const executable = executablePath || this.executablePath();
|
|
|
|
if (!executable)
|
|
|
|
throw new Error(`No executable path is specified. Pass "executablePath" option directly.`);
|
2020-07-29 23:16:24 -07:00
|
|
|
if (!(await existsAsync(executable))) {
|
|
|
|
const errorMessageLines = [`Failed to launch ${this._name} because executable doesn't exist at ${executable}`];
|
|
|
|
// If we tried using stock downloaded browser, suggest re-installing playwright.
|
|
|
|
if (!executablePath)
|
|
|
|
errorMessageLines.push(`Try re-installing playwright with "npm install playwright"`);
|
|
|
|
throw new Error(errorMessageLines.join('\n'));
|
|
|
|
}
|
2020-05-22 07:03:42 -07:00
|
|
|
|
2020-07-15 15:24:38 -07:00
|
|
|
if (!executablePath) {
|
|
|
|
// We can only validate dependencies for bundled browsers.
|
2021-02-08 16:02:49 -08:00
|
|
|
await validateHostRequirements(this._registry, this._name);
|
2020-07-15 15:24:38 -07:00
|
|
|
}
|
|
|
|
|
2020-05-22 07:03:42 -07:00
|
|
|
// Note: it is important to define these variables before launchProcess, so that we don't get
|
|
|
|
// "Cannot access 'browserServer' before initialization" if something went wrong.
|
|
|
|
let transport: ConnectionTransport | undefined = undefined;
|
2020-08-14 13:19:12 -07:00
|
|
|
let browserProcess: BrowserProcess | undefined = undefined;
|
2020-05-27 19:59:03 -07:00
|
|
|
const { launchedProcess, gracefullyClose, kill } = await launchProcess({
|
2020-05-22 07:03:42 -07:00
|
|
|
executablePath: executable,
|
2020-10-09 11:28:22 -07:00
|
|
|
args: browserArguments,
|
2020-05-22 07:03:42 -07:00
|
|
|
env: this._amendEnvironment(env, userDataDir, executable, browserArguments),
|
|
|
|
handleSIGINT,
|
|
|
|
handleSIGTERM,
|
|
|
|
handleSIGHUP,
|
2020-12-08 09:35:28 -08:00
|
|
|
log: (message: string) => {
|
|
|
|
progress.log(message);
|
|
|
|
browserLogsCollector.log(message);
|
|
|
|
},
|
2020-11-05 14:15:25 -08:00
|
|
|
stdio: 'pipe',
|
2020-05-22 16:06:00 -07:00
|
|
|
tempDirectories,
|
2020-05-22 07:03:42 -07:00
|
|
|
attemptToGracefullyClose: async () => {
|
|
|
|
if ((options as any).__testHookGracefullyClose)
|
|
|
|
await (options as any).__testHookGracefullyClose();
|
|
|
|
// We try to gracefully close to prevent crash reporting and core dumps.
|
|
|
|
// Note that it's fine to reuse the pipe transport, since
|
|
|
|
// our connection ignores kBrowserCloseMessageId.
|
|
|
|
this._attemptToGracefullyCloseBrowser(transport!);
|
|
|
|
},
|
2020-05-27 19:59:03 -07:00
|
|
|
onExit: (exitCode, signal) => {
|
2020-08-14 13:19:12 -07:00
|
|
|
if (browserProcess && browserProcess.onclose)
|
|
|
|
browserProcess.onclose(exitCode, signal);
|
2020-05-22 07:03:42 -07:00
|
|
|
},
|
|
|
|
});
|
2020-08-14 13:19:12 -07:00
|
|
|
browserProcess = {
|
|
|
|
onclose: undefined,
|
|
|
|
process: launchedProcess,
|
|
|
|
close: gracefullyClose,
|
|
|
|
kill
|
|
|
|
};
|
|
|
|
progress.cleanupWhenAborted(() => browserProcess && closeOrKill(browserProcess, progress.timeUntilDeadline()));
|
2020-05-29 14:39:34 -07:00
|
|
|
|
2020-11-05 14:15:25 -08:00
|
|
|
const stdio = launchedProcess.stdio as unknown as [NodeJS.ReadableStream, NodeJS.WritableStream, NodeJS.WritableStream, NodeJS.WritableStream, NodeJS.ReadableStream];
|
|
|
|
transport = new PipeTransport(stdio[3], stdio[4]);
|
2020-09-18 17:36:43 -07:00
|
|
|
return { browserProcess, downloadsPath, transport };
|
2020-05-22 07:03:42 -07:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:46:54 -08:00
|
|
|
async connectOverCDP(metadata: CallMetadata, wsEndpoint: string, options: { slowMo?: number, sdkLanguage: string }, timeout?: number): Promise<Browser> {
|
2021-02-10 14:00:02 -08:00
|
|
|
throw new Error('CDP connections are only supported by Chromium');
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:37:40 -07:00
|
|
|
abstract _defaultArgs(options: types.LaunchOptions, isPersistent: boolean, userDataDir: string): string[];
|
2020-08-19 10:31:59 -07:00
|
|
|
abstract _connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<Browser>;
|
2020-05-22 07:03:42 -07:00
|
|
|
abstract _amendEnvironment(env: Env, userDataDir: string, executable: string, browserArguments: string[]): Env;
|
2020-08-14 18:25:32 -07:00
|
|
|
abstract _rewriteStartupError(error: Error): Error;
|
2020-05-22 07:03:42 -07:00
|
|
|
abstract _attemptToGracefullyCloseBrowser(transport: ConnectionTransport): void;
|
|
|
|
}
|
2020-05-22 16:06:00 -07:00
|
|
|
|
|
|
|
function copyTestHooks(from: object, to: object) {
|
|
|
|
for (const [key, value] of Object.entries(from)) {
|
|
|
|
if (key.startsWith('__testHook'))
|
|
|
|
(to as any)[key] = value;
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 20:48:54 -07:00
|
|
|
|
2020-08-18 09:37:40 -07:00
|
|
|
function validateLaunchOptions<Options extends types.LaunchOptions>(options: Options): Options {
|
2020-08-22 07:07:13 -07:00
|
|
|
const { devtools = false, headless = !isDebugMode() && !devtools } = options;
|
2020-06-10 20:48:54 -07:00
|
|
|
return { ...options, devtools, headless };
|
|
|
|
}
|
2020-08-14 13:19:12 -07:00
|
|
|
|
|
|
|
async function closeOrKill(browserProcess: BrowserProcess, timeout: number): Promise<void> {
|
|
|
|
let timer: NodeJS.Timer;
|
|
|
|
try {
|
|
|
|
await Promise.race([
|
|
|
|
browserProcess.close(),
|
|
|
|
new Promise((resolve, reject) => timer = setTimeout(reject, timeout)),
|
|
|
|
]);
|
|
|
|
} catch (ignored) {
|
|
|
|
await browserProcess.kill().catch(ignored => {}); // Make sure to await actual process exit.
|
|
|
|
} finally {
|
|
|
|
clearTimeout(timer!);
|
|
|
|
}
|
|
|
|
}
|