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-22 17:42:10 -08:00
|
|
|
import { WKBrowser } from '../webkit/wkBrowser';
|
2020-01-23 14:40:37 -08:00
|
|
|
import { execSync } from 'child_process';
|
2020-01-07 16:15:07 -08:00
|
|
|
import { PipeTransport } from './pipeTransport';
|
|
|
|
import { launchProcess } from './processLauncher';
|
2020-01-16 22:11:14 -08:00
|
|
|
import * as fs from 'fs';
|
2020-01-07 16:15:07 -08:00
|
|
|
import * as path from 'path';
|
2020-01-16 22:11:14 -08:00
|
|
|
import * as platform from '../platform';
|
2020-01-07 16:15:07 -08:00
|
|
|
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-24 14:49:47 -08:00
|
|
|
import { LaunchOptions, BrowserArgOptions, BrowserType } from './browserType';
|
2020-01-22 17:42:10 -08:00
|
|
|
import { ConnectionTransport } from '../transport';
|
|
|
|
import * as ws from 'ws';
|
|
|
|
import * as uuidv4 from 'uuid/v4';
|
2020-01-23 14:40:37 -08:00
|
|
|
import { ConnectOptions } from '../browser';
|
|
|
|
import { BrowserApp } from './browserApp';
|
2020-01-24 15:58:04 -08:00
|
|
|
import { Events } from '../events';
|
2020-01-07 16:15:07 -08:00
|
|
|
|
2020-01-24 14:49:47 -08:00
|
|
|
export class WebKit implements BrowserType {
|
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> {
|
2020-01-23 14:40:37 -08:00
|
|
|
const app = await this.launchBrowserApp(options);
|
|
|
|
const browser = await WKBrowser.connect(app.connectOptions());
|
2020-01-23 08:51:43 -08:00
|
|
|
// Hack: for typical launch scenario, ensure that close waits for actual process termination.
|
2020-01-23 14:40:37 -08:00
|
|
|
browser.close = () => app.close();
|
2020-01-23 08:51:43 -08:00
|
|
|
return browser;
|
2020-01-07 16:15:07 -08:00
|
|
|
}
|
|
|
|
|
2020-01-23 14:40:37 -08:00
|
|
|
async launchBrowserApp(options: LaunchOptions = {}): Promise<BrowserApp> {
|
2020-01-07 16:15:07 -08:00
|
|
|
const {
|
|
|
|
ignoreDefaultArgs = false,
|
|
|
|
args = [],
|
|
|
|
dumpio = false,
|
|
|
|
executablePath = null,
|
|
|
|
env = process.env,
|
|
|
|
handleSIGINT = true,
|
|
|
|
handleSIGTERM = true,
|
|
|
|
handleSIGHUP = true,
|
|
|
|
slowMo = 0,
|
2020-01-23 17:45:31 -08:00
|
|
|
webSocket = false,
|
2020-01-07 16:15:07 -08:00
|
|
|
} = 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);
|
|
|
|
|
2020-01-16 22:11:14 -08:00
|
|
|
let userDataDir: string;
|
|
|
|
let temporaryUserDataDir: string | null = null;
|
2020-01-24 17:42:24 -08:00
|
|
|
const userDataDirArg = webkitArguments.find(arg => arg.startsWith('--user-data-dir='));
|
2020-01-16 22:11:14 -08:00
|
|
|
if (userDataDirArg) {
|
2020-01-24 17:42:24 -08:00
|
|
|
userDataDir = userDataDirArg.substr('--user-data-dir='.length).trim();
|
2020-01-16 22:11:14 -08:00
|
|
|
} else {
|
|
|
|
userDataDir = await mkdtempAsync(WEBKIT_PROFILE_PATH);
|
|
|
|
temporaryUserDataDir = userDataDir;
|
|
|
|
webkitArguments.push(`--user-data-dir=${temporaryUserDataDir}`);
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:15:07 -08:00
|
|
|
let webkitExecutable = executablePath;
|
|
|
|
if (!executablePath) {
|
|
|
|
const {missingText, executablePath} = this._resolveExecutablePath();
|
|
|
|
if (missingText)
|
|
|
|
throw new Error(missingText);
|
|
|
|
webkitExecutable = executablePath;
|
|
|
|
}
|
2020-01-08 13:55:38 -08:00
|
|
|
|
2020-01-24 15:58:04 -08:00
|
|
|
let transport: PipeTransport | undefined = undefined;
|
|
|
|
let browserApp: BrowserApp | undefined = undefined;
|
2020-01-08 13:55:38 -08:00
|
|
|
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,
|
2020-01-16 22:11:14 -08:00
|
|
|
env: { ...env, CURL_COOKIE_JAR_PATH: path.join(userDataDir, 'cookiejar.db') },
|
2020-01-07 16:15:07 -08:00
|
|
|
handleSIGINT,
|
|
|
|
handleSIGTERM,
|
|
|
|
handleSIGHUP,
|
|
|
|
dumpio,
|
|
|
|
pipe: true,
|
2020-01-16 22:11:14 -08:00
|
|
|
tempDir: temporaryUserDataDir || undefined,
|
2020-01-08 13:55:38 -08:00
|
|
|
attemptToGracefullyClose: async () => {
|
2020-01-22 17:42:10 -08:00
|
|
|
if (!transport)
|
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-01-08 13:55:38 -08:00
|
|
|
const message = JSON.stringify({method: 'Browser.close', params: {}, id: kBrowserCloseMessageId});
|
|
|
|
transport.send(message);
|
|
|
|
},
|
2020-01-24 15:58:04 -08:00
|
|
|
onkill: () => {
|
|
|
|
if (browserApp)
|
|
|
|
browserApp.emit(Events.BrowserApp.Close);
|
|
|
|
},
|
2020-01-07 16:15:07 -08:00
|
|
|
});
|
|
|
|
|
2020-01-22 17:42:10 -08:00
|
|
|
transport = new PipeTransport(launchedProcess.stdio[3] as NodeJS.WritableStream, launchedProcess.stdio[4] as NodeJS.ReadableStream);
|
|
|
|
|
2020-01-23 14:40:37 -08:00
|
|
|
let connectOptions: ConnectOptions;
|
2020-01-23 17:45:31 -08:00
|
|
|
if (webSocket) {
|
2020-01-22 17:42:10 -08:00
|
|
|
const browserWSEndpoint = wrapTransportWithWebSocket(transport);
|
|
|
|
connectOptions = { browserWSEndpoint, slowMo };
|
|
|
|
} else {
|
|
|
|
connectOptions = { transport, slowMo };
|
|
|
|
}
|
2020-01-24 15:58:04 -08:00
|
|
|
browserApp = new BrowserApp(launchedProcess, gracefullyClose, connectOptions);
|
|
|
|
return browserApp;
|
2020-01-07 16:15:07 -08:00
|
|
|
}
|
|
|
|
|
2020-01-24 14:49:47 -08:00
|
|
|
async connect(options: ConnectOptions & { browserURL?: string }): Promise<WKBrowser> {
|
|
|
|
if (options.browserURL)
|
|
|
|
throw new Error('Option "browserURL" is not supported by Firefox');
|
2020-01-23 17:45:31 -08:00
|
|
|
if (options.transport && options.transport.onmessage)
|
|
|
|
throw new Error('Transport is already in use');
|
2020-01-22 17:42:10 -08:00
|
|
|
return WKBrowser.connect(options);
|
|
|
|
}
|
|
|
|
|
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-24 14:49:47 -08:00
|
|
|
defaultArgs(options: BrowserArgOptions = {}): string[] {
|
2020-01-07 16:15:07 -08:00
|
|
|
const {
|
2020-01-24 14:49:47 -08:00
|
|
|
devtools = false,
|
|
|
|
headless = !devtools,
|
2020-01-07 16:15:07 -08:00
|
|
|
args = [],
|
2020-01-16 22:11:14 -08:00
|
|
|
userDataDir = null
|
2020-01-07 16:15:07 -08:00
|
|
|
} = options;
|
2020-01-24 14:49:47 -08:00
|
|
|
if (devtools)
|
|
|
|
throw new Error('Option "devtools" is not supported by WebKit');
|
2020-01-23 12:18:41 -08:00
|
|
|
const webkitArguments = ['--inspector-pipe'];
|
2020-01-16 22:11:14 -08:00
|
|
|
if (userDataDir)
|
|
|
|
webkitArguments.push(`--user-data-dir=${userDataDir}`);
|
2020-01-23 12:18:41 -08:00
|
|
|
if (headless)
|
|
|
|
webkitArguments.push('--headless');
|
2020-01-07 16:15:07 -08:00
|
|
|
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 = {
|
2020-01-17 16:30:19 -08:00
|
|
|
linux: '%s/builds/webkit/%s/minibrowser-gtk-wpe.zip',
|
2020-01-07 16:15:07 -08:00
|
|
|
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-23 16:00:55 -08:00
|
|
|
host: 'https://playwright.azureedge.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-16 22:11:14 -08:00
|
|
|
const mkdtempAsync = platform.promisify(fs.mkdtemp);
|
|
|
|
|
|
|
|
const WEBKIT_PROFILE_PATH = path.join(os.tmpdir(), 'playwright_dev_profile-');
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-22 17:42:10 -08:00
|
|
|
function wrapTransportWithWebSocket(transport: ConnectionTransport) {
|
|
|
|
const server = new ws.Server({ port: 0 });
|
|
|
|
let socket: ws | undefined;
|
|
|
|
const guid = uuidv4();
|
|
|
|
|
|
|
|
server.on('connection', (s, req) => {
|
|
|
|
if (req.url !== '/' + guid) {
|
|
|
|
s.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (socket) {
|
|
|
|
s.close(undefined, 'Multiple connections are not supported');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
socket = s;
|
|
|
|
s.on('message', message => transport.send(Buffer.from(message).toString()));
|
2020-01-23 10:33:05 -08:00
|
|
|
transport.onmessage = message => {
|
|
|
|
// We are not notified when socket starts closing, and sending messages to a closing
|
|
|
|
// socket throws an error.
|
|
|
|
if (s.readyState !== ws.CLOSING)
|
|
|
|
s.send(message);
|
|
|
|
};
|
2020-01-22 17:42:10 -08:00
|
|
|
s.on('close', () => {
|
|
|
|
socket = undefined;
|
|
|
|
transport.onmessage = undefined;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
transport.onclose = () => {
|
|
|
|
if (socket)
|
|
|
|
socket.close(undefined, 'Browser disconnected');
|
|
|
|
server.close();
|
|
|
|
transport.onmessage = undefined;
|
|
|
|
transport.onclose = undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
const address = server.address();
|
|
|
|
if (typeof address === 'string')
|
|
|
|
return address + '/' + guid;
|
|
|
|
return 'ws://127.0.0.1:' + address.port + '/' + guid;
|
|
|
|
}
|