2020-07-08 18:42:04 -07: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.
|
|
|
|
*/
|
|
|
|
|
2022-09-20 18:41:51 -07:00
|
|
|
import type * as channels from '@protocol/channels';
|
2022-04-07 13:36:13 -08:00
|
|
|
import { TimeoutError } from '../common/errors';
|
2021-08-19 15:16:46 -07:00
|
|
|
import { Android } from './android';
|
2020-07-08 18:42:04 -07:00
|
|
|
import { BrowserType } from './browserType';
|
|
|
|
import { ChannelOwner } from './channelOwner';
|
2020-07-13 21:46:59 -07:00
|
|
|
import { Electron } from './electron';
|
2021-11-05 16:27:49 +01:00
|
|
|
import { APIRequest } from './fetch';
|
2021-10-15 09:21:56 -07:00
|
|
|
import { Selectors, SelectorsOwner } from './selectors';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { Size } from './types';
|
2020-07-29 17:26:59 -07:00
|
|
|
|
|
|
|
type DeviceDescriptor = {
|
|
|
|
userAgent: string,
|
|
|
|
viewport: Size,
|
|
|
|
deviceScaleFactor: number,
|
|
|
|
isMobile: boolean,
|
2020-09-03 22:12:43 +02:00
|
|
|
hasTouch: boolean,
|
|
|
|
defaultBrowserType: 'chromium' | 'firefox' | 'webkit'
|
2020-07-29 17:26:59 -07:00
|
|
|
};
|
|
|
|
type Devices = { [name: string]: DeviceDescriptor };
|
2020-07-08 18:42:04 -07:00
|
|
|
|
2021-11-17 15:26:01 -08:00
|
|
|
export class Playwright extends ChannelOwner<channels.PlaywrightChannel> {
|
2020-12-09 15:06:57 -08:00
|
|
|
readonly _android: Android;
|
|
|
|
readonly _electron: Electron;
|
2020-07-16 22:38:52 -07:00
|
|
|
readonly chromium: BrowserType;
|
|
|
|
readonly firefox: BrowserType;
|
|
|
|
readonly webkit: BrowserType;
|
2020-07-29 17:26:59 -07:00
|
|
|
readonly devices: Devices;
|
2021-10-15 09:21:56 -07:00
|
|
|
selectors: Selectors;
|
2021-11-05 16:27:49 +01:00
|
|
|
readonly request: APIRequest;
|
2020-07-16 22:38:52 -07:00
|
|
|
readonly errors: { TimeoutError: typeof TimeoutError };
|
2020-07-08 18:42:04 -07:00
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.PlaywrightInitializer) {
|
2020-07-10 18:00:10 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2021-11-05 16:27:49 +01:00
|
|
|
this.request = new APIRequest(this);
|
2020-07-08 18:42:04 -07:00
|
|
|
this.chromium = BrowserType.from(initializer.chromium);
|
2021-10-15 09:21:56 -07:00
|
|
|
this.chromium._playwright = this;
|
2020-07-08 18:42:04 -07:00
|
|
|
this.firefox = BrowserType.from(initializer.firefox);
|
2021-10-15 09:21:56 -07:00
|
|
|
this.firefox._playwright = this;
|
2020-07-08 18:42:04 -07:00
|
|
|
this.webkit = BrowserType.from(initializer.webkit);
|
2021-10-15 09:21:56 -07:00
|
|
|
this.webkit._playwright = this;
|
2020-12-09 15:06:57 -08:00
|
|
|
this._android = Android.from(initializer.android);
|
|
|
|
this._electron = Electron.from(initializer.electron);
|
2020-07-16 13:18:54 -07:00
|
|
|
this.devices = {};
|
|
|
|
for (const { name, descriptor } of initializer.deviceDescriptors)
|
|
|
|
this.devices[name] = descriptor;
|
2021-10-15 09:21:56 -07:00
|
|
|
this.selectors = new Selectors();
|
2020-07-16 22:38:52 -07:00
|
|
|
this.errors = { TimeoutError };
|
2021-04-12 11:14:54 -07:00
|
|
|
|
2021-10-14 20:58:09 -07:00
|
|
|
const selectorsOwner = SelectorsOwner.from(initializer.selectors);
|
|
|
|
this.selectors._addChannel(selectorsOwner);
|
|
|
|
this._connection.on('close', () => {
|
|
|
|
this.selectors._removeChannel(selectorsOwner);
|
|
|
|
});
|
2022-01-12 07:37:48 -08:00
|
|
|
(global as any)._playwrightInstance = this;
|
|
|
|
}
|
|
|
|
|
2021-10-15 09:21:56 -07:00
|
|
|
_setSelectors(selectors: Selectors) {
|
|
|
|
const selectorsOwner = SelectorsOwner.from(this._initializer.selectors);
|
|
|
|
this.selectors._removeChannel(selectorsOwner);
|
|
|
|
this.selectors = selectors;
|
|
|
|
this.selectors._addChannel(selectorsOwner);
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:31:14 +02:00
|
|
|
static from(channel: channels.PlaywrightChannel): Playwright {
|
|
|
|
return (channel as any)._object;
|
|
|
|
}
|
2020-07-08 18:42:04 -07:00
|
|
|
}
|