2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
* Modifications 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.
|
|
|
|
*/
|
|
|
|
|
2020-01-07 16:13:49 -08:00
|
|
|
import * as fs from 'fs';
|
2019-12-19 16:53:24 -08:00
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
2020-03-02 13:51:32 -08:00
|
|
|
import { ConnectOptions, LaunchType } from '../browser';
|
|
|
|
import { BrowserContext } from '../browserContext';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { TimeoutError } from '../errors';
|
2020-03-02 13:51:32 -08:00
|
|
|
import { Events } from '../events';
|
|
|
|
import { FFBrowser } from '../firefox/ffBrowser';
|
|
|
|
import { kBrowserCloseMessageId } from '../firefox/ffConnection';
|
2020-03-19 11:43:35 -07:00
|
|
|
import { helper } from '../helper';
|
2020-03-02 13:51:32 -08:00
|
|
|
import * as platform from '../platform';
|
2020-02-05 12:41:55 -08:00
|
|
|
import { BrowserServer } from './browserServer';
|
2020-03-02 13:51:32 -08:00
|
|
|
import { BrowserArgOptions, BrowserType, LaunchOptions } from './browserType';
|
|
|
|
import { launchProcess, waitForLine } from './processLauncher';
|
2020-01-09 14:49:22 -08:00
|
|
|
|
2020-02-11 19:10:02 -08:00
|
|
|
const mkdtempAsync = platform.promisify(fs.mkdtemp);
|
|
|
|
|
2020-03-20 01:30:35 -07:00
|
|
|
export class Firefox implements BrowserType<FFBrowser> {
|
2020-03-19 11:43:35 -07:00
|
|
|
private _executablePath: (string|undefined);
|
2020-01-07 16:13:49 -08:00
|
|
|
|
2020-03-19 11:43:35 -07:00
|
|
|
executablePath(): string {
|
|
|
|
if (!this._executablePath)
|
|
|
|
throw new Error('No executable path!');
|
|
|
|
return this._executablePath;
|
2020-02-11 11:33:48 -08:00
|
|
|
}
|
|
|
|
|
2020-01-28 18:09:07 -08:00
|
|
|
name() {
|
|
|
|
return 'firefox';
|
|
|
|
}
|
|
|
|
|
2020-02-04 19:41:38 -08:00
|
|
|
async launch(options?: LaunchOptions & { slowMo?: number }): Promise<FFBrowser> {
|
2020-02-12 19:32:23 -08:00
|
|
|
if (options && (options as any).userDataDir)
|
2020-03-12 01:10:48 +00:00
|
|
|
throw new Error('userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistentContext` instead');
|
2020-03-02 13:51:32 -08:00
|
|
|
const browserServer = await this._launchServer(options, 'local');
|
|
|
|
const browser = await platform.connectToWebsocket(browserServer.wsEndpoint()!, transport => {
|
2020-03-06 16:49:48 -08:00
|
|
|
return FFBrowser.connect(transport, false, options && options.slowMo);
|
2020-03-02 13:51:32 -08:00
|
|
|
});
|
2020-01-23 08:51:43 -08:00
|
|
|
// Hack: for typical launch scenario, ensure that close waits for actual process termination.
|
2020-02-05 12:41:55 -08:00
|
|
|
browser.close = () => browserServer.close();
|
|
|
|
(browser as any)['__server__'] = browserServer;
|
2020-01-23 08:51:43 -08:00
|
|
|
return browser;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-02-05 12:41:55 -08:00
|
|
|
async launchServer(options?: LaunchOptions & { port?: number }): Promise<BrowserServer> {
|
2020-03-02 13:51:32 -08:00
|
|
|
return await this._launchServer(options, 'server', undefined, options && options.port);
|
2020-02-04 19:41:38 -08:00
|
|
|
}
|
|
|
|
|
2020-03-12 01:10:48 +00:00
|
|
|
async launchPersistentContext(userDataDir: string, options?: LaunchOptions): Promise<BrowserContext> {
|
2020-02-13 13:54:01 -08:00
|
|
|
const { timeout = 30000 } = options || {};
|
2020-03-02 13:51:32 -08:00
|
|
|
const browserServer = await this._launchServer(options, 'persistent', userDataDir);
|
|
|
|
const browser = await platform.connectToWebsocket(browserServer.wsEndpoint()!, transport => {
|
2020-03-06 16:49:48 -08:00
|
|
|
return FFBrowser.connect(transport, true);
|
2020-03-02 13:51:32 -08:00
|
|
|
});
|
2020-03-09 12:32:42 -07:00
|
|
|
await helper.waitWithTimeout(browser._firstPagePromise, 'first page', timeout);
|
2020-02-05 12:41:55 -08:00
|
|
|
// Hack: for typical launch scenario, ensure that close waits for actual process termination.
|
|
|
|
const browserContext = browser._defaultContext;
|
|
|
|
browserContext.close = () => browserServer.close();
|
|
|
|
return browserContext;
|
|
|
|
}
|
|
|
|
|
2020-03-02 13:51:32 -08:00
|
|
|
private async _launchServer(options: LaunchOptions = {}, launchType: LaunchType, userDataDir?: string, port?: number): Promise<BrowserServer> {
|
2019-12-19 16:53:24 -08:00
|
|
|
const {
|
|
|
|
ignoreDefaultArgs = false,
|
|
|
|
args = [],
|
|
|
|
dumpio = false,
|
|
|
|
executablePath = null,
|
|
|
|
env = process.env,
|
|
|
|
handleSIGHUP = true,
|
|
|
|
handleSIGINT = true,
|
|
|
|
handleSIGTERM = true,
|
|
|
|
timeout = 30000,
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
const firefoxArguments = [];
|
|
|
|
|
|
|
|
let temporaryProfileDir = null;
|
2020-02-05 12:41:55 -08:00
|
|
|
if (!userDataDir) {
|
2020-02-05 16:36:36 -08:00
|
|
|
userDataDir = await mkdtempAsync(path.join(os.tmpdir(), 'playwright_dev_firefox_profile-'));
|
|
|
|
temporaryProfileDir = userDataDir;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
2020-02-05 12:41:55 -08:00
|
|
|
|
2020-02-05 16:36:36 -08:00
|
|
|
if (!ignoreDefaultArgs)
|
2020-02-27 14:09:24 -08:00
|
|
|
firefoxArguments.push(...this._defaultArgs(options, launchType, userDataDir!, port || 0));
|
2020-02-05 16:36:36 -08:00
|
|
|
else if (Array.isArray(ignoreDefaultArgs))
|
2020-02-27 14:09:24 -08:00
|
|
|
firefoxArguments.push(...this._defaultArgs(options, launchType, userDataDir!, port || 0).filter(arg => !ignoreDefaultArgs.includes(arg)));
|
2020-02-05 16:36:36 -08:00
|
|
|
else
|
|
|
|
firefoxArguments.push(...args);
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-03-19 11:43:35 -07:00
|
|
|
const firefoxExecutable = executablePath || this._executablePath;
|
|
|
|
if (!firefoxExecutable)
|
|
|
|
throw new Error(`No executable path is specified. Pass "executablePath" option directly.`);
|
2020-01-08 13:55:38 -08:00
|
|
|
|
2020-02-05 12:41:55 -08:00
|
|
|
let browserServer: BrowserServer | undefined = undefined;
|
2020-01-08 13:55:38 -08:00
|
|
|
const { launchedProcess, gracefullyClose } = await launchProcess({
|
2019-12-19 16:53:24 -08:00
|
|
|
executablePath: firefoxExecutable,
|
|
|
|
args: firefoxArguments,
|
|
|
|
env: os.platform() === 'linux' ? {
|
|
|
|
...env,
|
|
|
|
// On linux Juggler ships the libstdc++ it was linked against.
|
|
|
|
LD_LIBRARY_PATH: `${path.dirname(firefoxExecutable)}:${process.env.LD_LIBRARY_PATH}`,
|
|
|
|
} : env,
|
|
|
|
handleSIGINT,
|
|
|
|
handleSIGTERM,
|
|
|
|
handleSIGHUP,
|
|
|
|
dumpio,
|
|
|
|
pipe: false,
|
2020-01-13 13:33:25 -08:00
|
|
|
tempDir: temporaryProfileDir || undefined,
|
2020-01-08 13:55:38 -08:00
|
|
|
attemptToGracefullyClose: async () => {
|
2020-02-05 12:41:55 -08:00
|
|
|
if (!browserServer)
|
2020-01-08 13:55:38 -08:00
|
|
|
return Promise.reject();
|
|
|
|
// We try to gracefully close to prevent crash reporting and core dumps.
|
2020-01-23 17:45:31 -08:00
|
|
|
// Note that it's fine to reuse the pipe transport, since
|
|
|
|
// our connection ignores kBrowserCloseMessageId.
|
2020-03-02 13:51:32 -08:00
|
|
|
const transport = await platform.connectToWebsocket(browserWSEndpoint, async transport => transport);
|
2020-01-23 17:45:31 -08:00
|
|
|
const message = { method: 'Browser.close', params: {}, id: kBrowserCloseMessageId };
|
2020-03-26 23:30:55 -07:00
|
|
|
await transport.send(message);
|
2020-01-08 13:55:38 -08:00
|
|
|
},
|
2020-01-28 13:07:53 -08:00
|
|
|
onkill: (exitCode, signal) => {
|
2020-02-05 12:41:55 -08:00
|
|
|
if (browserServer)
|
|
|
|
browserServer.emit(Events.BrowserServer.Close, exitCode, signal);
|
2020-01-24 15:58:04 -08:00
|
|
|
},
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
|
|
|
|
2020-01-08 13:55:38 -08:00
|
|
|
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);
|
2020-01-23 17:45:31 -08:00
|
|
|
const browserWSEndpoint = match[1];
|
2020-03-02 13:51:32 -08:00
|
|
|
browserServer = new BrowserServer(launchedProcess, gracefullyClose, browserWSEndpoint);
|
|
|
|
return browserServer;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-02-04 19:41:38 -08:00
|
|
|
async connect(options: ConnectOptions): Promise<FFBrowser> {
|
2020-03-02 13:51:32 -08:00
|
|
|
return await platform.connectToWebsocket(options.wsEndpoint, transport => {
|
2020-03-06 16:49:48 -08:00
|
|
|
return FFBrowser.connect(transport, false, options.slowMo);
|
2020-03-02 13:51:32 -08:00
|
|
|
});
|
2020-01-07 16:13:49 -08:00
|
|
|
}
|
|
|
|
|
2020-02-27 14:09:24 -08:00
|
|
|
private _defaultArgs(options: BrowserArgOptions = {}, launchType: LaunchType, userDataDir: string, port: number): string[] {
|
2020-01-07 16:13:49 -08:00
|
|
|
const {
|
2020-01-24 14:49:47 -08:00
|
|
|
devtools = false,
|
|
|
|
headless = !devtools,
|
2020-01-07 16:13:49 -08:00
|
|
|
args = [],
|
|
|
|
} = options;
|
2020-01-24 14:49:47 -08:00
|
|
|
if (devtools)
|
2020-03-24 22:42:20 +01:00
|
|
|
console.warn('devtools parameter is not supported as a launch argument in Firefox. You can launch the devtools window manually.');
|
2020-02-05 16:36:36 -08:00
|
|
|
const userDataDirArg = args.find(arg => arg.startsWith('-profile') || arg.startsWith('--profile'));
|
|
|
|
if (userDataDirArg)
|
|
|
|
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');
|
2020-02-27 14:09:24 -08:00
|
|
|
if (launchType !== 'persistent' && args.find(arg => !arg.startsWith('-')))
|
|
|
|
throw new Error('Arguments can not specify page to be opened');
|
2020-02-05 16:36:36 -08:00
|
|
|
|
|
|
|
const firefoxArguments = ['-no-remote'];
|
2020-02-10 20:35:58 -08:00
|
|
|
if (headless) {
|
2020-01-07 16:13:49 -08:00
|
|
|
firefoxArguments.push('-headless');
|
2020-02-10 20:35:58 -08:00
|
|
|
} else {
|
2020-01-21 17:22:48 -08:00
|
|
|
firefoxArguments.push('-wait-for-browser');
|
2020-02-10 20:35:58 -08:00
|
|
|
firefoxArguments.push('-foreground');
|
|
|
|
}
|
2020-02-05 16:36:36 -08:00
|
|
|
|
|
|
|
firefoxArguments.push(`-profile`, userDataDir);
|
|
|
|
firefoxArguments.push('-juggler', String(port));
|
2020-01-07 16:13:49 -08:00
|
|
|
firefoxArguments.push(...args);
|
2020-02-05 16:36:36 -08:00
|
|
|
|
2020-01-07 16:13:49 -08:00
|
|
|
if (args.every(arg => arg.startsWith('-')))
|
|
|
|
firefoxArguments.push('about:blank');
|
|
|
|
return firefoxArguments;
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|