2020-01-07 16:15:07 -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-09 14:49:22 -08:00
|
|
|
import { BrowserFetcher, BrowserFetcherOptions } from './browserFetcher';
|
2020-01-07 16:15:07 -08:00
|
|
|
import { DeviceDescriptors } from '../deviceDescriptors';
|
2020-01-09 14:49:22 -08:00
|
|
|
import { TimeoutError } from '../errors';
|
2020-01-07 16:15:07 -08:00
|
|
|
import * as types from '../types';
|
2020-01-08 13:55:38 -08:00
|
|
|
import { WKBrowser, createTransport } from '../webkit/wkBrowser';
|
|
|
|
import { WKConnectOptions } from '../webkit/wkBrowser';
|
2020-01-07 16:15:07 -08:00
|
|
|
import { execSync, ChildProcess } from 'child_process';
|
|
|
|
import { PipeTransport } from './pipeTransport';
|
|
|
|
import { launchProcess } from './processLauncher';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as util from 'util';
|
|
|
|
import * as os from 'os';
|
|
|
|
import { assert } from '../helper';
|
2020-01-08 13:55:38 -08:00
|
|
|
import { kBrowserCloseMessageId } from '../webkit/wkConnection';
|
2020-01-09 14:49:22 -08:00
|
|
|
import { Playwright } from './playwright';
|
2020-01-07 16:15:07 -08:00
|
|
|
|
|
|
|
export type LaunchOptions = {
|
2020-01-09 14:49:22 -08:00
|
|
|
ignoreDefaultArgs?: boolean | string[],
|
2020-01-07 16:15:07 -08:00
|
|
|
args?: string[],
|
|
|
|
executablePath?: string,
|
|
|
|
handleSIGINT?: boolean,
|
|
|
|
handleSIGTERM?: boolean,
|
|
|
|
handleSIGHUP?: boolean,
|
|
|
|
headless?: boolean,
|
|
|
|
timeout?: number,
|
|
|
|
dumpio?: boolean,
|
|
|
|
env?: {[key: string]: string} | undefined,
|
|
|
|
slowMo?: number,
|
|
|
|
};
|
|
|
|
|
|
|
|
export class WKBrowserServer {
|
|
|
|
private _process: ChildProcess;
|
2020-01-08 13:55:38 -08:00
|
|
|
private _gracefullyClose: () => Promise<void>;
|
2020-01-07 16:15:07 -08:00
|
|
|
private _connectOptions: WKConnectOptions;
|
|
|
|
|
2020-01-08 13:55:38 -08:00
|
|
|
constructor(process: ChildProcess, gracefullyClose: () => Promise<void>, connectOptions: WKConnectOptions) {
|
2020-01-07 16:15:07 -08:00
|
|
|
this._process = process;
|
2020-01-08 13:55:38 -08:00
|
|
|
this._gracefullyClose = gracefullyClose;
|
2020-01-07 16:15:07 -08:00
|
|
|
this._connectOptions = connectOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
async connect(): Promise<WKBrowser> {
|
2020-01-16 09:32:58 -08:00
|
|
|
const browser = await WKBrowser.connect(this._connectOptions);
|
|
|
|
// Hack: for typical launch scenario, ensure that close waits for actual process termination.
|
|
|
|
browser.close = this._gracefullyClose;
|
|
|
|
return browser;
|
2020-01-07 16:15:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
process(): ChildProcess {
|
|
|
|
return this._process;
|
|
|
|
}
|
|
|
|
|
|
|
|
connectOptions(): WKConnectOptions {
|
|
|
|
return this._connectOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
async close(): Promise<void> {
|
2020-01-08 13:55:38 -08:00
|
|
|
await this._gracefullyClose();
|
2020-01-07 16:15:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:49:22 -08:00
|
|
|
export class WKPlaywright implements Playwright {
|
2020-01-07 16:15:07 -08:00
|
|
|
private _projectRoot: string;
|
|
|
|
readonly _revision: string;
|
|
|
|
|
|
|
|
constructor(projectRoot: string, preferredRevision: string) {
|
|
|
|
this._projectRoot = projectRoot;
|
|
|
|
this._revision = preferredRevision;
|
|
|
|
}
|
|
|
|
|
|
|
|
async launch(options?: LaunchOptions): Promise<WKBrowser> {
|
|
|
|
const server = await this.launchServer(options);
|
|
|
|
return server.connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
async launchServer(options: LaunchOptions = {}): Promise<WKBrowserServer> {
|
|
|
|
const {
|
|
|
|
ignoreDefaultArgs = false,
|
|
|
|
args = [],
|
|
|
|
dumpio = false,
|
|
|
|
executablePath = null,
|
|
|
|
env = process.env,
|
|
|
|
handleSIGINT = true,
|
|
|
|
handleSIGTERM = true,
|
|
|
|
handleSIGHUP = true,
|
|
|
|
slowMo = 0,
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
const webkitArguments = [];
|
|
|
|
if (!ignoreDefaultArgs)
|
|
|
|
webkitArguments.push(...this.defaultArgs(options));
|
2020-01-09 14:49:22 -08:00
|
|
|
else if (Array.isArray(ignoreDefaultArgs))
|
|
|
|
webkitArguments.push(...this.defaultArgs(options).filter(arg => ignoreDefaultArgs.indexOf(arg) === -1));
|
2020-01-07 16:15:07 -08:00
|
|
|
else
|
|
|
|
webkitArguments.push(...args);
|
|
|
|
|
|
|
|
let webkitExecutable = executablePath;
|
|
|
|
if (!executablePath) {
|
|
|
|
const {missingText, executablePath} = this._resolveExecutablePath();
|
|
|
|
if (missingText)
|
|
|
|
throw new Error(missingText);
|
|
|
|
webkitExecutable = executablePath;
|
|
|
|
}
|
|
|
|
webkitArguments.push('--inspector-pipe');
|
2020-01-08 15:34:35 -08:00
|
|
|
if (options.headless !== false)
|
2020-01-07 16:15:07 -08:00
|
|
|
webkitArguments.push('--headless');
|
|
|
|
|
2020-01-08 13:55:38 -08:00
|
|
|
let connectOptions: WKConnectOptions | undefined = undefined;
|
|
|
|
|
|
|
|
const { launchedProcess, gracefullyClose } = await launchProcess({
|
2020-01-13 13:33:25 -08:00
|
|
|
executablePath: webkitExecutable!,
|
2020-01-07 16:15:07 -08:00
|
|
|
args: webkitArguments,
|
|
|
|
env,
|
|
|
|
handleSIGINT,
|
|
|
|
handleSIGTERM,
|
|
|
|
handleSIGHUP,
|
|
|
|
dumpio,
|
|
|
|
pipe: true,
|
2020-01-08 13:55:38 -08:00
|
|
|
attemptToGracefullyClose: async () => {
|
|
|
|
if (!connectOptions)
|
|
|
|
return Promise.reject();
|
|
|
|
// We try to gracefully close to prevent crash reporting and core dumps.
|
|
|
|
const transport = await createTransport(connectOptions);
|
|
|
|
const message = JSON.stringify({method: 'Browser.close', params: {}, id: kBrowserCloseMessageId});
|
|
|
|
transport.send(message);
|
|
|
|
},
|
2020-01-07 16:15:07 -08:00
|
|
|
});
|
|
|
|
|
2020-01-08 13:55:38 -08:00
|
|
|
const transport = new PipeTransport(launchedProcess.stdio[3] as NodeJS.WritableStream, launchedProcess.stdio[4] as NodeJS.ReadableStream);
|
|
|
|
connectOptions = { transport, slowMo };
|
|
|
|
return new WKBrowserServer(launchedProcess, gracefullyClose, connectOptions);
|
2020-01-07 16:15:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
executablePath(): string {
|
|
|
|
return this._resolveExecutablePath().executablePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
get devices(): types.Devices {
|
|
|
|
return DeviceDescriptors;
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:49:22 -08:00
|
|
|
get errors(): { TimeoutError: typeof TimeoutError } {
|
|
|
|
return { TimeoutError };
|
2020-01-07 16:15:07 -08:00
|
|
|
}
|
|
|
|
|
2020-01-09 14:49:22 -08:00
|
|
|
defaultArgs(options: { args?: string[] } = {}): string[] {
|
2020-01-07 16:15:07 -08:00
|
|
|
const {
|
|
|
|
args = [],
|
|
|
|
} = options;
|
|
|
|
const webkitArguments = [...DEFAULT_ARGS];
|
|
|
|
webkitArguments.push(...args);
|
|
|
|
return webkitArguments;
|
|
|
|
}
|
|
|
|
|
2020-01-14 10:07:26 -08:00
|
|
|
_createBrowserFetcher(options?: BrowserFetcherOptions): BrowserFetcher {
|
2020-01-07 16:15:07 -08:00
|
|
|
const downloadURLs = {
|
|
|
|
linux: '%s/builds/webkit/%s/minibrowser-linux.zip',
|
|
|
|
mac: '%s/builds/webkit/%s/minibrowser-mac-%s.zip',
|
2020-01-16 19:43:39 -08:00
|
|
|
win64: '%s/builds/webkit/%s/minibrowser-win64.zip',
|
2020-01-07 16:15:07 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultOptions = {
|
|
|
|
path: path.join(this._projectRoot, '.local-webkit'),
|
2020-01-13 13:27:03 -08:00
|
|
|
host: 'https://playwright.blob.core.windows.net',
|
2020-01-07 16:15:07 -08:00
|
|
|
platform: (() => {
|
|
|
|
const platform = os.platform();
|
|
|
|
if (platform === 'darwin')
|
|
|
|
return 'mac';
|
|
|
|
if (platform === 'linux')
|
|
|
|
return 'linux';
|
|
|
|
if (platform === 'win32')
|
2020-01-16 19:43:39 -08:00
|
|
|
return 'win64';
|
2020-01-07 16:15:07 -08:00
|
|
|
return platform;
|
|
|
|
})()
|
|
|
|
};
|
|
|
|
options = {
|
|
|
|
...defaultOptions,
|
|
|
|
...options,
|
|
|
|
};
|
2020-01-13 13:33:25 -08:00
|
|
|
assert(!!(downloadURLs as any)[options.platform!], 'Unsupported platform: ' + options.platform);
|
2020-01-07 16:15:07 -08:00
|
|
|
|
2020-01-13 13:33:25 -08:00
|
|
|
return new BrowserFetcher(options.path!, options.platform!, this._revision, (platform: string, revision: string) => {
|
2020-01-07 16:15:07 -08:00
|
|
|
return {
|
|
|
|
downloadUrl: (platform === 'mac') ?
|
2020-01-13 13:33:25 -08:00
|
|
|
util.format(downloadURLs[platform], options!.host, revision, getMacVersion()) :
|
|
|
|
util.format((downloadURLs as any)[platform], options!.host, revision),
|
2020-01-16 19:43:39 -08:00
|
|
|
executablePath: platform.startsWith('win') ? 'MiniBrowser.exe' : 'pw_run.sh',
|
2020-01-07 16:15:07 -08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_resolveExecutablePath(): { executablePath: string; missingText: string | null; } {
|
2020-01-14 10:07:26 -08:00
|
|
|
const browserFetcher = this._createBrowserFetcher();
|
2020-01-09 14:49:22 -08:00
|
|
|
const revisionInfo = browserFetcher.revisionInfo();
|
2020-01-07 16:15:07 -08:00
|
|
|
const missingText = !revisionInfo.local ? `WebKit revision is not downloaded. Run "npm install" or "yarn install"` : null;
|
|
|
|
return { executablePath: revisionInfo.executablePath, missingText };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 13:33:25 -08:00
|
|
|
const DEFAULT_ARGS: string[] = [];
|
2020-01-07 16:15:07 -08:00
|
|
|
|
2020-01-13 13:33:25 -08:00
|
|
|
let cachedMacVersion: string | undefined = undefined;
|
2020-01-07 16:15:07 -08:00
|
|
|
function getMacVersion() {
|
|
|
|
if (!cachedMacVersion) {
|
|
|
|
const [major, minor] = execSync('sw_vers -productVersion').toString('utf8').trim().split('.');
|
|
|
|
cachedMacVersion = major + '.' + minor;
|
|
|
|
}
|
|
|
|
return cachedMacVersion;
|
|
|
|
}
|
|
|
|
|