2020-01-09 14:49:22 -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.
|
|
|
|
*/
|
|
|
|
|
2020-12-09 15:06:57 -08:00
|
|
|
import { Android } from './android/android';
|
|
|
|
import { AdbBackend } from './android/backendAdb';
|
2023-06-01 17:54:43 -07:00
|
|
|
import type { Browser } from './browser';
|
2020-08-22 15:46:42 -07:00
|
|
|
import { Chromium } from './chromium/chromium';
|
2020-12-09 15:06:57 -08:00
|
|
|
import { Electron } from './electron/electron';
|
2020-08-23 17:05:58 -07:00
|
|
|
import { Firefox } from './firefox/firefox';
|
2021-06-15 14:56:29 -07:00
|
|
|
import { Selectors } from './selectors';
|
2020-12-09 15:06:57 -08:00
|
|
|
import { WebKit } from './webkit/webkit';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { CallMetadata } from './instrumentation';
|
|
|
|
import { createInstrumentation, SdkObject } from './instrumentation';
|
2024-08-13 12:47:02 -07:00
|
|
|
import { debugLogger, type Language } from '../utils';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Page } from './page';
|
2022-09-21 14:35:52 -08:00
|
|
|
import { DebugController } from './debugController';
|
2024-08-12 22:20:58 -07:00
|
|
|
import type { BrowserType } from './browserType';
|
2020-01-09 14:49:22 -08:00
|
|
|
|
2023-06-01 17:54:43 -07:00
|
|
|
type PlaywrightOptions = {
|
|
|
|
socksProxyPort?: number;
|
|
|
|
sdkLanguage: Language;
|
|
|
|
isInternalPlaywright?: boolean;
|
|
|
|
isServer?: boolean;
|
|
|
|
};
|
|
|
|
|
2021-02-09 09:00:00 -08:00
|
|
|
export class Playwright extends SdkObject {
|
|
|
|
readonly selectors: Selectors;
|
2024-08-12 22:20:58 -07:00
|
|
|
readonly chromium: BrowserType;
|
2020-12-09 15:06:57 -08:00
|
|
|
readonly android: Android;
|
|
|
|
readonly electron: Electron;
|
2024-08-12 22:20:58 -07:00
|
|
|
readonly firefox: BrowserType;
|
|
|
|
readonly webkit: BrowserType;
|
2021-01-31 16:37:13 -08:00
|
|
|
readonly options: PlaywrightOptions;
|
2022-09-21 14:35:52 -08:00
|
|
|
readonly debugController: DebugController;
|
2022-01-12 07:37:48 -08:00
|
|
|
private _allPages = new Set<Page>();
|
2022-07-31 14:31:17 -07:00
|
|
|
private _allBrowsers = new Set<Browser>();
|
2020-01-24 14:49:47 -08:00
|
|
|
|
2023-06-01 17:54:43 -07:00
|
|
|
constructor(options: PlaywrightOptions) {
|
|
|
|
super({ attribution: {}, instrumentation: createInstrumentation() } as any, undefined, 'Playwright');
|
|
|
|
this.options = options;
|
|
|
|
this.attribution.playwright = this;
|
2021-04-26 16:20:38 -07:00
|
|
|
this.instrumentation.addListener({
|
2022-07-31 14:31:17 -07:00
|
|
|
onBrowserOpen: browser => this._allBrowsers.add(browser),
|
|
|
|
onBrowserClose: browser => this._allBrowsers.delete(browser),
|
2022-01-12 07:37:48 -08:00
|
|
|
onPageOpen: page => this._allPages.add(page),
|
|
|
|
onPageClose: page => this._allPages.delete(page),
|
|
|
|
onCallLog: (sdkObject: SdkObject, metadata: CallMetadata, logName: string, message: string) => {
|
2021-04-26 16:20:38 -07:00
|
|
|
debugLogger.log(logName as any, message);
|
|
|
|
}
|
2022-01-12 07:37:48 -08:00
|
|
|
}, null);
|
2023-06-01 17:54:43 -07:00
|
|
|
this.chromium = new Chromium(this);
|
|
|
|
this.firefox = new Firefox(this);
|
|
|
|
this.webkit = new WebKit(this);
|
|
|
|
this.electron = new Electron(this);
|
|
|
|
this.android = new Android(this, new AdbBackend());
|
|
|
|
this.selectors = new Selectors();
|
2022-09-21 14:35:52 -08:00
|
|
|
this.debugController = new DebugController(this);
|
2020-01-24 14:49:47 -08:00
|
|
|
}
|
2022-01-12 07:37:48 -08:00
|
|
|
|
|
|
|
async hideHighlight() {
|
|
|
|
await Promise.all([...this._allPages].map(p => p.hideHighlight().catch(() => {})));
|
|
|
|
}
|
2022-07-31 14:31:17 -07:00
|
|
|
|
|
|
|
allBrowsers(): Browser[] {
|
|
|
|
return [...this._allBrowsers];
|
|
|
|
}
|
2022-08-05 19:34:57 -07:00
|
|
|
|
|
|
|
allPages(): Page[] {
|
|
|
|
return [...this._allPages];
|
|
|
|
}
|
2020-01-09 14:49:22 -08:00
|
|
|
}
|
2021-01-29 16:00:56 -08:00
|
|
|
|
2023-06-01 17:54:43 -07:00
|
|
|
export function createPlaywright(options: PlaywrightOptions) {
|
|
|
|
return new Playwright(options);
|
2021-01-29 16:00:56 -08:00
|
|
|
}
|