2020-06-25 16:05:36 -07: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as frames from './frame';
|
2020-07-13 16:03:24 -07:00
|
|
|
import { Page, BindingCall } from './page';
|
2020-06-25 16:05:36 -07:00
|
|
|
import * as types from '../../types';
|
|
|
|
import * as network from './network';
|
2020-06-26 12:28:27 -07:00
|
|
|
import { BrowserContextChannel, BrowserContextInitializer } from '../channels';
|
2020-06-25 16:05:36 -07:00
|
|
|
import { ChannelOwner } from './channelOwner';
|
|
|
|
import { helper } from '../../helper';
|
|
|
|
import { Browser } from './browser';
|
2020-06-26 12:28:27 -07:00
|
|
|
import { Events } from '../../events';
|
2020-06-26 21:22:03 -07:00
|
|
|
import { TimeoutSettings } from '../../timeoutSettings';
|
2020-07-13 16:03:24 -07:00
|
|
|
import { Waiter } from './waiter';
|
|
|
|
import { TimeoutError } from '../../errors';
|
2020-07-15 13:21:21 -07:00
|
|
|
import { headersObjectToArray } from '../serializers';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-06-26 12:28:27 -07:00
|
|
|
export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserContextInitializer> {
|
2020-06-25 16:05:36 -07:00
|
|
|
_pages = new Set<Page>();
|
|
|
|
private _routes: { url: types.URLMatch, handler: network.RouteHandler }[] = [];
|
2020-07-13 15:26:09 -07:00
|
|
|
readonly _browser: Browser | undefined;
|
2020-07-13 21:46:59 -07:00
|
|
|
readonly _browserName: string;
|
2020-06-26 11:51:47 -07:00
|
|
|
readonly _bindings = new Map<string, frames.FunctionWithSource>();
|
2020-06-26 21:22:03 -07:00
|
|
|
_timeoutSettings = new TimeoutSettings();
|
2020-06-29 16:37:38 -07:00
|
|
|
_ownerPage: Page | undefined;
|
2020-07-09 15:33:01 -07:00
|
|
|
private _isClosedOrClosing = false;
|
|
|
|
private _closedPromise: Promise<void>;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
|
|
|
static from(context: BrowserContextChannel): BrowserContext {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (context as any)._object;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static fromNullable(context: BrowserContextChannel | null): BrowserContext | null {
|
|
|
|
return context ? BrowserContext.from(context) : null;
|
|
|
|
}
|
|
|
|
|
2020-07-13 21:46:59 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: BrowserContextInitializer, browserName: string) {
|
2020-07-10 18:00:10 -07:00
|
|
|
super(parent, type, guid, initializer, true);
|
2020-07-13 21:46:59 -07:00
|
|
|
if (parent instanceof Browser)
|
2020-07-13 15:26:09 -07:00
|
|
|
this._browser = parent;
|
2020-07-13 21:46:59 -07:00
|
|
|
this._browserName = browserName;
|
2020-07-13 15:26:09 -07:00
|
|
|
|
2020-07-14 18:26:50 -07:00
|
|
|
this._channel.on('bindingCall', ({binding}) => this._onBinding(BindingCall.from(binding)));
|
2020-06-30 10:55:11 -07:00
|
|
|
this._channel.on('close', () => this._onClose());
|
2020-07-14 18:26:50 -07:00
|
|
|
this._channel.on('page', ({page}) => this._onPage(Page.from(page)));
|
2020-06-30 10:55:11 -07:00
|
|
|
this._channel.on('route', ({ route, request }) => this._onRoute(network.Route.from(route), network.Request.from(request)));
|
2020-07-09 15:33:01 -07:00
|
|
|
this._closedPromise = new Promise(f => this.once(Events.BrowserContext.Close, f));
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-06-26 12:28:27 -07:00
|
|
|
private _onPage(page: Page): void {
|
|
|
|
this._pages.add(page);
|
|
|
|
this.emit(Events.BrowserContext.Page, page);
|
|
|
|
}
|
2020-06-26 11:51:47 -07:00
|
|
|
|
|
|
|
_onRoute(route: network.Route, request: network.Request) {
|
|
|
|
for (const {url, handler} of this._routes) {
|
|
|
|
if (helper.urlMatches(request.url(), url)) {
|
|
|
|
handler(route, request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
route.continue();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _onBinding(bindingCall: BindingCall) {
|
2020-06-26 12:28:27 -07:00
|
|
|
const func = this._bindings.get(bindingCall._initializer.name);
|
2020-06-26 11:51:47 -07:00
|
|
|
if (!func)
|
|
|
|
return;
|
|
|
|
bindingCall.call(func);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultNavigationTimeout(timeout: number) {
|
2020-07-13 16:03:24 -07:00
|
|
|
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
2020-06-25 16:05:36 -07:00
|
|
|
this._channel.setDefaultNavigationTimeoutNoReply({ timeout });
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultTimeout(timeout: number) {
|
2020-06-26 21:22:03 -07:00
|
|
|
this._timeoutSettings.setDefaultTimeout(timeout);
|
2020-06-25 16:05:36 -07:00
|
|
|
this._channel.setDefaultTimeoutNoReply({ timeout });
|
|
|
|
}
|
|
|
|
|
|
|
|
pages(): Page[] {
|
|
|
|
return [...this._pages];
|
|
|
|
}
|
|
|
|
|
|
|
|
async newPage(): Promise<Page> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.newPage', async () => {
|
|
|
|
if (this._ownerPage)
|
|
|
|
throw new Error('Please use browser.newContext()');
|
|
|
|
return Page.from((await this._channel.newPage()).page);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async cookies(urls?: string | string[]): Promise<network.NetworkCookie[]> {
|
|
|
|
if (!urls)
|
|
|
|
urls = [];
|
|
|
|
if (urls && typeof urls === 'string')
|
|
|
|
urls = [ urls ];
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.cookies', async () => {
|
|
|
|
return (await this._channel.cookies({ urls: urls as string[] })).cookies;
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.addCookies', async () => {
|
|
|
|
await this._channel.addCookies({ cookies });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async clearCookies(): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.clearCookies', async () => {
|
|
|
|
await this._channel.clearCookies();
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async grantPermissions(permissions: string[], options?: { origin?: string }): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.grantPermissions', async () => {
|
|
|
|
await this._channel.grantPermissions({ permissions, ...options });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async clearPermissions(): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.clearPermissions', async () => {
|
|
|
|
await this._channel.clearPermissions();
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.setGeolocation', async () => {
|
|
|
|
await this._channel.setGeolocation({ geolocation });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async setExtraHTTPHeaders(headers: types.Headers): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.setExtraHTTPHeaders', async () => {
|
|
|
|
await this._channel.setExtraHTTPHeaders({ headers: headersObjectToArray(headers) });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async setOffline(offline: boolean): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.setOffline', async () => {
|
|
|
|
await this._channel.setOffline({ offline });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async setHTTPCredentials(httpCredentials: types.Credentials | null): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.setHTTPCredentials', async () => {
|
|
|
|
await this._channel.setHTTPCredentials({ httpCredentials });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.addInitScript', async () => {
|
|
|
|
const source = await helper.evaluationScript(script, arg);
|
|
|
|
await this._channel.addInitScript({ source });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-06-26 11:51:47 -07:00
|
|
|
async exposeBinding(name: string, binding: frames.FunctionWithSource): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.exposeBinding', async () => {
|
|
|
|
for (const page of this.pages()) {
|
|
|
|
if (page._bindings.has(name))
|
|
|
|
throw new Error(`Function "${name}" has been already registered in one of the pages`);
|
|
|
|
}
|
|
|
|
if (this._bindings.has(name))
|
|
|
|
throw new Error(`Function "${name}" has been already registered`);
|
|
|
|
this._bindings.set(name, binding);
|
|
|
|
await this._channel.exposeBinding({ name });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async exposeFunction(name: string, playwrightFunction: Function): Promise<void> {
|
|
|
|
await this.exposeBinding(name, (source, ...args) => playwrightFunction(...args));
|
|
|
|
}
|
|
|
|
|
|
|
|
async route(url: types.URLMatch, handler: network.RouteHandler): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.route', async () => {
|
|
|
|
this._routes.push({ url, handler });
|
|
|
|
if (this._routes.length === 1)
|
|
|
|
await this._channel.setNetworkInterceptionEnabled({ enabled: true });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async unroute(url: types.URLMatch, handler?: network.RouteHandler): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.unroute', async () => {
|
|
|
|
this._routes = this._routes.filter(route => route.url !== url || (handler && route.handler !== handler));
|
|
|
|
if (this._routes.length === 0)
|
|
|
|
await this._channel.setNetworkInterceptionEnabled({ enabled: false });
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-13 16:03:24 -07:00
|
|
|
async waitForEvent(event: string, optionsOrPredicate: types.WaitForEventOptions = {}): Promise<any> {
|
|
|
|
const timeout = this._timeoutSettings.timeout(optionsOrPredicate instanceof Function ? {} : optionsOrPredicate);
|
|
|
|
const predicate = optionsOrPredicate instanceof Function ? optionsOrPredicate : optionsOrPredicate.predicate;
|
|
|
|
const waiter = new Waiter();
|
|
|
|
waiter.rejectOnTimeout(timeout, new TimeoutError(`Timeout while waiting for event "${event}"`));
|
|
|
|
if (event !== Events.BrowserContext.Close)
|
|
|
|
waiter.rejectOnEvent(this, Events.BrowserContext.Close, new Error('Context closed'));
|
|
|
|
const result = await waiter.waitForEvent(this, event, predicate as any);
|
|
|
|
waiter.dispose();
|
2020-06-26 21:22:03 -07:00
|
|
|
return result;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-06-30 10:55:11 -07:00
|
|
|
private async _onClose() {
|
2020-07-09 15:33:01 -07:00
|
|
|
this._isClosedOrClosing = true;
|
2020-06-26 17:24:21 -07:00
|
|
|
if (this._browser)
|
|
|
|
this._browser._contexts.delete(this);
|
2020-06-26 12:28:27 -07:00
|
|
|
this.emit(Events.BrowserContext.Close);
|
2020-07-10 15:11:47 -07:00
|
|
|
this._dispose();
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-06-30 10:55:11 -07:00
|
|
|
|
|
|
|
async close(): Promise<void> {
|
2020-07-16 14:32:21 -07:00
|
|
|
return this._wrapApiCall('browserContext.close', async () => {
|
|
|
|
if (!this._isClosedOrClosing) {
|
|
|
|
this._isClosedOrClosing = true;
|
|
|
|
await this._channel.close();
|
|
|
|
}
|
|
|
|
await this._closedPromise;
|
|
|
|
});
|
2020-06-30 10:55:11 -07:00
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|