2019-11-19 03:18:28 +01: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.
|
|
|
|
|
*/
|
2019-12-13 02:52:13 +01:00
|
|
|
|
|
|
|
|
import { debugError, assert } from '../helper';
|
2019-11-19 03:18:28 +01:00
|
|
|
import { Browser } from './Browser';
|
2019-12-08 22:29:03 +01:00
|
|
|
import { BrowserFetcher, BrowserFetcherOptions } from '../browserFetcher';
|
2019-11-19 03:18:28 +01:00
|
|
|
import { Connection } from './Connection';
|
2019-12-06 20:33:24 +01:00
|
|
|
import * as types from '../types';
|
2019-11-19 03:18:28 +01:00
|
|
|
import { PipeTransport } from './PipeTransport';
|
2019-12-08 22:29:03 +01:00
|
|
|
import { execSync } from 'child_process';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as util from 'util';
|
|
|
|
|
import * as os from 'os';
|
2019-12-13 02:52:13 +01:00
|
|
|
import { launchProcess } from '../processLauncher';
|
2019-11-19 03:18:28 +01:00
|
|
|
|
2019-12-03 01:17:53 +01:00
|
|
|
const DEFAULT_ARGS = [
|
|
|
|
|
];
|
2019-11-19 03:18:28 +01:00
|
|
|
|
|
|
|
|
export class Launcher {
|
|
|
|
|
private _projectRoot: string;
|
|
|
|
|
private _preferredRevision: string;
|
|
|
|
|
|
|
|
|
|
constructor(projectRoot: string, preferredRevision: string) {
|
|
|
|
|
this._projectRoot = projectRoot;
|
|
|
|
|
this._preferredRevision = preferredRevision;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 01:17:53 +01:00
|
|
|
defaultArgs(options: any = {}) {
|
|
|
|
|
const {
|
|
|
|
|
args = [],
|
|
|
|
|
} = options;
|
|
|
|
|
const webkitArguments = [...DEFAULT_ARGS];
|
|
|
|
|
webkitArguments.push(...args);
|
|
|
|
|
return webkitArguments;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-19 03:18:28 +01:00
|
|
|
async launch(options: LauncherLaunchOptions = {}): Promise<Browser> {
|
|
|
|
|
const {
|
2019-12-03 01:17:53 +01:00
|
|
|
ignoreDefaultArgs = false,
|
2019-11-19 03:18:28 +01:00
|
|
|
args = [],
|
|
|
|
|
dumpio = false,
|
|
|
|
|
executablePath = null,
|
|
|
|
|
env = process.env,
|
|
|
|
|
handleSIGINT = true,
|
|
|
|
|
handleSIGTERM = true,
|
|
|
|
|
handleSIGHUP = true,
|
|
|
|
|
defaultViewport = {width: 800, height: 600},
|
|
|
|
|
slowMo = 0
|
|
|
|
|
} = options;
|
|
|
|
|
|
2019-12-03 01:17:53 +01:00
|
|
|
const webkitArguments = [];
|
|
|
|
|
if (!ignoreDefaultArgs)
|
|
|
|
|
webkitArguments.push(...this.defaultArgs(options));
|
|
|
|
|
else
|
|
|
|
|
webkitArguments.push(...args);
|
|
|
|
|
|
2019-11-19 03:18:28 +01:00
|
|
|
let webkitExecutable = executablePath;
|
|
|
|
|
if (!executablePath) {
|
|
|
|
|
const {missingText, executablePath} = this._resolveExecutablePath();
|
|
|
|
|
if (missingText)
|
|
|
|
|
throw new Error(missingText);
|
|
|
|
|
webkitExecutable = executablePath;
|
|
|
|
|
}
|
|
|
|
|
webkitArguments.push('--inspector-pipe');
|
2019-12-05 01:08:35 +01:00
|
|
|
// Headless options is only implemented on Mac at the moment.
|
|
|
|
|
if (process.platform === 'darwin' && options.headless !== false)
|
2019-12-04 22:11:10 +01:00
|
|
|
webkitArguments.push('--headless');
|
2019-12-03 01:17:53 +01:00
|
|
|
|
2019-12-13 02:52:13 +01:00
|
|
|
const launched = await launchProcess({
|
|
|
|
|
executablePath: webkitExecutable,
|
|
|
|
|
args: webkitArguments,
|
|
|
|
|
env,
|
|
|
|
|
handleSIGINT,
|
|
|
|
|
handleSIGTERM,
|
|
|
|
|
handleSIGHUP,
|
|
|
|
|
dumpio,
|
|
|
|
|
pipe: true,
|
|
|
|
|
tempDir: null
|
|
|
|
|
}, () => {
|
|
|
|
|
if (!connection)
|
|
|
|
|
return Promise.reject();
|
|
|
|
|
return connection.send('Browser.close').catch(error => {
|
|
|
|
|
debugError(error);
|
|
|
|
|
throw error;
|
2019-11-19 03:18:28 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let connection: Connection | null = null;
|
|
|
|
|
try {
|
2019-12-13 02:52:13 +01:00
|
|
|
const transport = new PipeTransport(launched.process.stdio[3] as NodeJS.WritableStream, launched.process.stdio[4] as NodeJS.ReadableStream);
|
2019-12-10 22:11:55 +01:00
|
|
|
connection = new Connection(transport, slowMo);
|
2019-12-13 02:52:13 +01:00
|
|
|
const browser = new Browser(connection, defaultViewport, launched.process, launched.gracefullyClose);
|
2019-12-09 21:13:19 +01:00
|
|
|
await browser._waitForTarget(t => t._type === 'page');
|
2019-11-19 03:18:28 +01:00
|
|
|
return browser;
|
|
|
|
|
} catch (e) {
|
2019-12-13 02:52:13 +01:00
|
|
|
await launched.gracefullyClose();
|
2019-11-19 03:18:28 +01:00
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
executablePath(): string {
|
|
|
|
|
return this._resolveExecutablePath().executablePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_resolveExecutablePath(): { executablePath: string; missingText: string | null; } {
|
2019-12-08 22:29:03 +01:00
|
|
|
const browserFetcher = createBrowserFetcher(this._projectRoot);
|
2019-11-19 03:18:28 +01:00
|
|
|
const revisionInfo = browserFetcher.revisionInfo(this._preferredRevision);
|
|
|
|
|
const missingText = !revisionInfo.local ? `WebKit revision is not downloaded. Run "npm install" or "yarn install"` : null;
|
|
|
|
|
return {executablePath: revisionInfo.executablePath, missingText};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type LauncherLaunchOptions = {
|
2019-12-03 01:17:53 +01:00
|
|
|
ignoreDefaultArgs?: boolean,
|
2019-11-19 03:18:28 +01:00
|
|
|
args?: string[],
|
|
|
|
|
executablePath?: string,
|
|
|
|
|
handleSIGINT?: boolean,
|
|
|
|
|
handleSIGTERM?: boolean,
|
|
|
|
|
handleSIGHUP?: boolean,
|
|
|
|
|
headless?: boolean,
|
|
|
|
|
dumpio?: boolean,
|
|
|
|
|
env?: {[key: string]: string} | undefined,
|
2019-12-06 20:33:24 +01:00
|
|
|
defaultViewport?: types.Viewport | null,
|
2019-11-19 03:18:28 +01:00
|
|
|
slowMo?: number,
|
|
|
|
|
};
|
2019-12-08 22:29:03 +01:00
|
|
|
|
|
|
|
|
let cachedMacVersion = undefined;
|
|
|
|
|
function getMacVersion() {
|
|
|
|
|
if (!cachedMacVersion) {
|
|
|
|
|
const [major, minor] = execSync('sw_vers -productVersion').toString('utf8').trim().split('.');
|
|
|
|
|
cachedMacVersion = major + '.' + minor;
|
|
|
|
|
}
|
|
|
|
|
return cachedMacVersion;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createBrowserFetcher(projectRoot: string, options: BrowserFetcherOptions = {}): BrowserFetcher {
|
|
|
|
|
const downloadURLs = {
|
|
|
|
|
linux: '%s/builds/webkit/%s/minibrowser-linux.zip',
|
|
|
|
|
mac: '%s/builds/webkit/%s/minibrowser-mac-%s.zip',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const defaultOptions = {
|
|
|
|
|
path: path.join(projectRoot, '.local-webkit'),
|
|
|
|
|
host: 'https://playwrightaccount.blob.core.windows.net',
|
|
|
|
|
platform: (() => {
|
|
|
|
|
const platform = os.platform();
|
|
|
|
|
if (platform === 'darwin')
|
|
|
|
|
return 'mac';
|
|
|
|
|
if (platform === 'linux')
|
|
|
|
|
return 'linux';
|
|
|
|
|
if (platform === 'win32')
|
|
|
|
|
return 'linux'; // Windows gets linux binaries and uses WSL
|
|
|
|
|
return platform;
|
|
|
|
|
})()
|
|
|
|
|
};
|
|
|
|
|
options = {
|
|
|
|
|
...defaultOptions,
|
|
|
|
|
...options,
|
|
|
|
|
};
|
|
|
|
|
assert(!!downloadURLs[options.platform], 'Unsupported platform: ' + options.platform);
|
|
|
|
|
|
|
|
|
|
return new BrowserFetcher(options.path, options.platform, (platform: string, revision: string) => {
|
|
|
|
|
return {
|
|
|
|
|
downloadUrl: (platform === 'mac') ?
|
|
|
|
|
util.format(downloadURLs[platform], options.host, revision, getMacVersion()) :
|
|
|
|
|
util.format(downloadURLs[platform], options.host, revision),
|
|
|
|
|
executablePath: 'pw_run.sh',
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|