2019-12-20 01:53:24 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright 2018 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-04-03 02:56:14 +02:00
|
|
|
import { BrowserBase } from '../browser';
|
2020-03-21 03:17:46 +01:00
|
|
|
import { assertBrowserContextIsNotOwned, BrowserContext, BrowserContextBase, BrowserContextOptions, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
|
2019-12-20 22:07:14 +01:00
|
|
|
import { Events } from '../events';
|
2020-03-06 00:18:27 +01:00
|
|
|
import { assert, helper, RegisteredListener } from '../helper';
|
2019-12-20 22:07:14 +01:00
|
|
|
import * as network from '../network';
|
2020-03-20 00:25:12 +01:00
|
|
|
import { Page, PageBinding } from '../page';
|
2020-02-05 04:41:38 +01:00
|
|
|
import { ConnectionTransport, SlowMoTransport } from '../transport';
|
2020-03-06 02:22:57 +01:00
|
|
|
import * as types from '../types';
|
2020-03-09 20:32:42 +01:00
|
|
|
import { ConnectionEvents, FFConnection } from './ffConnection';
|
2020-02-26 21:42:20 +01:00
|
|
|
import { headersArray } from './ffNetworkManager';
|
2020-03-06 02:22:57 +01:00
|
|
|
import { FFPage } from './ffPage';
|
|
|
|
|
import { Protocol } from './protocol';
|
2020-01-08 01:13:49 +01:00
|
|
|
|
2020-04-03 02:56:14 +02:00
|
|
|
export class FFBrowser extends BrowserBase {
|
2019-12-20 01:53:24 +01:00
|
|
|
_connection: FFConnection;
|
2020-03-09 20:32:42 +01:00
|
|
|
readonly _ffPages: Map<string, FFPage>;
|
2020-02-28 01:18:33 +01:00
|
|
|
readonly _defaultContext: FFBrowserContext;
|
2020-02-24 17:53:30 +01:00
|
|
|
readonly _contexts: Map<string, FFBrowserContext>;
|
2019-12-20 01:53:24 +01:00
|
|
|
private _eventListeners: RegisteredListener[];
|
2020-03-09 20:32:42 +01:00
|
|
|
readonly _firstPagePromise: Promise<void>;
|
|
|
|
|
private _firstPageCallback = () => {};
|
2019-12-20 01:53:24 +01:00
|
|
|
|
2020-03-07 01:49:48 +01:00
|
|
|
static async connect(transport: ConnectionTransport, attachToDefaultContext: boolean, slowMo?: number): Promise<FFBrowser> {
|
2020-02-05 04:41:38 +01:00
|
|
|
const connection = new FFConnection(SlowMoTransport.wrap(transport, slowMo));
|
2020-02-06 21:41:43 +01:00
|
|
|
const browser = new FFBrowser(connection);
|
2020-03-07 01:49:48 +01:00
|
|
|
await connection.send('Browser.enable', { attachToDefaultContext });
|
2019-12-20 01:53:24 +01:00
|
|
|
return browser;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 21:41:43 +01:00
|
|
|
constructor(connection: FFConnection) {
|
2019-12-20 01:53:24 +01:00
|
|
|
super();
|
|
|
|
|
this._connection = connection;
|
2020-03-09 20:32:42 +01:00
|
|
|
this._ffPages = new Map();
|
2019-12-20 01:53:24 +01:00
|
|
|
|
2020-02-24 17:53:30 +01:00
|
|
|
this._defaultContext = new FFBrowserContext(this, null, validateBrowserContextOptions({}));
|
2019-12-20 01:53:24 +01:00
|
|
|
this._contexts = new Map();
|
2020-02-11 19:27:19 +01:00
|
|
|
this._connection.on(ConnectionEvents.Disconnected, () => {
|
2020-02-24 17:53:30 +01:00
|
|
|
for (const context of this._contexts.values())
|
2020-02-11 19:27:19 +01:00
|
|
|
context._browserClosed();
|
|
|
|
|
this.emit(Events.Browser.Disconnected);
|
|
|
|
|
});
|
2019-12-20 01:53:24 +01:00
|
|
|
this._eventListeners = [
|
2020-03-07 01:49:48 +01:00
|
|
|
helper.addEventListener(this._connection, 'Browser.attachedToTarget', this._onAttachedToTarget.bind(this)),
|
|
|
|
|
helper.addEventListener(this._connection, 'Browser.detachedFromTarget', this._onDetachedFromTarget.bind(this)),
|
2020-04-08 00:01:42 +02:00
|
|
|
helper.addEventListener(this._connection, 'Browser.downloadCreated', this._onDownloadCreated.bind(this)),
|
|
|
|
|
helper.addEventListener(this._connection, 'Browser.downloadFinished', this._onDownloadFinished.bind(this)),
|
2019-12-20 01:53:24 +01:00
|
|
|
];
|
2020-03-09 20:32:42 +01:00
|
|
|
this._firstPagePromise = new Promise(f => this._firstPageCallback = f);
|
2019-12-20 01:53:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isConnected(): boolean {
|
|
|
|
|
return !this._connection._closed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> {
|
2020-02-24 17:53:30 +01:00
|
|
|
options = validateBrowserContextOptions(options);
|
2020-02-18 18:16:32 +01:00
|
|
|
let viewport;
|
|
|
|
|
if (options.viewport) {
|
2020-03-04 02:28:31 +01:00
|
|
|
// TODO: remove isMobile/hasTouch from the protocol?
|
2020-03-18 02:21:02 +01:00
|
|
|
if (options.isMobile)
|
|
|
|
|
throw new Error('options.isMobile is not supported in Firefox');
|
2020-02-18 18:16:32 +01:00
|
|
|
viewport = {
|
|
|
|
|
viewportSize: { width: options.viewport.width, height: options.viewport.height },
|
2020-03-18 02:21:02 +01:00
|
|
|
deviceScaleFactor: options.deviceScaleFactor || 1,
|
2020-03-04 02:28:31 +01:00
|
|
|
isMobile: false,
|
2020-03-22 01:58:33 +01:00
|
|
|
hasTouch: !!options.hasTouch,
|
2020-02-18 18:16:32 +01:00
|
|
|
};
|
|
|
|
|
} else if (options.viewport !== null) {
|
|
|
|
|
viewport = {
|
|
|
|
|
viewportSize: { width: 1280, height: 720 },
|
|
|
|
|
deviceScaleFactor: 1,
|
2020-03-04 02:28:31 +01:00
|
|
|
isMobile: false,
|
2020-02-18 18:16:32 +01:00
|
|
|
hasTouch: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-03-07 01:49:48 +01:00
|
|
|
const { browserContextId } = await this._connection.send('Browser.createBrowserContext', {
|
2020-02-12 03:52:01 +01:00
|
|
|
userAgent: options.userAgent,
|
|
|
|
|
bypassCSP: options.bypassCSP,
|
2020-04-01 21:59:48 +02:00
|
|
|
ignoreHTTPSErrors: options.ignoreHTTPSErrors,
|
2020-02-12 03:52:01 +01:00
|
|
|
javaScriptDisabled: options.javaScriptEnabled === false ? true : undefined,
|
|
|
|
|
viewport,
|
2020-03-22 16:56:50 +01:00
|
|
|
locale: options.locale,
|
2020-04-02 07:10:56 +02:00
|
|
|
timezoneId: options.timezoneId,
|
2020-04-08 00:01:42 +02:00
|
|
|
removeOnDetach: true,
|
|
|
|
|
downloadOptions: {
|
|
|
|
|
behavior: options.acceptDownloads ? 'saveToDisk' : 'cancel',
|
|
|
|
|
downloadsDir: this._downloadsPath,
|
|
|
|
|
},
|
2020-02-07 04:01:03 +01:00
|
|
|
});
|
2020-02-24 17:53:30 +01:00
|
|
|
const context = new FFBrowserContext(this, browserContextId, options);
|
2020-01-13 22:32:44 +01:00
|
|
|
await context._initialize();
|
2019-12-20 01:53:24 +01:00
|
|
|
this._contexts.set(browserContextId, context);
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 19:41:45 +01:00
|
|
|
contexts(): BrowserContext[] {
|
2020-02-05 21:41:55 +01:00
|
|
|
return Array.from(this._contexts.values());
|
2019-12-20 01:53:24 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-07 01:49:48 +01:00
|
|
|
_onDetachedFromTarget(payload: Protocol.Browser.detachedFromTargetPayload) {
|
2020-03-09 20:32:42 +01:00
|
|
|
const ffPage = this._ffPages.get(payload.targetId)!;
|
|
|
|
|
this._ffPages.delete(payload.targetId);
|
|
|
|
|
ffPage.didClose();
|
2019-12-20 01:53:24 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-13 19:33:33 +01:00
|
|
|
_onAttachedToTarget(payload: Protocol.Browser.attachedToTargetPayload) {
|
2020-03-07 01:49:48 +01:00
|
|
|
const {targetId, browserContextId, openerId, type} = payload.targetInfo;
|
2020-03-09 20:32:42 +01:00
|
|
|
assert(type === 'page');
|
2020-03-07 01:49:48 +01:00
|
|
|
const context = browserContextId ? this._contexts.get(browserContextId)! : this._defaultContext;
|
2020-03-09 20:32:42 +01:00
|
|
|
const session = this._connection.createSession(payload.sessionId, type);
|
|
|
|
|
const opener = openerId ? this._ffPages.get(openerId)! : null;
|
|
|
|
|
const ffPage = new FFPage(session, context, opener);
|
|
|
|
|
this._ffPages.set(targetId, ffPage);
|
|
|
|
|
|
2020-04-16 22:09:24 +02:00
|
|
|
if (opener && opener._initializedPage) {
|
|
|
|
|
for (const signalBarrier of opener._initializedPage._frameManager._signalBarriers)
|
|
|
|
|
signalBarrier.addPopup(ffPage.pageOrError());
|
|
|
|
|
}
|
2020-03-13 19:33:33 +01:00
|
|
|
ffPage.pageOrError().then(async () => {
|
|
|
|
|
this._firstPageCallback();
|
2020-03-20 00:25:12 +01:00
|
|
|
const page = ffPage._page;
|
|
|
|
|
context.emit(Events.BrowserContext.Page, page);
|
2020-03-13 19:33:33 +01:00
|
|
|
if (!opener)
|
|
|
|
|
return;
|
|
|
|
|
const openerPage = await opener.pageOrError();
|
|
|
|
|
if (openerPage instanceof Page && !openerPage.isClosed())
|
2020-03-20 00:25:12 +01:00
|
|
|
openerPage.emit(Events.Page.Popup, page);
|
2020-03-13 19:33:33 +01:00
|
|
|
});
|
2020-02-07 04:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-08 00:01:42 +02:00
|
|
|
_onDownloadCreated(payload: Protocol.Browser.downloadCreatedPayload) {
|
|
|
|
|
const ffPage = this._ffPages.get(payload.pageTargetId)!;
|
|
|
|
|
assert(ffPage);
|
|
|
|
|
if (!ffPage)
|
|
|
|
|
return;
|
|
|
|
|
this._downloadCreated(ffPage._page, payload.uuid, payload.url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onDownloadFinished(payload: Protocol.Browser.downloadFinishedPayload) {
|
|
|
|
|
const error = payload.canceled ? 'canceled' : payload.error;
|
|
|
|
|
this._downloadFinished(payload.uuid, error);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-04 01:34:07 +02:00
|
|
|
_disconnect() {
|
2019-12-20 01:53:24 +01:00
|
|
|
helper.removeEventListeners(this._eventListeners);
|
2020-02-06 21:41:43 +01:00
|
|
|
this._connection.close();
|
2020-04-03 01:57:12 +02:00
|
|
|
}
|
2019-12-20 01:53:24 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-06 02:22:57 +01:00
|
|
|
export class FFBrowserContext extends BrowserContextBase {
|
2020-02-24 17:53:30 +01:00
|
|
|
readonly _browser: FFBrowser;
|
|
|
|
|
readonly _browserContextId: string | null;
|
2020-02-28 01:18:33 +01:00
|
|
|
private readonly _evaluateOnNewDocumentSources: string[];
|
2020-02-24 17:53:30 +01:00
|
|
|
|
|
|
|
|
constructor(browser: FFBrowser, browserContextId: string | null, options: BrowserContextOptions) {
|
2020-03-06 02:22:57 +01:00
|
|
|
super(options);
|
2020-02-24 17:53:30 +01:00
|
|
|
this._browser = browser;
|
|
|
|
|
this._browserContextId = browserContextId;
|
2020-02-28 01:18:33 +01:00
|
|
|
this._evaluateOnNewDocumentSources = [];
|
2020-02-24 17:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _initialize() {
|
2020-03-17 23:32:50 +01:00
|
|
|
if (this._options.permissions)
|
|
|
|
|
await this.grantPermissions(this._options.permissions);
|
2020-03-21 03:32:27 +01:00
|
|
|
if (this._options.extraHTTPHeaders || this._options.locale)
|
|
|
|
|
await this.setExtraHTTPHeaders(this._options.extraHTTPHeaders || {});
|
2020-03-06 22:50:42 +01:00
|
|
|
if (this._options.httpCredentials)
|
|
|
|
|
await this.setHTTPCredentials(this._options.httpCredentials);
|
2020-03-22 16:56:50 +01:00
|
|
|
if (this._options.geolocation)
|
|
|
|
|
await this.setGeolocation(this._options.geolocation);
|
2020-03-22 23:34:30 +01:00
|
|
|
if (this._options.offline)
|
|
|
|
|
await this.setOffline(this._options.offline);
|
2020-04-07 04:49:33 +02:00
|
|
|
if (this._options.colorScheme)
|
|
|
|
|
await this._setColorScheme(this._options.colorScheme);
|
2020-02-24 17:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-09 20:32:42 +01:00
|
|
|
_ffPages(): FFPage[] {
|
|
|
|
|
return Array.from(this._browser._ffPages.values()).filter(ffPage => ffPage._browserContext === this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-24 17:53:30 +01:00
|
|
|
setDefaultNavigationTimeout(timeout: number) {
|
|
|
|
|
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDefaultTimeout(timeout: number) {
|
|
|
|
|
this._timeoutSettings.setDefaultTimeout(timeout);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 19:33:33 +01:00
|
|
|
pages(): Page[] {
|
2020-04-16 22:09:24 +02:00
|
|
|
return this._ffPages().map(ffPage => ffPage._initializedPage).filter(pageOrNull => !!pageOrNull) as Page[];
|
2020-02-24 17:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async newPage(): Promise<Page> {
|
|
|
|
|
assertBrowserContextIsNotOwned(this);
|
2020-03-09 20:32:42 +01:00
|
|
|
const { targetId } = await this._browser._connection.send('Browser.newPage', {
|
2020-02-24 17:53:30 +01:00
|
|
|
browserContextId: this._browserContextId || undefined
|
2020-04-02 07:10:56 +02:00
|
|
|
}).catch(e => {
|
|
|
|
|
if (e.message.includes('Failed to override timezone'))
|
|
|
|
|
throw new Error(`Invalid timezone ID: ${this._options.timezoneId}`);
|
|
|
|
|
throw e;
|
2020-02-24 17:53:30 +01:00
|
|
|
});
|
2020-03-09 20:32:42 +01:00
|
|
|
const ffPage = this._browser._ffPages.get(targetId)!;
|
|
|
|
|
const pageOrError = await ffPage.pageOrError();
|
|
|
|
|
if (pageOrError instanceof Page) {
|
|
|
|
|
if (pageOrError.isClosed())
|
2020-03-06 00:18:27 +01:00
|
|
|
throw new Error('Page has been closed.');
|
2020-03-09 20:32:42 +01:00
|
|
|
return pageOrError;
|
2020-03-06 00:18:27 +01:00
|
|
|
}
|
2020-03-09 20:32:42 +01:00
|
|
|
throw pageOrError;
|
2020-02-24 17:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-06 17:24:32 +01:00
|
|
|
async cookies(urls?: string | string[]): Promise<network.NetworkCookie[]> {
|
2020-02-24 17:53:30 +01:00
|
|
|
const { cookies } = await this._browser._connection.send('Browser.getCookies', { browserContextId: this._browserContextId || undefined });
|
|
|
|
|
return network.filterCookies(cookies.map(c => {
|
|
|
|
|
const copy: any = { ... c };
|
|
|
|
|
delete copy.size;
|
2020-03-07 17:41:57 +01:00
|
|
|
delete copy.session;
|
2020-02-24 17:53:30 +01:00
|
|
|
return copy as network.NetworkCookie;
|
|
|
|
|
}), urls);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 01:32:33 +01:00
|
|
|
async addCookies(cookies: network.SetNetworkCookieParam[]) {
|
2020-02-24 17:53:30 +01:00
|
|
|
await this._browser._connection.send('Browser.setCookies', { browserContextId: this._browserContextId || undefined, cookies: network.rewriteCookies(cookies) });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async clearCookies() {
|
|
|
|
|
await this._browser._connection.send('Browser.clearCookies', { browserContextId: this._browserContextId || undefined });
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 23:32:50 +01:00
|
|
|
async _doGrantPermissions(origin: string, permissions: string[]) {
|
|
|
|
|
const webPermissionToProtocol = new Map<string, 'geo' | 'desktop-notification' | 'persistent-storage' | 'push'>([
|
2020-02-24 17:53:30 +01:00
|
|
|
['geolocation', 'geo'],
|
2020-03-17 23:32:50 +01:00
|
|
|
['persistent-storage', 'persistent-storage'],
|
|
|
|
|
['push', 'push'],
|
|
|
|
|
['notifications', 'desktop-notification'],
|
2020-02-24 17:53:30 +01:00
|
|
|
]);
|
|
|
|
|
const filtered = permissions.map(permission => {
|
|
|
|
|
const protocolPermission = webPermissionToProtocol.get(permission);
|
|
|
|
|
if (!protocolPermission)
|
|
|
|
|
throw new Error('Unknown permission: ' + permission);
|
|
|
|
|
return protocolPermission;
|
|
|
|
|
});
|
2020-03-17 23:32:50 +01:00
|
|
|
await this._browser._connection.send('Browser.grantPermissions', { origin: origin, browserContextId: this._browserContextId || undefined, permissions: filtered});
|
2020-02-24 17:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-17 23:32:50 +01:00
|
|
|
async _doClearPermissions() {
|
2020-02-24 17:53:30 +01:00
|
|
|
await this._browser._connection.send('Browser.resetPermissions', { browserContextId: this._browserContextId || undefined });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {
|
2020-03-21 03:17:46 +01:00
|
|
|
if (geolocation)
|
|
|
|
|
geolocation = verifyGeolocation(geolocation);
|
|
|
|
|
this._options.geolocation = geolocation || undefined;
|
2020-03-22 16:56:50 +01:00
|
|
|
await this._browser._connection.send('Browser.setGeolocationOverride', { browserContextId: this._browserContextId || undefined, geolocation });
|
2020-02-24 17:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-26 21:42:20 +01:00
|
|
|
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
|
|
|
|
|
this._options.extraHTTPHeaders = network.verifyHeaders(headers);
|
2020-03-21 03:32:27 +01:00
|
|
|
const allHeaders = { ...this._options.extraHTTPHeaders };
|
|
|
|
|
if (this._options.locale)
|
|
|
|
|
allHeaders['Accept-Language'] = this._options.locale;
|
|
|
|
|
await this._browser._connection.send('Browser.setExtraHTTPHeaders', { browserContextId: this._browserContextId || undefined, headers: headersArray(allHeaders) });
|
2020-02-26 21:42:20 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-05 02:58:12 +01:00
|
|
|
async setOffline(offline: boolean): Promise<void> {
|
|
|
|
|
this._options.offline = offline;
|
2020-03-22 23:34:30 +01:00
|
|
|
await this._browser._connection.send('Browser.setOnlineOverride', { browserContextId: this._browserContextId || undefined, override: offline ? 'offline' : 'online' });
|
2020-03-05 02:58:12 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-07 04:49:33 +02:00
|
|
|
async _setColorScheme(colorScheme?: types.ColorScheme): Promise<void> {
|
|
|
|
|
await this._browser._connection.send('Browser.setColorScheme', { browserContextId: this._browserContextId || undefined, colorScheme });
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 22:50:42 +01:00
|
|
|
async setHTTPCredentials(httpCredentials: types.Credentials | null): Promise<void> {
|
|
|
|
|
this._options.httpCredentials = httpCredentials || undefined;
|
|
|
|
|
await this._browser._connection.send('Browser.setHTTPCredentials', { browserContextId: this._browserContextId || undefined, credentials: httpCredentials });
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 23:08:17 +01:00
|
|
|
async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any) {
|
|
|
|
|
const source = await helper.evaluationScript(script, arg);
|
2020-02-28 01:18:33 +01:00
|
|
|
this._evaluateOnNewDocumentSources.push(source);
|
|
|
|
|
await this._browser._connection.send('Browser.addScriptToEvaluateOnNewDocument', { browserContextId: this._browserContextId || undefined, script: source });
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 01:46:06 +01:00
|
|
|
async exposeFunction(name: string, playwrightFunction: Function): Promise<void> {
|
2020-03-13 19:33:33 +01:00
|
|
|
for (const page of this.pages()) {
|
2020-03-04 01:46:06 +01:00
|
|
|
if (page._pageBindings.has(name))
|
|
|
|
|
throw new Error(`Function "${name}" has been already registered in one of the pages`);
|
|
|
|
|
}
|
|
|
|
|
if (this._pageBindings.has(name))
|
|
|
|
|
throw new Error(`Function "${name}" has been already registered`);
|
|
|
|
|
const binding = new PageBinding(name, playwrightFunction);
|
|
|
|
|
this._pageBindings.set(name, binding);
|
2020-03-23 06:45:15 +01:00
|
|
|
await this._browser._connection.send('Browser.addBinding', { browserContextId: this._browserContextId || undefined, name, script: binding.source });
|
2020-03-04 01:46:06 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-10 05:02:54 +01:00
|
|
|
async route(url: types.URLMatch, handler: network.RouteHandler): Promise<void> {
|
|
|
|
|
this._routes.push({ url, handler });
|
2020-03-22 16:56:50 +01:00
|
|
|
if (this._routes.length === 1)
|
|
|
|
|
await this._browser._connection.send('Browser.setRequestInterception', { browserContextId: this._browserContextId || undefined, enabled: true });
|
2020-03-10 05:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-16 04:55:22 +02:00
|
|
|
async unroute(url: types.URLMatch, handler?: network.RouteHandler): Promise<void> {
|
|
|
|
|
this._routes = this._routes.filter(route => route.url !== url || (handler && route.handler !== handler));
|
|
|
|
|
if (this._routes.length === 0)
|
|
|
|
|
await this._browser._connection.send('Browser.setRequestInterception', { browserContextId: this._browserContextId || undefined, enabled: false });
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-24 17:53:30 +01:00
|
|
|
async close() {
|
|
|
|
|
if (this._closed)
|
|
|
|
|
return;
|
2020-03-10 00:53:33 +01:00
|
|
|
if (!this._browserContextId) {
|
|
|
|
|
// Default context is only created in 'persistent' mode and closing it should close
|
|
|
|
|
// the browser.
|
|
|
|
|
await this._browser.close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-07 01:49:48 +01:00
|
|
|
await this._browser._connection.send('Browser.removeBrowserContext', { browserContextId: this._browserContextId });
|
2020-02-24 17:53:30 +01:00
|
|
|
this._browser._contexts.delete(this._browserContextId);
|
2020-04-03 02:56:14 +02:00
|
|
|
await this._didCloseInternal();
|
2020-02-24 17:53:30 +01:00
|
|
|
}
|
|
|
|
|
}
|