2019-12-19 16:53:24 -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-02-11 12:06:58 -08:00
|
|
|
import { Browser, createPageInNewContext } from '../browser';
|
2020-03-05 17:22:57 -08:00
|
|
|
import { assertBrowserContextIsNotOwned, BrowserContext, BrowserContextBase, BrowserContextOptions, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
|
|
|
|
import { Events as CommonEvents } from '../events';
|
|
|
|
import { assert, debugError, helper } from '../helper';
|
2019-12-19 16:53:24 -08:00
|
|
|
import * as network from '../network';
|
2020-03-05 17:22:57 -08:00
|
|
|
import { Page, PageBinding, PageEvent } from '../page';
|
2020-01-07 11:55:24 -08:00
|
|
|
import * as platform from '../platform';
|
2020-02-04 19:41:38 -08:00
|
|
|
import { ConnectionTransport, SlowMoTransport } from '../transport';
|
2020-03-05 17:22:57 -08:00
|
|
|
import * as types from '../types';
|
|
|
|
import { ConnectionEvents, CRConnection, CRSession } from './crConnection';
|
|
|
|
import { CRPage } from './crPage';
|
|
|
|
import { readProtocolStream } from './crProtocolHelper';
|
|
|
|
import { CRTarget } from './crTarget';
|
|
|
|
import { Events } from './events';
|
|
|
|
import { Protocol } from './protocol';
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-01-08 14:04:33 -08:00
|
|
|
export class CRBrowser extends platform.EventEmitter implements Browser {
|
2020-03-06 15:11:03 -08:00
|
|
|
readonly _connection: CRConnection;
|
|
|
|
_session: CRSession;
|
|
|
|
private _clientRootSessionPromise: Promise<CRSession> | null = null;
|
2020-02-24 14:35:51 -08:00
|
|
|
readonly _defaultContext: CRBrowserContext;
|
2020-02-24 08:53:30 -08:00
|
|
|
readonly _contexts = new Map<string, CRBrowserContext>();
|
2019-12-19 16:53:24 -08:00
|
|
|
_targets = new Map<string, CRTarget>();
|
|
|
|
|
|
|
|
private _tracingRecording = false;
|
2020-01-13 13:33:25 -08:00
|
|
|
private _tracingPath: string | null = '';
|
2019-12-19 16:53:24 -08:00
|
|
|
private _tracingClient: CRSession | undefined;
|
|
|
|
|
2020-03-05 10:45:32 -08:00
|
|
|
static async connect(transport: ConnectionTransport, isPersistent: boolean, slowMo?: number): Promise<CRBrowser> {
|
2020-02-04 19:41:38 -08:00
|
|
|
const connection = new CRConnection(SlowMoTransport.wrap(transport, slowMo));
|
2020-02-06 12:41:43 -08:00
|
|
|
const browser = new CRBrowser(connection);
|
2020-03-05 10:45:32 -08:00
|
|
|
const session = connection.rootSession;
|
|
|
|
const promises = [
|
|
|
|
session.send('Target.setDiscoverTargets', { discover: true }),
|
|
|
|
session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true }),
|
|
|
|
];
|
|
|
|
const existingPageAttachPromises: Promise<any>[] = [];
|
|
|
|
if (isPersistent) {
|
|
|
|
// First page and background pages in the persistent context are created automatically
|
|
|
|
// and may be initialized before we enable auto-attach.
|
|
|
|
function attachToExistingPage({targetInfo}: Protocol.Target.targetCreatedPayload) {
|
|
|
|
if (!CRTarget.isPageType(targetInfo.type))
|
|
|
|
return;
|
|
|
|
existingPageAttachPromises.push(session.send('Target.attachToTarget', {targetId: targetInfo.targetId, flatten: true}));
|
|
|
|
}
|
|
|
|
session.on('Target.targetCreated', attachToExistingPage);
|
|
|
|
Promise.all(promises).then(() => session.off('Target.targetCreated', attachToExistingPage)).catch(debugError);
|
|
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
|
|
await Promise.all(existingPageAttachPromises);
|
2019-12-19 16:53:24 -08:00
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:41:43 -08:00
|
|
|
constructor(connection: CRConnection) {
|
2019-12-19 16:53:24 -08:00
|
|
|
super();
|
|
|
|
this._connection = connection;
|
2020-03-06 15:11:03 -08:00
|
|
|
this._session = this._connection.rootSession;
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-02-24 08:53:30 -08:00
|
|
|
this._defaultContext = new CRBrowserContext(this, null, validateBrowserContextOptions({}));
|
2020-02-11 10:27:19 -08:00
|
|
|
this._connection.on(ConnectionEvents.Disconnected, () => {
|
2020-02-24 08:53:30 -08:00
|
|
|
for (const context of this._contexts.values())
|
2020-02-11 10:27:19 -08:00
|
|
|
context._browserClosed();
|
|
|
|
this.emit(CommonEvents.Browser.Disconnected);
|
|
|
|
});
|
2020-03-06 15:11:03 -08:00
|
|
|
this._session.on('Target.targetCreated', this._targetCreated.bind(this));
|
|
|
|
this._session.on('Target.targetDestroyed', this._targetDestroyed.bind(this));
|
|
|
|
this._session.on('Target.targetInfoChanged', this._targetInfoChanged.bind(this));
|
|
|
|
this._session.on('Target.attachedToTarget', this._onAttachedToTarget.bind(this));
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
2020-02-10 10:41:45 -08:00
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> {
|
2020-02-24 08:53:30 -08:00
|
|
|
options = validateBrowserContextOptions(options);
|
2020-03-06 15:11:03 -08:00
|
|
|
const { browserContextId } = await this._session.send('Target.createBrowserContext', { disposeOnDetach: true });
|
2020-02-24 08:53:30 -08:00
|
|
|
const context = new CRBrowserContext(this, browserContextId, options);
|
2020-01-13 13:32:44 -08:00
|
|
|
await context._initialize();
|
2019-12-19 16:53:24 -08:00
|
|
|
this._contexts.set(browserContextId, context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2020-02-10 10:41:45 -08:00
|
|
|
contexts(): BrowserContext[] {
|
2020-02-05 12:41:55 -08:00
|
|
|
return Array.from(this._contexts.values());
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-02-10 10:41:45 -08:00
|
|
|
async newPage(options?: BrowserContextOptions): Promise<Page> {
|
2020-02-11 12:06:58 -08:00
|
|
|
return createPageInNewContext(this, options);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-03-09 15:53:45 -07:00
|
|
|
async _onAttachedToTarget({targetInfo, sessionId, waitingForDebugger}: Protocol.Target.attachedToTargetPayload) {
|
|
|
|
const session = this._connection.session(sessionId)!;
|
|
|
|
if (!CRTarget.isPageType(targetInfo.type)) {
|
|
|
|
assert(targetInfo.type === 'service_worker' || targetInfo.type === 'browser' || targetInfo.type === 'other');
|
|
|
|
if (waitingForDebugger) {
|
|
|
|
// Ideally, detaching should resume any target, but there is a bug in the backend.
|
|
|
|
session.send('Runtime.runIfWaitingForDebugger').catch(debugError).then(() => {
|
|
|
|
this._session.send('Target.detachFromTarget', { sessionId }).catch(debugError);
|
|
|
|
});
|
|
|
|
}
|
2020-03-05 10:45:32 -08:00
|
|
|
return;
|
2020-03-09 15:53:45 -07:00
|
|
|
}
|
|
|
|
const { context, target } = this._createTarget(targetInfo, session);
|
2020-03-02 13:58:22 -08:00
|
|
|
try {
|
|
|
|
switch (targetInfo.type) {
|
|
|
|
case 'page': {
|
2020-03-05 15:18:27 -08:00
|
|
|
const event = new PageEvent(target.pageOrError());
|
2020-03-02 18:32:56 -08:00
|
|
|
context.emit(CommonEvents.BrowserContext.Page, event);
|
2020-03-05 15:18:27 -08:00
|
|
|
const opener = target.opener();
|
|
|
|
if (!opener)
|
|
|
|
break;
|
|
|
|
const openerPage = await opener.pageOrError();
|
|
|
|
if (openerPage instanceof Page && !openerPage.isClosed())
|
|
|
|
openerPage.emit(CommonEvents.Page.Popup, new PageEvent(target.pageOrError()));
|
2020-03-02 13:58:22 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'background_page': {
|
2020-03-05 15:18:27 -08:00
|
|
|
const event = new PageEvent(target.pageOrError());
|
2020-03-02 13:58:22 -08:00
|
|
|
context.emit(Events.CRBrowserContext.BackgroundPage, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Do not dispatch the event if initialization failed.
|
|
|
|
debugError(e);
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-03-09 15:53:45 -07:00
|
|
|
async _targetCreated({targetInfo}: Protocol.Target.targetCreatedPayload) {
|
|
|
|
if (targetInfo.type !== 'service_worker')
|
|
|
|
return;
|
|
|
|
const { context, target } = this._createTarget(targetInfo, null);
|
|
|
|
const serviceWorker = await target.serviceWorker();
|
|
|
|
context.emit(Events.CRBrowserContext.ServiceWorker, serviceWorker);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _createTarget(targetInfo: Protocol.Target.TargetInfo, session: CRSession | null) {
|
|
|
|
const {browserContextId} = targetInfo;
|
|
|
|
const context = (browserContextId && this._contexts.has(browserContextId)) ? this._contexts.get(browserContextId)! : this._defaultContext;
|
|
|
|
const target = new CRTarget(this, targetInfo, context, session, () => this._connection.createSession(targetInfo));
|
|
|
|
assert(!this._targets.has(targetInfo.targetId), 'Target should not exist before targetCreated');
|
|
|
|
this._targets.set(targetInfo.targetId, target);
|
|
|
|
return { context, target };
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async _targetDestroyed(event: { targetId: string; }) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const target = this._targets.get(event.targetId)!;
|
2020-03-09 15:53:45 -07:00
|
|
|
if (!target)
|
|
|
|
return;
|
2019-12-19 16:53:24 -08:00
|
|
|
this._targets.delete(event.targetId);
|
|
|
|
target._didClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
_targetInfoChanged(event: Protocol.Target.targetInfoChangedPayload) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const target = this._targets.get(event.targetInfo.targetId)!;
|
2020-03-09 15:53:45 -07:00
|
|
|
if (!target)
|
|
|
|
return;
|
2019-12-19 16:53:24 -08:00
|
|
|
target._targetInfoChanged(event.targetInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _closePage(page: Page) {
|
2020-03-06 15:11:03 -08:00
|
|
|
await this._session.send('Target.closeTarget', { targetId: CRTarget.fromPage(page)._targetId });
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_allTargets(): CRTarget[] {
|
2020-03-05 10:45:32 -08:00
|
|
|
return Array.from(this._targets.values());
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
2020-01-08 13:55:38 -08:00
|
|
|
const disconnected = new Promise(f => this._connection.once(ConnectionEvents.Disconnected, f));
|
2020-02-10 10:41:45 -08:00
|
|
|
await Promise.all(this.contexts().map(context => context.close()));
|
2020-02-06 12:41:43 -08:00
|
|
|
this._connection.close();
|
2020-01-08 13:55:38 -08:00
|
|
|
await disconnected;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-03-02 13:58:22 -08:00
|
|
|
async createBrowserSession(): Promise<CRSession> {
|
|
|
|
return await this._connection.createBrowserSession();
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-03-03 17:29:12 -08:00
|
|
|
async startTracing(page?: Page, options: { path?: string; screenshots?: boolean; categories?: string[]; } = {}) {
|
2019-12-19 16:53:24 -08:00
|
|
|
assert(!this._tracingRecording, 'Cannot start recording trace while already recording trace.');
|
2020-03-06 15:11:03 -08:00
|
|
|
this._tracingClient = page ? (page._delegate as CRPage)._client : this._session;
|
2019-12-19 16:53:24 -08:00
|
|
|
|
|
|
|
const defaultCategories = [
|
|
|
|
'-*', 'devtools.timeline', 'v8.execute', 'disabled-by-default-devtools.timeline',
|
|
|
|
'disabled-by-default-devtools.timeline.frame', 'toplevel',
|
|
|
|
'blink.console', 'blink.user_timing', 'latencyInfo', 'disabled-by-default-devtools.timeline.stack',
|
|
|
|
'disabled-by-default-v8.cpu_profiler', 'disabled-by-default-v8.cpu_profiler.hires'
|
|
|
|
];
|
|
|
|
const {
|
|
|
|
path = null,
|
|
|
|
screenshots = false,
|
|
|
|
categories = defaultCategories,
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
if (screenshots)
|
|
|
|
categories.push('disabled-by-default-devtools.screenshot');
|
|
|
|
|
|
|
|
this._tracingPath = path;
|
|
|
|
this._tracingRecording = true;
|
|
|
|
await this._tracingClient.send('Tracing.start', {
|
|
|
|
transferMode: 'ReturnAsStream',
|
|
|
|
categories: categories.join(',')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-07 11:55:24 -08:00
|
|
|
async stopTracing(): Promise<platform.BufferType> {
|
2019-12-19 16:53:24 -08:00
|
|
|
assert(this._tracingClient, 'Tracing was not started.');
|
2020-01-07 11:55:24 -08:00
|
|
|
let fulfill: (buffer: platform.BufferType) => void;
|
|
|
|
const contentPromise = new Promise<platform.BufferType>(x => fulfill = x);
|
2020-02-05 16:53:36 -08:00
|
|
|
this._tracingClient.once('Tracing.tracingComplete', event => {
|
2020-01-13 13:33:25 -08:00
|
|
|
readProtocolStream(this._tracingClient!, event.stream!, this._tracingPath).then(fulfill);
|
2019-12-19 16:53:24 -08:00
|
|
|
});
|
2020-02-07 13:38:50 -08:00
|
|
|
await this._tracingClient.send('Tracing.end');
|
2019-12-19 16:53:24 -08:00
|
|
|
this._tracingRecording = false;
|
|
|
|
return contentPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
isConnected(): boolean {
|
|
|
|
return !this._connection._closed;
|
|
|
|
}
|
2020-02-12 22:35:06 -08:00
|
|
|
|
2020-03-06 15:11:03 -08:00
|
|
|
async _clientRootSession(): Promise<CRSession> {
|
|
|
|
if (!this._clientRootSessionPromise)
|
|
|
|
this._clientRootSessionPromise = this._connection.createBrowserSession();
|
|
|
|
return this._clientRootSessionPromise;
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:35:06 -08:00
|
|
|
_setDebugFunction(debugFunction: (message: string) => void) {
|
|
|
|
this._connection._debugProtocol = debugFunction;
|
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
2020-02-24 08:53:30 -08:00
|
|
|
|
2020-03-05 17:22:57 -08:00
|
|
|
export class CRBrowserContext extends BrowserContextBase {
|
2020-02-24 08:53:30 -08:00
|
|
|
readonly _browser: CRBrowser;
|
|
|
|
readonly _browserContextId: string | null;
|
2020-02-27 16:18:33 -08:00
|
|
|
readonly _evaluateOnNewDocumentSources: string[];
|
2020-02-24 08:53:30 -08:00
|
|
|
|
|
|
|
constructor(browser: CRBrowser, browserContextId: string | null, options: BrowserContextOptions) {
|
2020-03-05 17:22:57 -08:00
|
|
|
super(options);
|
2020-02-24 08:53:30 -08:00
|
|
|
this._browser = browser;
|
|
|
|
this._browserContextId = browserContextId;
|
2020-02-27 16:18:33 -08:00
|
|
|
this._evaluateOnNewDocumentSources = [];
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async _initialize() {
|
|
|
|
const entries = Object.entries(this._options.permissions || {});
|
|
|
|
await Promise.all(entries.map(entry => this.setPermissions(entry[0], entry[1])));
|
|
|
|
if (this._options.geolocation)
|
|
|
|
await this.setGeolocation(this._options.geolocation);
|
2020-03-04 17:58:12 -08:00
|
|
|
if (this._options.offline)
|
|
|
|
await this.setOffline(this._options.offline);
|
2020-03-06 13:50:42 -08:00
|
|
|
if (this._options.httpCredentials)
|
|
|
|
await this.setHTTPCredentials(this._options.httpCredentials);
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_existingPages(): Page[] {
|
|
|
|
const pages: Page[] = [];
|
|
|
|
for (const target of this._browser._allTargets()) {
|
|
|
|
if (target.context() === this && target._crPage)
|
|
|
|
pages.push(target._crPage.page());
|
|
|
|
}
|
|
|
|
return pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
async pages(): Promise<Page[]> {
|
|
|
|
const targets = this._browser._allTargets().filter(target => target.context() === this && target.type() === 'page');
|
2020-03-05 15:18:27 -08:00
|
|
|
const pages = await Promise.all(targets.map(target => target.pageOrError()));
|
|
|
|
return pages.filter(page => (page instanceof Page) && !page.isClosed()) as Page[];
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async newPage(): Promise<Page> {
|
|
|
|
assertBrowserContextIsNotOwned(this);
|
2020-03-06 15:11:03 -08:00
|
|
|
const { targetId } = await this._browser._session.send('Target.createTarget', { url: 'about:blank', browserContextId: this._browserContextId || undefined });
|
2020-02-24 08:53:30 -08:00
|
|
|
const target = this._browser._targets.get(targetId)!;
|
2020-03-05 15:18:27 -08:00
|
|
|
const result = await target.pageOrError();
|
|
|
|
if (result instanceof Page) {
|
|
|
|
if (result.isClosed())
|
|
|
|
throw new Error('Page has been closed.');
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
throw result;
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
async cookies(urls?: string | string[]): Promise<network.NetworkCookie[]> {
|
2020-03-06 15:11:03 -08:00
|
|
|
const { cookies } = await this._browser._session.send('Storage.getCookies', { browserContextId: this._browserContextId || undefined });
|
2020-02-24 08:53:30 -08:00
|
|
|
return network.filterCookies(cookies.map(c => {
|
|
|
|
const copy: any = { sameSite: 'None', ...c };
|
|
|
|
delete copy.size;
|
|
|
|
delete copy.priority;
|
2020-03-07 08:41:57 -08:00
|
|
|
delete copy.session;
|
2020-02-24 08:53:30 -08:00
|
|
|
return copy as network.NetworkCookie;
|
|
|
|
}), urls);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setCookies(cookies: network.SetNetworkCookieParam[]) {
|
2020-03-06 15:11:03 -08:00
|
|
|
await this._browser._session.send('Storage.setCookies', { cookies: network.rewriteCookies(cookies), browserContextId: this._browserContextId || undefined });
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async clearCookies() {
|
2020-03-06 15:11:03 -08:00
|
|
|
await this._browser._session.send('Storage.clearCookies', { browserContextId: this._browserContextId || undefined });
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async setPermissions(origin: string, permissions: string[]): Promise<void> {
|
|
|
|
const webPermissionToProtocol = new Map<string, Protocol.Browser.PermissionType>([
|
|
|
|
['geolocation', 'geolocation'],
|
|
|
|
['midi', 'midi'],
|
|
|
|
['notifications', 'notifications'],
|
|
|
|
['camera', 'videoCapture'],
|
|
|
|
['microphone', 'audioCapture'],
|
|
|
|
['background-sync', 'backgroundSync'],
|
|
|
|
['ambient-light-sensor', 'sensors'],
|
|
|
|
['accelerometer', 'sensors'],
|
|
|
|
['gyroscope', 'sensors'],
|
|
|
|
['magnetometer', 'sensors'],
|
|
|
|
['accessibility-events', 'accessibilityEvents'],
|
|
|
|
['clipboard-read', 'clipboardReadWrite'],
|
|
|
|
['clipboard-write', 'clipboardSanitizedWrite'],
|
|
|
|
['payment-handler', 'paymentHandler'],
|
|
|
|
// chrome-specific permissions we have.
|
|
|
|
['midi-sysex', 'midiSysex'],
|
|
|
|
]);
|
|
|
|
const filtered = permissions.map(permission => {
|
|
|
|
const protocolPermission = webPermissionToProtocol.get(permission);
|
|
|
|
if (!protocolPermission)
|
|
|
|
throw new Error('Unknown permission: ' + permission);
|
|
|
|
return protocolPermission;
|
|
|
|
});
|
2020-03-06 15:11:03 -08:00
|
|
|
await this._browser._session.send('Browser.grantPermissions', { origin, browserContextId: this._browserContextId || undefined, permissions: filtered });
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async clearPermissions() {
|
2020-03-06 15:11:03 -08:00
|
|
|
await this._browser._session.send('Browser.resetPermissions', { browserContextId: this._browserContextId || undefined });
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {
|
|
|
|
if (geolocation)
|
|
|
|
geolocation = verifyGeolocation(geolocation);
|
|
|
|
this._options.geolocation = geolocation || undefined;
|
|
|
|
for (const page of this._existingPages())
|
|
|
|
await (page._delegate as CRPage)._client.send('Emulation.setGeolocationOverride', geolocation || {});
|
|
|
|
}
|
|
|
|
|
2020-02-26 12:42:20 -08:00
|
|
|
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
|
|
|
|
this._options.extraHTTPHeaders = network.verifyHeaders(headers);
|
|
|
|
for (const page of this._existingPages())
|
|
|
|
await (page._delegate as CRPage).updateExtraHTTPHeaders();
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:58:12 -08:00
|
|
|
async setOffline(offline: boolean): Promise<void> {
|
|
|
|
this._options.offline = offline;
|
|
|
|
for (const page of this._existingPages())
|
|
|
|
await (page._delegate as CRPage)._networkManager.setOffline(offline);
|
|
|
|
}
|
|
|
|
|
2020-03-06 13:50:42 -08:00
|
|
|
async setHTTPCredentials(httpCredentials: types.Credentials | null): Promise<void> {
|
|
|
|
this._options.httpCredentials = httpCredentials || undefined;
|
|
|
|
for (const page of this._existingPages())
|
|
|
|
await (page._delegate as CRPage)._networkManager.authenticate(httpCredentials);
|
|
|
|
}
|
|
|
|
|
2020-02-27 17:42:14 -08:00
|
|
|
async addInitScript(script: Function | string | { path?: string, content?: string }, ...args: any[]) {
|
2020-02-28 15:34:07 -08:00
|
|
|
const source = await helper.evaluationScript(script, args);
|
2020-02-27 16:18:33 -08:00
|
|
|
this._evaluateOnNewDocumentSources.push(source);
|
|
|
|
for (const page of this._existingPages())
|
|
|
|
await (page._delegate as CRPage).evaluateOnNewDocument(source);
|
|
|
|
}
|
|
|
|
|
2020-03-03 16:46:06 -08:00
|
|
|
async exposeFunction(name: string, playwrightFunction: Function): Promise<void> {
|
|
|
|
for (const page of this._existingPages()) {
|
|
|
|
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);
|
|
|
|
for (const page of this._existingPages())
|
|
|
|
await (page._delegate as CRPage).exposeBinding(binding);
|
|
|
|
}
|
|
|
|
|
2020-02-24 08:53:30 -08:00
|
|
|
async close() {
|
|
|
|
if (this._closed)
|
|
|
|
return;
|
2020-03-09 16:53:33 -07: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-06 15:11:03 -08:00
|
|
|
await this._browser._session.send('Target.disposeBrowserContext', { browserContextId: this._browserContextId });
|
2020-02-24 08:53:30 -08:00
|
|
|
this._browser._contexts.delete(this._browserContextId);
|
2020-03-05 17:22:57 -08:00
|
|
|
this._didCloseInternal();
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
2020-03-02 13:58:22 -08:00
|
|
|
async backgroundPages(): Promise<Page[]> {
|
|
|
|
const targets = this._browser._allTargets().filter(target => target.context() === this && target.type() === 'background_page');
|
2020-03-05 15:18:27 -08:00
|
|
|
const pages = await Promise.all(targets.map(target => target.pageOrError()));
|
|
|
|
return pages.filter(page => (page instanceof Page) && !page.isClosed()) as Page[];
|
2020-02-24 14:35:51 -08:00
|
|
|
}
|
|
|
|
|
2020-03-02 13:58:22 -08:00
|
|
|
async createSession(page: Page): Promise<CRSession> {
|
2020-03-06 15:11:03 -08:00
|
|
|
const targetId = CRTarget.fromPage(page)._targetId;
|
|
|
|
const rootSession = await this._browser._clientRootSession();
|
|
|
|
const { sessionId } = await rootSession.send('Target.attachToTarget', { targetId, flatten: true });
|
|
|
|
return this._browser._connection.session(sessionId)!;
|
2020-02-24 14:35:51 -08:00
|
|
|
}
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|