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-22 17:42:10 -08:00
|
|
|
import { WKBrowser } from '../webkit/wkBrowser';
|
2020-01-07 16:15:07 -08:00
|
|
|
import { PipeTransport } from './pipeTransport';
|
2020-02-26 13:02:15 -08:00
|
|
|
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 os from 'os';
|
2020-03-19 11:43:35 -07:00
|
|
|
import { helper } 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-02-26 13:02:15 -08:00
|
|
|
import { ConnectionTransport } from '../transport';
|
2020-01-22 17:42:10 -08:00
|
|
|
import * as ws from 'ws';
|
2020-02-05 12:41:55 -08:00
|
|
|
import { ConnectOptions, LaunchType } from '../browser';
|
|
|
|
import { BrowserServer } from './browserServer';
|
2020-01-24 15:58:04 -08:00
|
|
|
import { Events } from '../events';
|
2020-02-05 12:41:55 -08:00
|
|
|
import { BrowserContext } from '../browserContext';
|
2020-01-07 16:15:07 -08:00
|
|
|
|
2020-01-24 14:49:47 -08:00
|
|
|
export class WebKit implements BrowserType {
|
2020-03-19 11:43:35 -07:00
|
|
|
private _executablePath: (string|undefined);
|
|
|
|
|
|
|
|
executablePath(): string {
|
|
|
|
if (!this._executablePath)
|
|
|
|
throw new Error('No executable path!');
|
|
|
|
return this._executablePath;
|
2020-01-07 16:15:07 -08:00
|
|
|
}
|
|
|
|
|
2020-01-28 18:09:07 -08:00
|
|
|
name() {
|
|
|
|
return 'webkit';
|
|
|
|
}
|
|
|
|
|
2020-02-04 19:41:38 -08:00
|
|
|
async launch(options?: LaunchOptions & { slowMo?: number }): Promise<WKBrowser> {
|
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-02-05 16:36:36 -08:00
|
|
|
const { browserServer, transport } = await this._launchServer(options, 'local');
|
2020-02-04 19:41:38 -08:00
|
|
|
const browser = await WKBrowser.connect(transport!, options && options.slowMo);
|
2020-02-05 12:41:55 -08:00
|
|
|
(browser as any)['__server__'] = browserServer;
|
2020-01-23 08:51:43 -08:00
|
|
|
return browser;
|
2020-01-07 16:15:07 -08:00
|
|
|
}
|
|
|
|
|
2020-02-05 12:41:55 -08:00
|
|
|
async launchServer(options?: LaunchOptions & { port?: number }): Promise<BrowserServer> {
|
|
|
|
return (await this._launchServer(options, 'server', undefined, options && options.port)).browserServer;
|
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-09 16:53:33 -07:00
|
|
|
const { transport } = await this._launchServer(options, 'persistent', userDataDir);
|
2020-02-27 08:49:09 -08:00
|
|
|
const browser = await WKBrowser.connect(transport!, undefined, true);
|
2020-02-13 13:54:01 -08:00
|
|
|
await helper.waitWithTimeout(browser._waitForFirstPageTarget(), 'first page', timeout);
|
2020-03-09 16:53:33 -07:00
|
|
|
return browser._defaultContext;
|
2020-02-05 12:41:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private async _launchServer(options: LaunchOptions = {}, launchType: LaunchType, userDataDir?: string, port?: number): Promise<{ browserServer: BrowserServer, transport?: ConnectionTransport }> {
|
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,
|
|
|
|
} = options;
|
|
|
|
|
2020-02-05 12:41:55 -08:00
|
|
|
let temporaryUserDataDir: string | null = null;
|
|
|
|
if (!userDataDir) {
|
2020-01-16 22:11:14 -08:00
|
|
|
userDataDir = await mkdtempAsync(WEBKIT_PROFILE_PATH);
|
2020-02-05 12:41:55 -08:00
|
|
|
temporaryUserDataDir = userDataDir!;
|
2020-01-16 22:11:14 -08:00
|
|
|
}
|
2020-02-05 16:36:36 -08:00
|
|
|
|
|
|
|
const webkitArguments = [];
|
|
|
|
if (!ignoreDefaultArgs)
|
2020-02-26 13:02:15 -08:00
|
|
|
webkitArguments.push(...this._defaultArgs(options, launchType, userDataDir!, port || 0));
|
2020-02-05 16:36:36 -08:00
|
|
|
else if (Array.isArray(ignoreDefaultArgs))
|
2020-02-26 13:02:15 -08:00
|
|
|
webkitArguments.push(...this._defaultArgs(options, launchType, userDataDir!, port || 0).filter(arg => ignoreDefaultArgs.indexOf(arg) === -1));
|
2020-02-05 16:36:36 -08:00
|
|
|
else
|
|
|
|
webkitArguments.push(...args);
|
2020-01-16 22:11:14 -08:00
|
|
|
|
2020-03-19 11:43:35 -07:00
|
|
|
const webkitExecutable = executablePath || this._executablePath;
|
|
|
|
if (!webkitExecutable)
|
|
|
|
throw new Error(`No executable path is specified.`);
|
2020-01-08 13:55:38 -08:00
|
|
|
|
2020-02-13 17:46:40 -08:00
|
|
|
let transport: ConnectionTransport | undefined = undefined;
|
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({
|
2020-03-19 11:43:35 -07:00
|
|
|
executablePath: webkitExecutable,
|
2020-01-07 16:15:07 -08:00
|
|
|
args: webkitArguments,
|
2020-02-05 12:41:55 -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-03-11 21:08:22 +00:00
|
|
|
const message = JSON.stringify({method: 'Playwright.close', params: {}, id: kBrowserCloseMessageId});
|
2020-01-08 13:55:38 -08:00
|
|
|
transport.send(message);
|
|
|
|
},
|
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
|
|
|
},
|
2020-01-07 16:15:07 -08:00
|
|
|
});
|
|
|
|
|
2020-03-09 16:53:33 -07:00
|
|
|
// For local launch scenario close will terminate the browser process.
|
|
|
|
transport = new PipeTransport(launchedProcess.stdio[3] as NodeJS.WritableStream, launchedProcess.stdio[4] as NodeJS.ReadableStream, () => browserServer!.close());
|
2020-02-07 17:39:32 -08:00
|
|
|
browserServer = new BrowserServer(launchedProcess, gracefullyClose, launchType === 'server' ? await wrapTransportWithWebSocket(transport, port || 0) : null);
|
2020-03-09 16:53:33 -07:00
|
|
|
if (launchType === 'server')
|
|
|
|
return { browserServer };
|
2020-02-05 12:41:55 -08:00
|
|
|
return { browserServer, transport };
|
2020-01-07 16:15:07 -08:00
|
|
|
}
|
|
|
|
|
2020-02-04 19:41:38 -08:00
|
|
|
async connect(options: ConnectOptions): Promise<WKBrowser> {
|
2020-03-02 13:51:32 -08:00
|
|
|
return await platform.connectToWebsocket(options.wsEndpoint, transport => {
|
|
|
|
return WKBrowser.connect(transport, options.slowMo);
|
|
|
|
});
|
2020-01-22 17:42:10 -08:00
|
|
|
}
|
|
|
|
|
2020-02-26 13:02:15 -08:00
|
|
|
_defaultArgs(options: BrowserArgOptions = {}, launchType: LaunchType, userDataDir: string, port: number): 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 = [],
|
|
|
|
} = options;
|
2020-01-24 14:49:47 -08:00
|
|
|
if (devtools)
|
|
|
|
throw new Error('Option "devtools" is not supported by WebKit');
|
2020-02-05 16:36:36 -08:00
|
|
|
const userDataDirArg = args.find(arg => arg.startsWith('--user-data-dir='));
|
|
|
|
if (userDataDirArg)
|
|
|
|
throw new Error('Pass userDataDir parameter instead of specifying --user-data-dir 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-01-23 12:18:41 -08:00
|
|
|
const webkitArguments = ['--inspector-pipe'];
|
|
|
|
if (headless)
|
|
|
|
webkitArguments.push('--headless');
|
2020-02-26 13:02:15 -08:00
|
|
|
if (launchType === 'persistent')
|
|
|
|
webkitArguments.push(`--user-data-dir=${userDataDir}`);
|
|
|
|
else
|
|
|
|
webkitArguments.push(`--no-startup-window`);
|
2020-01-07 16:15:07 -08:00
|
|
|
webkitArguments.push(...args);
|
|
|
|
return webkitArguments;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-06 12:41:43 -08:00
|
|
|
class SequenceNumberMixer<V> {
|
|
|
|
static _lastSequenceNumber = 1;
|
|
|
|
private _values = new Map<number, V>();
|
|
|
|
|
|
|
|
generate(value: V): number {
|
|
|
|
const sequenceNumber = ++SequenceNumberMixer._lastSequenceNumber;
|
|
|
|
this._values.set(sequenceNumber, value);
|
|
|
|
return sequenceNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
take(sequenceNumber: number): V | undefined {
|
|
|
|
const value = this._values.get(sequenceNumber);
|
|
|
|
this._values.delete(sequenceNumber);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 13:51:32 -08:00
|
|
|
function wrapTransportWithWebSocket(transport: ConnectionTransport, port: number) {
|
2020-02-05 12:41:55 -08:00
|
|
|
const server = new ws.Server({ port });
|
2020-03-18 10:41:46 -07:00
|
|
|
const guid = platform.guid();
|
2020-02-06 12:41:43 -08:00
|
|
|
const idMixer = new SequenceNumberMixer<{id: number, socket: ws}>();
|
|
|
|
const pendingBrowserContextCreations = new Set<number>();
|
|
|
|
const pendingBrowserContextDeletions = new Map<number, string>();
|
|
|
|
const browserContextIds = new Map<string, ws>();
|
|
|
|
const pageProxyIds = new Map<string, ws>();
|
|
|
|
const sockets = new Set<ws>();
|
2020-01-22 17:42:10 -08:00
|
|
|
|
2020-02-06 12:41:43 -08:00
|
|
|
transport.onmessage = message => {
|
|
|
|
const parsedMessage = JSON.parse(message);
|
|
|
|
if ('id' in parsedMessage) {
|
|
|
|
if (parsedMessage.id === -9999)
|
|
|
|
return;
|
|
|
|
// Process command response.
|
|
|
|
const value = idMixer.take(parsedMessage.id);
|
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
const { id, socket } = value;
|
|
|
|
|
|
|
|
if (!socket || socket.readyState === ws.CLOSING) {
|
|
|
|
if (pendingBrowserContextCreations.has(id)) {
|
|
|
|
transport.send(JSON.stringify({
|
|
|
|
id: ++SequenceNumberMixer._lastSequenceNumber,
|
2020-03-11 21:08:22 +00:00
|
|
|
method: 'Playwright.deleteContext',
|
2020-02-06 12:41:43 -08:00
|
|
|
params: { browserContextId: parsedMessage.result.browserContextId }
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pendingBrowserContextCreations.has(parsedMessage.id)) {
|
|
|
|
// Browser.createContext response -> establish context attribution.
|
|
|
|
browserContextIds.set(parsedMessage.result.browserContextId, socket);
|
|
|
|
pendingBrowserContextCreations.delete(parsedMessage.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
const deletedContextId = pendingBrowserContextDeletions.get(parsedMessage.id);
|
|
|
|
if (deletedContextId) {
|
|
|
|
// Browser.deleteContext response -> remove context attribution.
|
|
|
|
browserContextIds.delete(deletedContextId);
|
|
|
|
pendingBrowserContextDeletions.delete(parsedMessage.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedMessage.id = id;
|
|
|
|
socket.send(JSON.stringify(parsedMessage));
|
2020-01-22 17:42:10 -08:00
|
|
|
return;
|
|
|
|
}
|
2020-02-06 12:41:43 -08:00
|
|
|
|
|
|
|
// Process notification response.
|
|
|
|
const { method, params, pageProxyId } = parsedMessage;
|
|
|
|
if (pageProxyId) {
|
|
|
|
const socket = pageProxyIds.get(pageProxyId);
|
|
|
|
if (!socket || socket.readyState === ws.CLOSING) {
|
|
|
|
// Drop unattributed messages on the floor.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
socket.send(message);
|
2020-01-22 17:42:10 -08:00
|
|
|
return;
|
|
|
|
}
|
2020-03-11 21:08:22 +00:00
|
|
|
if (method === 'Playwright.pageProxyCreated') {
|
2020-02-06 12:41:43 -08:00
|
|
|
const socket = browserContextIds.get(params.pageProxyInfo.browserContextId);
|
|
|
|
if (!socket || socket.readyState === ws.CLOSING) {
|
|
|
|
// Drop unattributed messages on the floor.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pageProxyIds.set(params.pageProxyInfo.pageProxyId, socket);
|
|
|
|
socket.send(message);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-11 21:08:22 +00:00
|
|
|
if (method === 'Playwright.pageProxyDestroyed') {
|
2020-02-06 12:41:43 -08:00
|
|
|
const socket = pageProxyIds.get(params.pageProxyId);
|
|
|
|
pageProxyIds.delete(params.pageProxyId);
|
|
|
|
if (socket && socket.readyState !== ws.CLOSING)
|
|
|
|
socket.send(message);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-11 21:08:22 +00:00
|
|
|
if (method === 'Playwright.provisionalLoadFailed') {
|
2020-02-06 12:41:43 -08:00
|
|
|
const socket = pageProxyIds.get(params.pageProxyId);
|
|
|
|
if (socket && socket.readyState !== ws.CLOSING)
|
2020-02-07 13:38:50 -08:00
|
|
|
socket.send(message);
|
2020-02-06 12:41:43 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
server.on('connection', (socket: ws, req) => {
|
|
|
|
if (req.url !== '/' + guid) {
|
|
|
|
socket.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sockets.add(socket);
|
|
|
|
|
|
|
|
socket.on('message', (message: string) => {
|
|
|
|
const parsedMessage = JSON.parse(Buffer.from(message).toString());
|
|
|
|
const { id, method, params } = parsedMessage;
|
|
|
|
const seqNum = idMixer.generate({ id, socket });
|
|
|
|
transport.send(JSON.stringify({ ...parsedMessage, id: seqNum }));
|
2020-03-11 21:08:22 +00:00
|
|
|
if (method === 'Playwright.createContext')
|
2020-02-06 12:41:43 -08:00
|
|
|
pendingBrowserContextCreations.add(seqNum);
|
2020-03-11 21:08:22 +00:00
|
|
|
if (method === 'Playwright.deleteContext')
|
2020-02-06 12:41:43 -08:00
|
|
|
pendingBrowserContextDeletions.set(seqNum, params.browserContextId);
|
|
|
|
});
|
|
|
|
|
2020-03-09 16:53:33 -07:00
|
|
|
socket.on('close', (socket as any).__closeListener = () => {
|
2020-02-06 12:41:43 -08:00
|
|
|
for (const [pageProxyId, s] of pageProxyIds) {
|
|
|
|
if (s === socket)
|
|
|
|
pageProxyIds.delete(pageProxyId);
|
|
|
|
}
|
|
|
|
for (const [browserContextId, s] of browserContextIds) {
|
|
|
|
if (s === socket) {
|
|
|
|
transport.send(JSON.stringify({
|
|
|
|
id: ++SequenceNumberMixer._lastSequenceNumber,
|
2020-03-11 21:08:22 +00:00
|
|
|
method: 'Playwright.deleteContext',
|
2020-02-06 12:41:43 -08:00
|
|
|
params: { browserContextId }
|
|
|
|
}));
|
|
|
|
browserContextIds.delete(browserContextId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sockets.delete(socket);
|
2020-01-22 17:42:10 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
transport.onclose = () => {
|
2020-03-09 16:53:33 -07:00
|
|
|
for (const socket of sockets) {
|
|
|
|
socket.removeListener('close', (socket as any).__closeListener);
|
2020-01-22 17:42:10 -08:00
|
|
|
socket.close(undefined, 'Browser disconnected');
|
2020-03-09 16:53:33 -07:00
|
|
|
}
|
2020-01-22 17:42:10 -08:00
|
|
|
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;
|
|
|
|
}
|