2019-12-19 16:53:24 -08: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-02-11 12:06:58 -08:00
|
|
|
import { Browser, createPageInNewContext } from '../browser';
|
2020-03-20 19:17:46 -07:00
|
|
|
import { assertBrowserContextIsNotOwned, BrowserContext, BrowserContextBase, BrowserContextOptions, validateBrowserContextOptions, verifyGeolocation } from '../browserContext';
|
2019-12-20 13:07:14 -08:00
|
|
|
import { Events } from '../events';
|
2020-03-05 15:18:27 -08:00
|
|
|
import { assert, helper, RegisteredListener } from '../helper';
|
2019-12-20 13:07:14 -08:00
|
|
|
import * as network from '../network';
|
2020-03-19 16:25:12 -07:00
|
|
|
import { Page, PageBinding } from '../page';
|
2020-01-07 16:13:49 -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';
|
2020-03-09 12:32:42 -07:00
|
|
|
import { ConnectionEvents, FFConnection } from './ffConnection';
|
2020-02-26 12:42:20 -08:00
|
|
|
import { headersArray } from './ffNetworkManager';
|
2020-03-05 17:22:57 -08:00
|
|
|
import { FFPage } from './ffPage';
|
|
|
|
import { Protocol } from './protocol';
|
2020-01-07 16:13:49 -08:00
|
|
|
|
2020-01-08 14:04:33 -08:00
|
|
|
export class FFBrowser extends platform.EventEmitter implements Browser {
|
2019-12-19 16:53:24 -08:00
|
|
|
_connection: FFConnection;
|
2020-03-09 12:32:42 -07:00
|
|
|
readonly _ffPages: Map<string, FFPage>;
|
2020-02-27 16:18:33 -08:00
|
|
|
readonly _defaultContext: FFBrowserContext;
|
2020-02-24 08:53:30 -08:00
|
|
|
readonly _contexts: Map<string, FFBrowserContext>;
|
2019-12-19 16:53:24 -08:00
|
|
|
private _eventListeners: RegisteredListener[];
|
2020-03-09 12:32:42 -07:00
|
|
|
readonly _firstPagePromise: Promise<void>;
|
|
|
|
private _firstPageCallback = () => {};
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-03-06 16:49:48 -08:00
|
|
|
static async connect(transport: ConnectionTransport, attachToDefaultContext: boolean, slowMo?: number): Promise<FFBrowser> {
|
2020-02-04 19:41:38 -08:00
|
|
|
const connection = new FFConnection(SlowMoTransport.wrap(transport, slowMo));
|
2020-02-06 12:41:43 -08:00
|
|
|
const browser = new FFBrowser(connection);
|
2020-03-06 16:49:48 -08:00
|
|
|
await connection.send('Browser.enable', { attachToDefaultContext });
|
2019-12-19 16:53:24 -08:00
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:41:43 -08:00
|
|
|
constructor(connection: FFConnection) {
|
2019-12-19 16:53:24 -08:00
|
|
|
super();
|
|
|
|
this._connection = connection;
|
2020-03-09 12:32:42 -07:00
|
|
|
this._ffPages = new Map();
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-02-24 08:53:30 -08:00
|
|
|
this._defaultContext = new FFBrowserContext(this, null, validateBrowserContextOptions({}));
|
2019-12-19 16:53:24 -08:00
|
|
|
this._contexts = new Map();
|
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(Events.Browser.Disconnected);
|
|
|
|
});
|
2019-12-19 16:53:24 -08:00
|
|
|
this._eventListeners = [
|
2020-03-06 16:49:48 -08:00
|
|
|
helper.addEventListener(this._connection, 'Browser.attachedToTarget', this._onAttachedToTarget.bind(this)),
|
|
|
|
helper.addEventListener(this._connection, 'Browser.detachedFromTarget', this._onDetachedFromTarget.bind(this)),
|
2019-12-19 16:53:24 -08:00
|
|
|
];
|
2020-03-09 12:32:42 -07:00
|
|
|
this._firstPagePromise = new Promise(f => this._firstPageCallback = f);
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
isConnected(): boolean {
|
|
|
|
return !this._connection._closed;
|
|
|
|
}
|
|
|
|
|
|
|
|
async newContext(options: BrowserContextOptions = {}): Promise<BrowserContext> {
|
2020-02-24 08:53:30 -08:00
|
|
|
options = validateBrowserContextOptions(options);
|
2020-02-18 09:16:32 -08:00
|
|
|
let viewport;
|
|
|
|
if (options.viewport) {
|
2020-03-03 17:28:31 -08:00
|
|
|
// TODO: remove isMobile/hasTouch from the protocol?
|
2020-03-17 18:21:02 -07:00
|
|
|
if (options.isMobile)
|
|
|
|
throw new Error('options.isMobile is not supported in Firefox');
|
|
|
|
if (options.hasTouch)
|
|
|
|
throw new Error('options.hasTouch is not supported in Firefox');
|
2020-02-18 09:16:32 -08:00
|
|
|
viewport = {
|
|
|
|
viewportSize: { width: options.viewport.width, height: options.viewport.height },
|
2020-03-17 18:21:02 -07:00
|
|
|
deviceScaleFactor: options.deviceScaleFactor || 1,
|
2020-03-03 17:28:31 -08:00
|
|
|
isMobile: false,
|
|
|
|
hasTouch: false,
|
2020-02-18 09:16:32 -08:00
|
|
|
};
|
|
|
|
} else if (options.viewport !== null) {
|
|
|
|
viewport = {
|
|
|
|
viewportSize: { width: 1280, height: 720 },
|
|
|
|
deviceScaleFactor: 1,
|
2020-03-03 17:28:31 -08:00
|
|
|
isMobile: false,
|
2020-02-18 09:16:32 -08:00
|
|
|
hasTouch: false,
|
|
|
|
};
|
|
|
|
}
|
2020-03-06 16:49:48 -08:00
|
|
|
const { browserContextId } = await this._connection.send('Browser.createBrowserContext', {
|
2020-02-11 18:52:01 -08:00
|
|
|
userAgent: options.userAgent,
|
|
|
|
bypassCSP: options.bypassCSP,
|
|
|
|
javaScriptDisabled: options.javaScriptEnabled === false ? true : undefined,
|
|
|
|
viewport,
|
2020-02-26 11:01:01 -08:00
|
|
|
removeOnDetach: true
|
2020-02-06 19:01:03 -08:00
|
|
|
});
|
2019-12-19 16:53:24 -08:00
|
|
|
// TODO: move ignoreHTTPSErrors to browser context level.
|
|
|
|
if (options.ignoreHTTPSErrors)
|
|
|
|
await this._connection.send('Browser.setIgnoreHTTPSErrors', { enabled: true });
|
2020-02-24 08:53:30 -08:00
|
|
|
const context = new FFBrowserContext(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-06 16:49:48 -08:00
|
|
|
_onDetachedFromTarget(payload: Protocol.Browser.detachedFromTargetPayload) {
|
2020-03-09 12:32:42 -07:00
|
|
|
const ffPage = this._ffPages.get(payload.targetId)!;
|
|
|
|
this._ffPages.delete(payload.targetId);
|
|
|
|
ffPage.didClose();
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-03-13 11:33:33 -07:00
|
|
|
_onAttachedToTarget(payload: Protocol.Browser.attachedToTargetPayload) {
|
2020-03-06 16:49:48 -08:00
|
|
|
const {targetId, browserContextId, openerId, type} = payload.targetInfo;
|
2020-03-09 12:32:42 -07:00
|
|
|
assert(type === 'page');
|
2020-03-06 16:49:48 -08:00
|
|
|
const context = browserContextId ? this._contexts.get(browserContextId)! : this._defaultContext;
|
2020-03-09 12:32:42 -07: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-03-13 11:33:33 -07:00
|
|
|
ffPage.pageOrError().then(async () => {
|
|
|
|
this._firstPageCallback();
|
2020-03-19 16:25:12 -07:00
|
|
|
const page = ffPage._page;
|
|
|
|
context.emit(Events.BrowserContext.Page, page);
|
2020-03-13 11:33:33 -07:00
|
|
|
if (!opener)
|
|
|
|
return;
|
|
|
|
const openerPage = await opener.pageOrError();
|
|
|
|
if (openerPage instanceof Page && !openerPage.isClosed())
|
2020-03-19 16:25:12 -07:00
|
|
|
openerPage.emit(Events.Page.Popup, page);
|
2020-03-13 11:33:33 -07:00
|
|
|
});
|
2020-02-06 19:01:03 -08:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
async close() {
|
2020-02-10 10:41:45 -08:00
|
|
|
await Promise.all(this.contexts().map(context => context.close()));
|
2019-12-19 16:53:24 -08:00
|
|
|
helper.removeEventListeners(this._eventListeners);
|
2020-02-06 12:41:43 -08:00
|
|
|
const disconnected = new Promise(f => this.once(Events.Browser.Disconnected, f));
|
|
|
|
this._connection.close();
|
2020-01-08 13:55:38 -08:00
|
|
|
await disconnected;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
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-03-05 17:22:57 -08:00
|
|
|
export class FFBrowserContext extends BrowserContextBase {
|
2020-02-24 08:53:30 -08:00
|
|
|
readonly _browser: FFBrowser;
|
|
|
|
readonly _browserContextId: string | null;
|
2020-02-27 16:18:33 -08:00
|
|
|
private readonly _evaluateOnNewDocumentSources: string[];
|
2020-02-24 08:53:30 -08:00
|
|
|
|
|
|
|
constructor(browser: FFBrowser, 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() {
|
2020-03-17 15:32:50 -07:00
|
|
|
if (this._options.permissions)
|
|
|
|
await this.grantPermissions(this._options.permissions);
|
2020-03-20 19:32:27 -07:00
|
|
|
if (this._options.extraHTTPHeaders || this._options.locale)
|
|
|
|
await this.setExtraHTTPHeaders(this._options.extraHTTPHeaders || {});
|
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
|
|
|
}
|
|
|
|
|
2020-03-09 12:32:42 -07:00
|
|
|
_ffPages(): FFPage[] {
|
|
|
|
return Array.from(this._browser._ffPages.values()).filter(ffPage => ffPage._browserContext === this);
|
|
|
|
}
|
|
|
|
|
2020-02-24 08:53:30 -08:00
|
|
|
setDefaultNavigationTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultNavigationTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultTimeout(timeout: number) {
|
|
|
|
this._timeoutSettings.setDefaultTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
2020-03-13 11:33:33 -07:00
|
|
|
pages(): Page[] {
|
|
|
|
return this._ffPages().map(ffPage => ffPage._initializedPage()).filter(pageOrNull => !!pageOrNull) as Page[];
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async newPage(): Promise<Page> {
|
|
|
|
assertBrowserContextIsNotOwned(this);
|
2020-03-09 12:32:42 -07:00
|
|
|
const { targetId } = await this._browser._connection.send('Browser.newPage', {
|
2020-02-24 08:53:30 -08:00
|
|
|
browserContextId: this._browserContextId || undefined
|
|
|
|
});
|
2020-03-09 12:32:42 -07:00
|
|
|
const ffPage = this._browser._ffPages.get(targetId)!;
|
|
|
|
const pageOrError = await ffPage.pageOrError();
|
|
|
|
if (pageOrError instanceof Page) {
|
|
|
|
if (pageOrError.isClosed())
|
2020-03-05 15:18:27 -08:00
|
|
|
throw new Error('Page has been closed.');
|
2020-03-09 12:32:42 -07:00
|
|
|
return pageOrError;
|
2020-03-05 15:18:27 -08:00
|
|
|
}
|
2020-03-09 12:32:42 -07:00
|
|
|
throw pageOrError;
|
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-02-24 08:53:30 -08: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 08:41:57 -08:00
|
|
|
delete copy.session;
|
2020-02-24 08:53:30 -08:00
|
|
|
return copy as network.NetworkCookie;
|
|
|
|
}), urls);
|
|
|
|
}
|
|
|
|
|
2020-03-12 17:32:33 -07:00
|
|
|
async addCookies(cookies: network.SetNetworkCookieParam[]) {
|
2020-02-24 08:53:30 -08: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 15:32:50 -07:00
|
|
|
async _doGrantPermissions(origin: string, permissions: string[]) {
|
|
|
|
const webPermissionToProtocol = new Map<string, 'geo' | 'desktop-notification' | 'persistent-storage' | 'push'>([
|
2020-02-24 08:53:30 -08:00
|
|
|
['geolocation', 'geo'],
|
2020-03-17 15:32:50 -07:00
|
|
|
['persistent-storage', 'persistent-storage'],
|
|
|
|
['push', 'push'],
|
|
|
|
['notifications', 'desktop-notification'],
|
2020-02-24 08:53:30 -08:00
|
|
|
]);
|
|
|
|
const filtered = permissions.map(permission => {
|
|
|
|
const protocolPermission = webPermissionToProtocol.get(permission);
|
|
|
|
if (!protocolPermission)
|
|
|
|
throw new Error('Unknown permission: ' + permission);
|
|
|
|
return protocolPermission;
|
|
|
|
});
|
2020-03-17 15:32:50 -07:00
|
|
|
await this._browser._connection.send('Browser.grantPermissions', { origin: origin, browserContextId: this._browserContextId || undefined, permissions: filtered});
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:32:50 -07:00
|
|
|
async _doClearPermissions() {
|
2020-02-24 08:53:30 -08:00
|
|
|
await this._browser._connection.send('Browser.resetPermissions', { browserContextId: this._browserContextId || undefined });
|
|
|
|
}
|
|
|
|
|
|
|
|
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {
|
2020-03-20 19:17:46 -07:00
|
|
|
if (geolocation)
|
|
|
|
geolocation = verifyGeolocation(geolocation);
|
|
|
|
this._options.geolocation = geolocation || undefined;
|
|
|
|
for (const page of this.pages())
|
|
|
|
await (page._delegate as FFPage)._setGeolocation(geolocation);
|
2020-02-24 08:53:30 -08:00
|
|
|
}
|
|
|
|
|
2020-02-26 12:42:20 -08:00
|
|
|
async setExtraHTTPHeaders(headers: network.Headers): Promise<void> {
|
|
|
|
this._options.extraHTTPHeaders = network.verifyHeaders(headers);
|
2020-03-20 19:32:27 -07: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 12:42:20 -08:00
|
|
|
}
|
|
|
|
|
2020-03-04 17:58:12 -08:00
|
|
|
async setOffline(offline: boolean): Promise<void> {
|
|
|
|
if (offline)
|
|
|
|
throw new Error('Offline mode is not implemented in Firefox');
|
|
|
|
this._options.offline = offline;
|
|
|
|
}
|
|
|
|
|
2020-03-06 13:50:42 -08: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 15:08:17 -07:00
|
|
|
async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any) {
|
|
|
|
const source = await helper.evaluationScript(script, arg);
|
2020-02-27 16:18:33 -08:00
|
|
|
this._evaluateOnNewDocumentSources.push(source);
|
|
|
|
await this._browser._connection.send('Browser.addScriptToEvaluateOnNewDocument', { browserContextId: this._browserContextId || undefined, script: source });
|
|
|
|
}
|
|
|
|
|
2020-03-03 16:46:06 -08:00
|
|
|
async exposeFunction(name: string, playwrightFunction: Function): Promise<void> {
|
2020-03-13 11:33:33 -07:00
|
|
|
for (const page of this.pages()) {
|
2020-03-03 16:46:06 -08: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);
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
2020-03-09 21:02:54 -07:00
|
|
|
async route(url: types.URLMatch, handler: network.RouteHandler): Promise<void> {
|
|
|
|
this._routes.push({ url, handler });
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
// TODO: update interception on the context if this is a first route.
|
|
|
|
}
|
|
|
|
|
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 16:49:48 -08:00
|
|
|
await this._browser._connection.send('Browser.removeBrowserContext', { 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
|
|
|
}
|
|
|
|
}
|