2020-01-06 18:22:35 -08:00
|
|
|
/**
|
|
|
|
* 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-19 14:51:49 -08:00
|
|
|
|
2020-06-25 08:30:56 -07:00
|
|
|
import * as types from './types';
|
2021-03-31 10:38:05 -07:00
|
|
|
import { BrowserContext } from './browserContext';
|
2020-02-05 12:41:55 -08:00
|
|
|
import { Page } from './page';
|
2020-04-02 17:56:14 -07:00
|
|
|
import { Download } from './download';
|
2020-06-05 13:50:15 -07:00
|
|
|
import { ProxySettings } from './types';
|
2020-08-14 13:19:12 -07:00
|
|
|
import { ChildProcess } from 'child_process';
|
2020-12-08 09:35:28 -08:00
|
|
|
import { RecentLogsCollector } from '../utils/debugLogger';
|
2021-02-09 14:44:48 -08:00
|
|
|
import { SdkObject } from './instrumentation';
|
2021-03-31 10:38:05 -07:00
|
|
|
import { Artifact } from './artifact';
|
2021-06-15 14:56:29 -07:00
|
|
|
import { Selectors } from './selectors';
|
2020-08-14 13:19:12 -07:00
|
|
|
|
|
|
|
export interface BrowserProcess {
|
2021-02-10 14:00:02 -08:00
|
|
|
onclose?: ((exitCode: number | null, signal: string | null) => void);
|
2020-11-06 16:31:11 -08:00
|
|
|
process?: ChildProcess;
|
2020-08-14 13:19:12 -07:00
|
|
|
kill(): Promise<void>;
|
|
|
|
close(): Promise<void>;
|
|
|
|
}
|
2020-05-14 13:22:33 -07:00
|
|
|
|
2021-01-29 16:00:56 -08:00
|
|
|
export type PlaywrightOptions = {
|
2021-02-09 09:00:00 -08:00
|
|
|
rootSdkObject: SdkObject,
|
2021-06-15 14:56:29 -07:00
|
|
|
selectors: Selectors,
|
2021-01-29 16:00:56 -08:00
|
|
|
};
|
|
|
|
|
2021-02-11 17:46:54 -08:00
|
|
|
export type BrowserOptions = PlaywrightOptions & {
|
2020-07-08 21:36:03 -07:00
|
|
|
name: string,
|
2021-01-13 12:08:14 -08:00
|
|
|
isChromium: boolean,
|
2021-05-25 14:33:27 -07:00
|
|
|
channel?: string,
|
2021-06-02 22:00:34 -07:00
|
|
|
artifactsDir: string;
|
|
|
|
downloadsPath: string,
|
|
|
|
tracesDir: string,
|
2020-05-14 13:22:33 -07:00
|
|
|
headful?: boolean,
|
2020-06-25 08:30:56 -07:00
|
|
|
persistent?: types.BrowserContextOptions, // Undefined means no persistent context.
|
2020-08-14 13:19:12 -07:00
|
|
|
browserProcess: BrowserProcess,
|
2021-04-16 15:19:44 -07:00
|
|
|
customExecutablePath?: string;
|
2020-06-05 13:50:15 -07:00
|
|
|
proxy?: ProxySettings,
|
2020-11-11 15:12:10 -08:00
|
|
|
protocolLogger: types.ProtocolLogger,
|
2020-12-08 09:35:28 -08:00
|
|
|
browserLogsCollector: RecentLogsCollector,
|
2021-02-11 17:46:54 -08:00
|
|
|
slowMo?: number;
|
2021-02-15 08:32:13 -08:00
|
|
|
wsEndpoint?: string; // Only there when connected over web socket.
|
2020-05-14 13:22:33 -07:00
|
|
|
};
|
2019-12-19 14:51:49 -08:00
|
|
|
|
2021-02-09 09:00:00 -08:00
|
|
|
export abstract class Browser extends SdkObject {
|
2020-08-21 16:26:33 -07:00
|
|
|
static Events = {
|
|
|
|
Disconnected: 'disconnected',
|
|
|
|
};
|
|
|
|
|
2021-01-29 16:00:56 -08:00
|
|
|
readonly options: BrowserOptions;
|
2020-04-02 17:56:14 -07:00
|
|
|
private _downloads = new Map<string, Download>();
|
2020-08-19 10:31:59 -07:00
|
|
|
_defaultContext: BrowserContext | null = null;
|
2020-06-29 16:26:32 -07:00
|
|
|
private _startedClosing = false;
|
2021-03-31 10:38:05 -07:00
|
|
|
readonly _idToVideo = new Map<string, { context: BrowserContext, artifact: Artifact }>();
|
2020-04-20 07:52:26 -07:00
|
|
|
|
2020-05-14 13:22:33 -07:00
|
|
|
constructor(options: BrowserOptions) {
|
2021-04-20 23:03:56 -07:00
|
|
|
super(options.rootSdkObject, 'browser');
|
2021-02-09 09:00:00 -08:00
|
|
|
this.attribution.browser = this;
|
2021-01-29 16:00:56 -08:00
|
|
|
this.options = options;
|
2020-04-20 07:52:26 -07:00
|
|
|
}
|
2020-04-02 17:56:14 -07:00
|
|
|
|
2021-02-11 17:46:54 -08:00
|
|
|
abstract newContext(options: types.BrowserContextOptions): Promise<BrowserContext>;
|
2020-04-02 17:56:14 -07:00
|
|
|
abstract contexts(): BrowserContext[];
|
|
|
|
abstract isConnected(): boolean;
|
2020-07-27 13:41:35 -07:00
|
|
|
abstract version(): string;
|
2020-04-02 17:56:14 -07:00
|
|
|
|
2020-05-12 19:23:08 -07:00
|
|
|
_downloadCreated(page: Page, uuid: string, url: string, suggestedFilename?: string) {
|
2021-01-29 16:00:56 -08:00
|
|
|
const download = new Download(page, this.options.downloadsPath || '', uuid, url, suggestedFilename);
|
2020-04-02 17:56:14 -07:00
|
|
|
this._downloads.set(uuid, download);
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:23:08 -07:00
|
|
|
_downloadFilenameSuggested(uuid: string, suggestedFilename: string) {
|
|
|
|
const download = this._downloads.get(uuid);
|
|
|
|
if (!download)
|
|
|
|
return;
|
|
|
|
download._filenameSuggested(suggestedFilename);
|
|
|
|
}
|
|
|
|
|
2020-04-07 15:01:42 -07:00
|
|
|
_downloadFinished(uuid: string, error?: string) {
|
2020-04-02 17:56:14 -07:00
|
|
|
const download = this._downloads.get(uuid);
|
|
|
|
if (!download)
|
|
|
|
return;
|
2021-03-31 10:38:05 -07:00
|
|
|
download.artifact.reportFinished(error);
|
2020-04-02 17:56:14 -07:00
|
|
|
this._downloads.delete(uuid);
|
|
|
|
}
|
2020-04-03 16:34:07 -07:00
|
|
|
|
2020-09-18 17:36:43 -07:00
|
|
|
_videoStarted(context: BrowserContext, videoId: string, path: string, pageOrError: Promise<Page | Error>) {
|
2021-04-20 23:03:56 -07:00
|
|
|
const artifact = new Artifact(context, path);
|
2021-03-31 10:38:05 -07:00
|
|
|
this._idToVideo.set(videoId, { context, artifact });
|
|
|
|
context.emit(BrowserContext.Events.VideoStarted, artifact);
|
|
|
|
pageOrError.then(page => {
|
|
|
|
if (page instanceof Page) {
|
|
|
|
page._video = artifact;
|
|
|
|
page.emit(Page.Events.Video, artifact);
|
|
|
|
}
|
2020-09-08 17:01:00 -07:00
|
|
|
});
|
2020-08-25 17:18:28 -07:00
|
|
|
}
|
|
|
|
|
2021-04-09 14:09:45 -07:00
|
|
|
_takeVideo(videoId: string): Artifact | undefined {
|
2021-03-31 10:38:05 -07:00
|
|
|
const video = this._idToVideo.get(videoId);
|
2020-09-04 22:37:38 -07:00
|
|
|
this._idToVideo.delete(videoId);
|
2021-04-09 14:09:45 -07:00
|
|
|
return video?.artifact;
|
2020-08-25 17:18:28 -07:00
|
|
|
}
|
|
|
|
|
2020-06-29 16:26:32 -07:00
|
|
|
_didClose() {
|
|
|
|
for (const context of this.contexts())
|
2020-08-19 10:31:59 -07:00
|
|
|
context._browserClosed();
|
2020-07-09 08:34:07 -07:00
|
|
|
if (this._defaultContext)
|
|
|
|
this._defaultContext._browserClosed();
|
2020-08-21 16:26:33 -07:00
|
|
|
this.emit(Browser.Events.Disconnected);
|
2020-06-29 16:26:32 -07:00
|
|
|
}
|
|
|
|
|
2020-04-03 16:34:07 -07:00
|
|
|
async close() {
|
2020-06-29 16:26:32 -07:00
|
|
|
if (!this._startedClosing) {
|
|
|
|
this._startedClosing = true;
|
2021-01-29 16:00:56 -08:00
|
|
|
await this.options.browserProcess.close();
|
2020-04-03 16:34:07 -07:00
|
|
|
}
|
|
|
|
if (this.isConnected())
|
2020-08-21 16:26:33 -07:00
|
|
|
await new Promise(x => this.once(Browser.Events.Disconnected, x));
|
2020-04-03 16:34:07 -07:00
|
|
|
}
|
2021-04-08 18:56:09 -07:00
|
|
|
|
|
|
|
async killForTests() {
|
|
|
|
await this.options.browserProcess.kill();
|
|
|
|
}
|
2020-02-05 12:41:55 -08:00
|
|
|
}
|