2019-11-19 03:18:28 +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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import * as childProcess from 'child_process';
|
|
|
|
|
import { EventEmitter } from 'events';
|
2019-11-21 00:16:57 +01:00
|
|
|
import { Events } from './events';
|
2019-11-19 03:18:28 +01:00
|
|
|
import { assert, helper } from '../helper';
|
|
|
|
|
import { BrowserContext } from './BrowserContext';
|
2019-11-22 01:54:10 +01:00
|
|
|
import { Connection, ConnectionEvents, CDPSession } from './Connection';
|
2019-12-09 22:08:21 +01:00
|
|
|
import { Page } from '../page';
|
2019-11-19 03:18:28 +01:00
|
|
|
import { Target } from './Target';
|
|
|
|
|
import { Protocol } from './protocol';
|
2019-11-22 01:54:10 +01:00
|
|
|
import { Chromium } from './features/chromium';
|
2019-12-06 20:33:24 +01:00
|
|
|
import * as types from '../types';
|
2019-12-09 22:08:21 +01:00
|
|
|
import { FrameManager } from './FrameManager';
|
2019-11-19 03:18:28 +01:00
|
|
|
|
|
|
|
|
export class Browser extends EventEmitter {
|
|
|
|
|
private _ignoreHTTPSErrors: boolean;
|
2019-12-06 20:33:24 +01:00
|
|
|
private _defaultViewport: types.Viewport;
|
2019-11-19 03:18:28 +01:00
|
|
|
private _process: childProcess.ChildProcess;
|
2019-12-05 01:12:43 +01:00
|
|
|
_connection: Connection;
|
2019-11-26 20:23:13 +01:00
|
|
|
_client: CDPSession;
|
2019-11-19 03:18:28 +01:00
|
|
|
private _closeCallback: () => Promise<void>;
|
|
|
|
|
private _defaultContext: BrowserContext;
|
|
|
|
|
private _contexts = new Map<string, BrowserContext>();
|
|
|
|
|
_targets = new Map<string, Target>();
|
2019-11-22 01:54:10 +01:00
|
|
|
readonly chromium: Chromium;
|
2019-11-19 03:18:28 +01:00
|
|
|
|
|
|
|
|
static async create(
|
|
|
|
|
connection: Connection,
|
|
|
|
|
contextIds: string[],
|
|
|
|
|
ignoreHTTPSErrors: boolean,
|
2019-12-06 20:33:24 +01:00
|
|
|
defaultViewport: types.Viewport | null,
|
2019-11-19 03:18:28 +01:00
|
|
|
process: childProcess.ChildProcess | null,
|
|
|
|
|
closeCallback?: (() => Promise<void>)) {
|
2019-11-22 19:05:32 +01:00
|
|
|
const browser = new Browser(connection, contextIds, ignoreHTTPSErrors, defaultViewport, process, closeCallback);
|
|
|
|
|
await connection.rootSession.send('Target.setDiscoverTargets', { discover: true });
|
2019-11-19 03:18:28 +01:00
|
|
|
return browser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
connection: Connection,
|
|
|
|
|
contextIds: string[],
|
|
|
|
|
ignoreHTTPSErrors: boolean,
|
2019-12-06 20:33:24 +01:00
|
|
|
defaultViewport: types.Viewport | null,
|
2019-11-19 03:18:28 +01:00
|
|
|
process: childProcess.ChildProcess | null,
|
|
|
|
|
closeCallback?: (() => Promise<void>)) {
|
|
|
|
|
super();
|
2019-11-22 01:54:10 +01:00
|
|
|
this._connection = connection;
|
2019-11-22 19:05:32 +01:00
|
|
|
this._client = connection.rootSession;
|
2019-11-19 03:18:28 +01:00
|
|
|
this._ignoreHTTPSErrors = ignoreHTTPSErrors;
|
|
|
|
|
this._defaultViewport = defaultViewport;
|
|
|
|
|
this._process = process;
|
|
|
|
|
this._closeCallback = closeCallback || (() => Promise.resolve());
|
2019-12-05 01:12:43 +01:00
|
|
|
this.chromium = new Chromium(this);
|
2019-11-19 03:18:28 +01:00
|
|
|
|
2019-11-22 01:54:10 +01:00
|
|
|
this._defaultContext = new BrowserContext(this._client, this, null);
|
2019-11-19 03:18:28 +01:00
|
|
|
for (const contextId of contextIds)
|
2019-11-22 01:54:10 +01:00
|
|
|
this._contexts.set(contextId, new BrowserContext(this._client, this, contextId));
|
2019-11-19 03:18:28 +01:00
|
|
|
|
|
|
|
|
this._connection.on(ConnectionEvents.Disconnected, () => this.emit(Events.Browser.Disconnected));
|
2019-11-22 01:54:10 +01:00
|
|
|
this._client.on('Target.targetCreated', this._targetCreated.bind(this));
|
|
|
|
|
this._client.on('Target.targetDestroyed', this._targetDestroyed.bind(this));
|
|
|
|
|
this._client.on('Target.targetInfoChanged', this._targetInfoChanged.bind(this));
|
2019-11-19 03:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process(): childProcess.ChildProcess | null {
|
|
|
|
|
return this._process;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createIncognitoBrowserContext(): Promise<BrowserContext> {
|
2019-11-22 01:54:10 +01:00
|
|
|
const {browserContextId} = await this._client.send('Target.createBrowserContext');
|
|
|
|
|
const context = new BrowserContext(this._client, this, browserContextId);
|
2019-11-19 03:18:28 +01:00
|
|
|
this._contexts.set(browserContextId, context);
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
browserContexts(): BrowserContext[] {
|
|
|
|
|
return [this._defaultContext, ...Array.from(this._contexts.values())];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defaultBrowserContext(): BrowserContext {
|
|
|
|
|
return this._defaultContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _disposeContext(contextId: string | null) {
|
2019-11-22 01:54:10 +01:00
|
|
|
await this._client.send('Target.disposeBrowserContext', {browserContextId: contextId || undefined});
|
2019-11-19 03:18:28 +01:00
|
|
|
this._contexts.delete(contextId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _targetCreated(event: Protocol.Target.targetCreatedPayload) {
|
|
|
|
|
const targetInfo = event.targetInfo;
|
|
|
|
|
const {browserContextId} = targetInfo;
|
|
|
|
|
const context = (browserContextId && this._contexts.has(browserContextId)) ? this._contexts.get(browserContextId) : this._defaultContext;
|
|
|
|
|
|
2019-12-06 20:33:24 +01:00
|
|
|
const target = new Target(targetInfo, context, () => this._connection.createSession(targetInfo), this._ignoreHTTPSErrors, this._defaultViewport);
|
2019-11-19 03:18:28 +01:00
|
|
|
assert(!this._targets.has(event.targetInfo.targetId), 'Target should not exist before targetCreated');
|
|
|
|
|
this._targets.set(event.targetInfo.targetId, target);
|
|
|
|
|
|
2019-12-05 01:12:43 +01:00
|
|
|
if (await target._initializedPromise)
|
|
|
|
|
this.chromium.emit(Events.Chromium.TargetCreated, target);
|
2019-11-19 03:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _targetDestroyed(event: { targetId: string; }) {
|
|
|
|
|
const target = this._targets.get(event.targetId);
|
|
|
|
|
target._initializedCallback(false);
|
|
|
|
|
this._targets.delete(event.targetId);
|
2019-12-05 23:11:48 +01:00
|
|
|
target._didClose();
|
2019-12-05 01:12:43 +01:00
|
|
|
if (await target._initializedPromise)
|
|
|
|
|
this.chromium.emit(Events.Chromium.TargetDestroyed, target);
|
2019-11-19 03:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_targetInfoChanged(event: Protocol.Target.targetInfoChangedPayload) {
|
|
|
|
|
const target = this._targets.get(event.targetInfo.targetId);
|
|
|
|
|
assert(target, 'target should exist before targetInfoChanged');
|
|
|
|
|
const previousURL = target.url();
|
|
|
|
|
const wasInitialized = target._isInitialized;
|
|
|
|
|
target._targetInfoChanged(event.targetInfo);
|
2019-12-05 01:12:43 +01:00
|
|
|
if (wasInitialized && previousURL !== target.url())
|
|
|
|
|
this.chromium.emit(Events.Chromium.TargetChanged, target);
|
2019-11-19 03:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-09 22:08:21 +01:00
|
|
|
async newPage(): Promise<Page<Browser, BrowserContext>> {
|
2019-11-19 03:18:28 +01:00
|
|
|
return this._defaultContext.newPage();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 22:08:21 +01:00
|
|
|
async _createPageInContext(contextId: string | null): Promise<Page<Browser, BrowserContext>> {
|
2019-11-22 19:05:32 +01:00
|
|
|
const { targetId } = await this._client.send('Target.createTarget', { url: 'about:blank', browserContextId: contextId || undefined });
|
2019-11-26 20:23:13 +01:00
|
|
|
const target = this._targets.get(targetId);
|
2019-11-19 03:18:28 +01:00
|
|
|
assert(await target._initializedPromise, 'Failed to create target for page');
|
|
|
|
|
const page = await target.page();
|
|
|
|
|
return page;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 22:08:21 +01:00
|
|
|
async _closePage(page: Page<Browser, BrowserContext>) {
|
2019-12-05 23:11:48 +01:00
|
|
|
await this._client.send('Target.closeTarget', { targetId: Target.fromPage(page)._targetId });
|
2019-11-22 19:05:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-05 01:12:43 +01:00
|
|
|
_allTargets(): Target[] {
|
2019-11-19 03:18:28 +01:00
|
|
|
return Array.from(this._targets.values()).filter(target => target._isInitialized);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 22:08:21 +01:00
|
|
|
async _pages(context: BrowserContext): Promise<Page<Browser, BrowserContext>[]> {
|
2019-12-05 23:11:48 +01:00
|
|
|
const targets = this._allTargets().filter(target => target.browserContext() === context && target.type() === 'page');
|
|
|
|
|
const pages = await Promise.all(targets.map(target => target.page()));
|
|
|
|
|
return pages.filter(page => !!page);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 22:08:21 +01:00
|
|
|
async _activatePage(page: Page<Browser, BrowserContext>) {
|
|
|
|
|
await (page._delegate as FrameManager)._client.send('Target.activateTarget', {targetId: Target.fromPage(page)._targetId});
|
2019-12-05 23:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-05 01:12:43 +01:00
|
|
|
async _waitForTarget(predicate: (arg0: Target) => boolean, options: { timeout?: number; } | undefined = {}): Promise<Target> {
|
2019-11-19 03:18:28 +01:00
|
|
|
const {
|
|
|
|
|
timeout = 30000
|
|
|
|
|
} = options;
|
2019-12-05 01:12:43 +01:00
|
|
|
const existingTarget = this._allTargets().find(predicate);
|
2019-11-19 03:18:28 +01:00
|
|
|
if (existingTarget)
|
|
|
|
|
return existingTarget;
|
|
|
|
|
let resolve;
|
|
|
|
|
const targetPromise = new Promise<Target>(x => resolve = x);
|
2019-12-05 01:12:43 +01:00
|
|
|
this.chromium.on(Events.Chromium.TargetCreated, check);
|
|
|
|
|
this.chromium.on(Events.Chromium.TargetChanged, check);
|
2019-11-19 03:18:28 +01:00
|
|
|
try {
|
|
|
|
|
if (!timeout)
|
|
|
|
|
return await targetPromise;
|
|
|
|
|
return await helper.waitWithTimeout(targetPromise, 'target', timeout);
|
|
|
|
|
} finally {
|
2019-12-05 01:12:43 +01:00
|
|
|
this.chromium.removeListener(Events.Chromium.TargetCreated, check);
|
|
|
|
|
this.chromium.removeListener(Events.Chromium.TargetChanged, check);
|
2019-11-19 03:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function check(target: Target) {
|
|
|
|
|
if (predicate(target))
|
|
|
|
|
resolve(target);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 22:08:21 +01:00
|
|
|
async pages(): Promise<Page<Browser, BrowserContext>[]> {
|
2019-11-19 03:18:28 +01:00
|
|
|
const contextPages = await Promise.all(this.browserContexts().map(context => context.pages()));
|
|
|
|
|
// Flatten array.
|
|
|
|
|
return contextPages.reduce((acc, x) => acc.concat(x), []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async version(): Promise<string> {
|
|
|
|
|
const version = await this._getVersion();
|
|
|
|
|
return version.product;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async userAgent(): Promise<string> {
|
|
|
|
|
const version = await this._getVersion();
|
|
|
|
|
return version.userAgent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async close() {
|
|
|
|
|
await this._closeCallback.call(null);
|
|
|
|
|
this.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
|
this._connection.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isConnected(): boolean {
|
|
|
|
|
return !this._connection._closed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_getVersion(): Promise<any> {
|
2019-11-22 19:05:32 +01:00
|
|
|
return this._client.send('Browser.getVersion');
|
2019-11-19 03:18:28 +01:00
|
|
|
}
|
|
|
|
|
}
|