2019-12-10 15:13:56 -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-01-13 15:39:13 -08:00
|
|
|
import { helper } from './helper';
|
2020-03-05 17:22:57 -08:00
|
|
|
import * as network from './network';
|
|
|
|
import { Page, PageBinding } from './page';
|
2020-02-13 14:18:18 -08:00
|
|
|
import { TimeoutSettings } from './timeoutSettings';
|
2020-03-05 17:22:57 -08:00
|
|
|
import * as types from './types';
|
|
|
|
import { Events } from './events';
|
2020-03-31 16:18:49 -07:00
|
|
|
import { ExtendedEventEmitter } from './extendedEventEmitter';
|
2020-04-02 17:56:14 -07:00
|
|
|
import { Download } from './download';
|
2019-12-11 07:18:43 -08:00
|
|
|
|
2019-12-18 12:23:33 -08:00
|
|
|
export type BrowserContextOptions = {
|
2020-03-17 18:21:02 -07:00
|
|
|
viewport?: types.Size | null,
|
2019-12-18 12:23:33 -08:00
|
|
|
ignoreHTTPSErrors?: boolean,
|
|
|
|
javaScriptEnabled?: boolean,
|
|
|
|
bypassCSP?: boolean,
|
|
|
|
userAgent?: string,
|
2020-02-13 13:37:59 -08:00
|
|
|
locale?: string,
|
2020-01-03 10:14:50 -08:00
|
|
|
timezoneId?: string,
|
2020-01-13 13:32:44 -08:00
|
|
|
geolocation?: types.Geolocation,
|
2020-03-17 15:32:50 -07:00
|
|
|
permissions?: string[],
|
2020-02-26 12:42:20 -08:00
|
|
|
extraHTTPHeaders?: network.Headers,
|
2020-03-04 17:58:12 -08:00
|
|
|
offline?: boolean,
|
2020-03-06 13:50:42 -08:00
|
|
|
httpCredentials?: types.Credentials,
|
2020-03-17 18:21:02 -07:00
|
|
|
deviceScaleFactor?: number,
|
|
|
|
isMobile?: boolean,
|
2020-04-02 17:56:14 -07:00
|
|
|
hasTouch?: boolean,
|
2020-04-06 19:49:33 -07:00
|
|
|
colorScheme?: types.ColorScheme,
|
2020-04-02 17:56:14 -07:00
|
|
|
acceptDownloads?: boolean
|
2019-12-18 12:23:33 -08:00
|
|
|
};
|
|
|
|
|
2020-02-24 08:53:30 -08:00
|
|
|
export interface BrowserContext {
|
|
|
|
setDefaultNavigationTimeout(timeout: number): void;
|
|
|
|
setDefaultTimeout(timeout: number): void;
|
2020-03-13 11:33:33 -07:00
|
|
|
pages(): Page[];
|
2020-02-24 08:53:30 -08:00
|
|
|
newPage(): Promise<Page>;
|
2020-03-06 08:24:32 -08:00
|
|
|
cookies(urls?: string | string[]): Promise<network.NetworkCookie[]>;
|
2020-03-12 17:32:33 -07:00
|
|
|
addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>;
|
2020-02-24 08:53:30 -08:00
|
|
|
clearCookies(): Promise<void>;
|
2020-03-17 15:32:50 -07:00
|
|
|
grantPermissions(permissions: string[], options?: { origin?: string }): Promise<void>;
|
2020-02-24 08:53:30 -08:00
|
|
|
clearPermissions(): Promise<void>;
|
|
|
|
setGeolocation(geolocation: types.Geolocation | null): Promise<void>;
|
2020-02-26 12:42:20 -08:00
|
|
|
setExtraHTTPHeaders(headers: network.Headers): Promise<void>;
|
2020-03-04 17:58:12 -08:00
|
|
|
setOffline(offline: boolean): Promise<void>;
|
2020-03-06 13:50:42 -08:00
|
|
|
setHTTPCredentials(httpCredentials: types.Credentials | null): Promise<void>;
|
2020-03-20 15:08:17 -07:00
|
|
|
addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any): Promise<void>;
|
2020-03-03 16:46:06 -08:00
|
|
|
exposeFunction(name: string, playwrightFunction: Function): Promise<void>;
|
2020-03-09 21:02:54 -07:00
|
|
|
route(url: types.URLMatch, handler: network.RouteHandler): Promise<void>;
|
2020-03-05 17:22:57 -08:00
|
|
|
waitForEvent(event: string, optionsOrPredicate?: Function | (types.TimeoutOptions & { predicate?: Function })): Promise<any>;
|
2020-02-24 08:53:30 -08:00
|
|
|
close(): Promise<void>;
|
2020-03-05 17:22:57 -08:00
|
|
|
}
|
2020-01-03 10:14:50 -08:00
|
|
|
|
2020-03-31 16:18:49 -07:00
|
|
|
export abstract class BrowserContextBase extends ExtendedEventEmitter implements BrowserContext {
|
2020-03-05 17:22:57 -08:00
|
|
|
readonly _timeoutSettings = new TimeoutSettings();
|
|
|
|
readonly _pageBindings = new Map<string, PageBinding>();
|
2020-02-24 08:53:30 -08:00
|
|
|
readonly _options: BrowserContextOptions;
|
2020-03-13 14:30:40 -07:00
|
|
|
readonly _routes: { url: types.URLMatch, handler: network.RouteHandler }[] = [];
|
2020-03-05 17:22:57 -08:00
|
|
|
_closed = false;
|
|
|
|
private readonly _closePromise: Promise<Error>;
|
|
|
|
private _closePromiseFulfill: ((error: Error) => void) | undefined;
|
2020-03-20 19:45:35 -07:00
|
|
|
readonly _permissions = new Map<string, string[]>();
|
2020-04-02 17:56:14 -07:00
|
|
|
readonly _downloads = new Set<Download>();
|
2020-03-05 17:22:57 -08:00
|
|
|
|
|
|
|
constructor(options: BrowserContextOptions) {
|
|
|
|
super();
|
|
|
|
this._options = options;
|
|
|
|
this._closePromise = new Promise(fulfill => this._closePromiseFulfill = fulfill);
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:18:49 -07:00
|
|
|
protected _abortPromiseForEvent(event: string) {
|
|
|
|
return event === Events.BrowserContext.Close ? super._abortPromiseForEvent(event) : this._closePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _timeoutForEvent(event: string): number {
|
|
|
|
return this._timeoutSettings.timeout();
|
|
|
|
}
|
|
|
|
|
2020-03-05 17:22:57 -08:00
|
|
|
_browserClosed() {
|
2020-03-13 11:33:33 -07:00
|
|
|
for (const page of this.pages())
|
2020-03-05 17:22:57 -08:00
|
|
|
page._didClose();
|
2020-04-02 17:56:14 -07:00
|
|
|
this._didCloseInternal(true);
|
2020-03-05 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:56:14 -07:00
|
|
|
async _didCloseInternal(omitDeleteDownloads = false) {
|
2020-03-05 17:22:57 -08:00
|
|
|
this._closed = true;
|
|
|
|
this.emit(Events.BrowserContext.Close);
|
|
|
|
this._closePromiseFulfill!(new Error('Context closed'));
|
2020-04-02 17:56:14 -07:00
|
|
|
if (!omitDeleteDownloads)
|
|
|
|
await Promise.all([...this._downloads].map(d => d.delete()));
|
|
|
|
this._downloads.clear();
|
2020-03-05 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// BrowserContext methods.
|
2020-03-13 11:33:33 -07:00
|
|
|
abstract pages(): Page[];
|
2020-03-05 17:22:57 -08:00
|
|
|
abstract newPage(): Promise<Page>;
|
|
|
|
abstract cookies(...urls: string[]): Promise<network.NetworkCookie[]>;
|
2020-03-12 17:32:33 -07:00
|
|
|
abstract addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>;
|
2020-03-05 17:22:57 -08:00
|
|
|
abstract clearCookies(): Promise<void>;
|
2020-03-17 15:32:50 -07:00
|
|
|
abstract _doGrantPermissions(origin: string, permissions: string[]): Promise<void>;
|
|
|
|
abstract _doClearPermissions(): Promise<void>;
|
2020-03-05 17:22:57 -08:00
|
|
|
abstract setGeolocation(geolocation: types.Geolocation | null): Promise<void>;
|
2020-03-06 13:50:42 -08:00
|
|
|
abstract setHTTPCredentials(httpCredentials: types.Credentials | null): Promise<void>;
|
2020-03-05 17:22:57 -08:00
|
|
|
abstract setExtraHTTPHeaders(headers: network.Headers): Promise<void>;
|
|
|
|
abstract setOffline(offline: boolean): Promise<void>;
|
2020-03-20 15:08:17 -07:00
|
|
|
abstract addInitScript(script: string | Function | { path?: string | undefined; content?: string | undefined; }, arg?: any): Promise<void>;
|
2020-03-05 17:22:57 -08:00
|
|
|
abstract exposeFunction(name: string, playwrightFunction: Function): Promise<void>;
|
2020-03-09 21:02:54 -07:00
|
|
|
abstract route(url: types.URLMatch, handler: network.RouteHandler): Promise<void>;
|
2020-03-05 17:22:57 -08:00
|
|
|
abstract close(): Promise<void>;
|
|
|
|
|
2020-03-17 15:32:50 -07:00
|
|
|
async grantPermissions(permissions: string[], options?: { origin?: string }) {
|
|
|
|
let origin = '*';
|
|
|
|
if (options && options.origin) {
|
|
|
|
const url = new URL(options.origin);
|
|
|
|
origin = url.origin;
|
|
|
|
}
|
|
|
|
const existing = new Set(this._permissions.get(origin) || []);
|
|
|
|
permissions.forEach(p => existing.add(p));
|
|
|
|
const list = [...existing.values()];
|
|
|
|
this._permissions.set(origin, list);
|
|
|
|
await this._doGrantPermissions(origin, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
async clearPermissions() {
|
|
|
|
this._permissions.clear();
|
|
|
|
await this._doClearPermissions();
|
|
|
|
}
|
|
|
|
|
2020-03-05 17:22:57 -08:00
|
|
|
setDefaultNavigationTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultTimeout(timeout);
|
|
|
|
}
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
2020-01-13 17:16:05 -08:00
|
|
|
|
2020-03-05 17:22:57 -08:00
|
|
|
export function assertBrowserContextIsNotOwned(context: BrowserContextBase) {
|
2020-03-13 11:33:33 -07:00
|
|
|
for (const page of context.pages()) {
|
2020-02-24 08:53:30 -08:00
|
|
|
if (page._ownedContext)
|
|
|
|
throw new Error('Please use browser.newContext() for multi-page scripts that share the context.');
|
2020-01-13 17:16:05 -08:00
|
|
|
}
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
2020-02-11 10:27:19 -08:00
|
|
|
|
2020-02-24 08:53:30 -08:00
|
|
|
export function validateBrowserContextOptions(options: BrowserContextOptions): BrowserContextOptions {
|
|
|
|
const result = { ...options };
|
|
|
|
if (!result.viewport && result.viewport !== null)
|
|
|
|
result.viewport = { width: 1280, height: 720 };
|
|
|
|
if (result.viewport)
|
|
|
|
result.viewport = { ...result.viewport };
|
|
|
|
if (result.geolocation)
|
|
|
|
result.geolocation = verifyGeolocation(result.geolocation);
|
2020-02-26 12:42:20 -08:00
|
|
|
if (result.extraHTTPHeaders)
|
|
|
|
result.extraHTTPHeaders = network.verifyHeaders(result.extraHTTPHeaders);
|
2020-02-24 08:53:30 -08:00
|
|
|
return result;
|
2019-12-10 15:13:56 -08:00
|
|
|
}
|
2020-01-13 15:39:13 -08:00
|
|
|
|
2020-02-24 08:53:30 -08:00
|
|
|
export function verifyGeolocation(geolocation: types.Geolocation): types.Geolocation {
|
2020-01-13 15:39:13 -08:00
|
|
|
const result = { ...geolocation };
|
|
|
|
result.accuracy = result.accuracy || 0;
|
|
|
|
const { longitude, latitude, accuracy } = result;
|
|
|
|
if (!helper.isNumber(longitude) || longitude < -180 || longitude > 180)
|
|
|
|
throw new Error(`Invalid longitude "${longitude}": precondition -180 <= LONGITUDE <= 180 failed.`);
|
|
|
|
if (!helper.isNumber(latitude) || latitude < -90 || latitude > 90)
|
|
|
|
throw new Error(`Invalid latitude "${latitude}": precondition -90 <= LATITUDE <= 90 failed.`);
|
|
|
|
if (!helper.isNumber(accuracy) || accuracy < 0)
|
|
|
|
throw new Error(`Invalid accuracy "${accuracy}": precondition 0 <= ACCURACY failed.`);
|
|
|
|
return result;
|
|
|
|
}
|