2019-12-11 00:13:56 +01: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-03-04 01:46:06 +01:00
|
|
|
import { Page, PageBinding } from './page';
|
2019-12-11 00:13:56 +01:00
|
|
|
import * as network from './network';
|
2019-12-18 21:23:33 +01:00
|
|
|
import * as types from './types';
|
2020-01-14 00:39:13 +01:00
|
|
|
import { helper } from './helper';
|
2020-02-13 23:18:18 +01:00
|
|
|
import { TimeoutSettings } from './timeoutSettings';
|
2019-12-11 16:18:43 +01:00
|
|
|
|
2019-12-18 21:23:33 +01:00
|
|
|
export type BrowserContextOptions = {
|
|
|
|
|
viewport?: types.Viewport | null,
|
|
|
|
|
ignoreHTTPSErrors?: boolean,
|
|
|
|
|
javaScriptEnabled?: boolean,
|
|
|
|
|
bypassCSP?: boolean,
|
|
|
|
|
userAgent?: string,
|
2020-02-13 22:37:59 +01:00
|
|
|
locale?: string,
|
2020-01-03 19:14:50 +01:00
|
|
|
timezoneId?: string,
|
2020-01-13 22:32:44 +01:00
|
|
|
geolocation?: types.Geolocation,
|
2020-02-26 21:42:20 +01:00
|
|
|
permissions?: { [key: string]: string[] },
|
|
|
|
|
extraHTTPHeaders?: network.Headers,
|
2020-03-05 02:58:12 +01:00
|
|
|
offline?: boolean,
|
2019-12-18 21:23:33 +01:00
|
|
|
};
|
|
|
|
|
|
2020-02-24 17:53:30 +01:00
|
|
|
export interface BrowserContext {
|
|
|
|
|
setDefaultNavigationTimeout(timeout: number): void;
|
|
|
|
|
setDefaultTimeout(timeout: number): void;
|
|
|
|
|
pages(): Promise<Page[]>;
|
|
|
|
|
newPage(): Promise<Page>;
|
|
|
|
|
cookies(...urls: string[]): Promise<network.NetworkCookie[]>;
|
|
|
|
|
setCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>;
|
|
|
|
|
clearCookies(): Promise<void>;
|
|
|
|
|
setPermissions(origin: string, permissions: string[]): Promise<void>;
|
|
|
|
|
clearPermissions(): Promise<void>;
|
|
|
|
|
setGeolocation(geolocation: types.Geolocation | null): Promise<void>;
|
2020-02-26 21:42:20 +01:00
|
|
|
setExtraHTTPHeaders(headers: network.Headers): Promise<void>;
|
2020-03-05 02:58:12 +01:00
|
|
|
setOffline(offline: boolean): Promise<void>;
|
2020-02-28 02:42:14 +01:00
|
|
|
addInitScript(script: Function | string | { path?: string, content?: string }, ...args: any[]): Promise<void>;
|
2020-03-04 01:46:06 +01:00
|
|
|
exposeFunction(name: string, playwrightFunction: Function): Promise<void>;
|
2020-02-24 17:53:30 +01:00
|
|
|
close(): Promise<void>;
|
2020-01-03 19:14:50 +01:00
|
|
|
|
2020-02-24 17:53:30 +01:00
|
|
|
_existingPages(): Page[];
|
|
|
|
|
readonly _timeoutSettings: TimeoutSettings;
|
|
|
|
|
readonly _options: BrowserContextOptions;
|
2020-03-04 01:46:06 +01:00
|
|
|
readonly _pageBindings: Map<string, PageBinding>;
|
2020-02-24 17:53:30 +01:00
|
|
|
}
|
2020-01-14 02:16:05 +01:00
|
|
|
|
2020-02-24 17:53:30 +01:00
|
|
|
export function assertBrowserContextIsNotOwned(context: BrowserContext) {
|
|
|
|
|
const pages = context._existingPages();
|
|
|
|
|
for (const page of pages) {
|
|
|
|
|
if (page._ownedContext)
|
|
|
|
|
throw new Error('Please use browser.newContext() for multi-page scripts that share the context.');
|
2020-01-14 02:16:05 +01:00
|
|
|
}
|
2020-02-24 17:53:30 +01:00
|
|
|
}
|
2020-02-11 19:27:19 +01:00
|
|
|
|
2020-02-24 17:53:30 +01: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 21:42:20 +01:00
|
|
|
if (result.extraHTTPHeaders)
|
|
|
|
|
result.extraHTTPHeaders = network.verifyHeaders(result.extraHTTPHeaders);
|
2020-02-24 17:53:30 +01:00
|
|
|
return result;
|
2019-12-11 00:13:56 +01:00
|
|
|
}
|
2020-01-14 00:39:13 +01:00
|
|
|
|
2020-02-24 17:53:30 +01:00
|
|
|
export function verifyGeolocation(geolocation: types.Geolocation): types.Geolocation {
|
2020-01-14 00:39:13 +01: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;
|
|
|
|
|
}
|