2019-11-18 18:18:28 -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.
|
|
|
|
*/
|
|
|
|
|
2019-11-27 12:38:26 -08:00
|
|
|
import * as types from './types';
|
2019-11-27 12:39:53 -08:00
|
|
|
import * as js from './javascript';
|
2019-11-27 16:02:31 -08:00
|
|
|
import * as dom from './dom';
|
2019-11-27 12:44:12 -08:00
|
|
|
import * as network from './network';
|
2019-12-11 07:17:32 -08:00
|
|
|
import { helper, assert, RegisteredListener } from './helper';
|
2019-12-12 20:54:40 -08:00
|
|
|
import { TimeoutError } from './errors';
|
2019-12-11 07:17:32 -08:00
|
|
|
import { Events } from './events';
|
2019-12-11 12:36:42 -08:00
|
|
|
import { Page } from './page';
|
2019-12-11 13:51:03 -08:00
|
|
|
import { ConsoleMessage } from './console';
|
2020-01-07 11:55:24 -08:00
|
|
|
import * as platform from './platform';
|
2019-11-18 18:18:28 -08:00
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
type ContextType = 'main' | 'utility';
|
|
|
|
type ContextData = {
|
|
|
|
contextPromise: Promise<dom.FrameExecutionContext>;
|
|
|
|
contextResolveCallback: (c: dom.FrameExecutionContext) => void;
|
|
|
|
context: dom.FrameExecutionContext | null;
|
2019-12-03 10:51:41 -08:00
|
|
|
rerunnableTasks: Set<RerunnableTask>;
|
2019-11-26 15:37:25 -08:00
|
|
|
};
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
export type GotoOptions = types.NavigateOptions & {
|
2019-11-26 16:19:43 -08:00
|
|
|
referer?: string,
|
|
|
|
};
|
2019-12-16 22:02:33 -08:00
|
|
|
export type GotoResult = {
|
|
|
|
newDocumentId?: string,
|
|
|
|
};
|
2019-11-26 16:19:43 -08:00
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
const kLifecycleEvents: Set<types.LifecycleEvent> = new Set(['commit', 'load', 'domcontentloaded', 'networkidle0', 'networkidle2']);
|
2019-12-09 16:34:42 -08:00
|
|
|
|
2020-01-27 16:51:52 -08:00
|
|
|
type ConsoleTagHandler = () => void;
|
2019-12-14 19:13:22 -08:00
|
|
|
|
2019-12-16 15:56:11 -08:00
|
|
|
export class FrameManager {
|
|
|
|
private _page: Page;
|
|
|
|
private _frames = new Map<string, Frame>();
|
|
|
|
private _mainFrame: Frame;
|
2020-02-10 18:35:47 -08:00
|
|
|
readonly _lifecycleWatchers = new Set<() => void>();
|
2020-01-27 16:51:52 -08:00
|
|
|
readonly _consoleMessageTags = new Map<string, ConsoleTagHandler>();
|
2020-03-05 14:47:04 -08:00
|
|
|
private _pendingNavigationBarriers = new Set<PendingNavigationBarrier>();
|
2019-12-16 15:56:11 -08:00
|
|
|
|
|
|
|
constructor(page: Page) {
|
|
|
|
this._page = page;
|
2020-01-13 13:33:25 -08:00
|
|
|
this._mainFrame = undefined as any as Frame;
|
2019-12-16 15:56:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mainFrame(): Frame {
|
|
|
|
return this._mainFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
frames() {
|
|
|
|
const frames: Frame[] = [];
|
|
|
|
collect(this._mainFrame);
|
|
|
|
return frames;
|
|
|
|
|
|
|
|
function collect(frame: Frame) {
|
|
|
|
frames.push(frame);
|
|
|
|
for (const subframe of frame.childFrames())
|
|
|
|
collect(subframe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
frame(frameId: string): Frame | null {
|
|
|
|
return this._frames.get(frameId) || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
frameAttached(frameId: string, parentFrameId: string | null | undefined): Frame {
|
2020-01-13 13:33:25 -08:00
|
|
|
const parentFrame = parentFrameId ? this._frames.get(parentFrameId)! : null;
|
2019-12-16 15:56:11 -08:00
|
|
|
if (!parentFrame) {
|
|
|
|
if (this._mainFrame) {
|
|
|
|
// Update frame id to retain frame identity on cross-process navigation.
|
|
|
|
this._frames.delete(this._mainFrame._id);
|
|
|
|
this._mainFrame._id = frameId;
|
|
|
|
} else {
|
|
|
|
assert(!this._frames.has(frameId));
|
|
|
|
this._mainFrame = new Frame(this._page, frameId, parentFrame);
|
|
|
|
}
|
|
|
|
this._frames.set(frameId, this._mainFrame);
|
|
|
|
return this._mainFrame;
|
|
|
|
} else {
|
|
|
|
assert(!this._frames.has(frameId));
|
|
|
|
const frame = new Frame(this._page, frameId, parentFrame);
|
|
|
|
this._frames.set(frameId, frame);
|
|
|
|
this._page.emit(Events.Page.FrameAttached, frame);
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 08:19:31 -08:00
|
|
|
async waitForNavigationsCreatedBy<T>(action: () => Promise<T>, options?: types.NavigatingActionWaitOptions, input?: boolean): Promise<T> {
|
2020-03-06 14:32:15 -08:00
|
|
|
if (options && options.waitUntil === 'nowait')
|
|
|
|
return action();
|
2020-03-05 14:47:04 -08:00
|
|
|
const barrier = new PendingNavigationBarrier(options);
|
|
|
|
this._pendingNavigationBarriers.add(barrier);
|
2020-03-04 19:15:01 -08:00
|
|
|
try {
|
|
|
|
const result = await action();
|
2020-03-07 08:19:31 -08:00
|
|
|
if (input)
|
|
|
|
await this._page._delegate.inputActionEpilogue();
|
2020-03-05 14:47:04 -08:00
|
|
|
await barrier.waitFor();
|
|
|
|
// Resolve in the next task, after all waitForNavigations.
|
2020-03-04 19:15:01 -08:00
|
|
|
await new Promise(platform.makeWaitForNextTask());
|
|
|
|
return result;
|
|
|
|
} finally {
|
2020-03-05 14:47:04 -08:00
|
|
|
this._pendingNavigationBarriers.delete(barrier);
|
2020-03-04 19:15:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:47:04 -08:00
|
|
|
frameWillPotentiallyRequestNavigation() {
|
|
|
|
for (const barrier of this._pendingNavigationBarriers)
|
|
|
|
barrier.retain();
|
2020-03-04 19:15:01 -08:00
|
|
|
}
|
|
|
|
|
2020-03-05 14:47:04 -08:00
|
|
|
frameDidPotentiallyRequestNavigation() {
|
|
|
|
for (const barrier of this._pendingNavigationBarriers)
|
|
|
|
barrier.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
frameRequestedNavigation(frameId: string) {
|
|
|
|
const frame = this._frames.get(frameId);
|
|
|
|
if (!frame)
|
|
|
|
return;
|
|
|
|
for (const barrier of this._pendingNavigationBarriers)
|
|
|
|
barrier.addFrame(frame);
|
2020-03-04 19:15:01 -08:00
|
|
|
}
|
|
|
|
|
2019-12-16 15:56:11 -08:00
|
|
|
frameCommittedNewDocumentNavigation(frameId: string, url: string, name: string, documentId: string, initial: boolean) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const frame = this._frames.get(frameId)!;
|
2019-12-16 15:56:11 -08:00
|
|
|
for (const child of frame.childFrames())
|
|
|
|
this._removeFramesRecursively(child);
|
|
|
|
frame._url = url;
|
|
|
|
frame._name = name;
|
|
|
|
frame._lastDocumentId = documentId;
|
2020-02-10 18:35:47 -08:00
|
|
|
for (const watcher of frame._documentWatchers)
|
|
|
|
watcher(documentId);
|
2020-01-27 16:51:52 -08:00
|
|
|
this.clearFrameLifecycle(frame);
|
2020-02-10 18:35:47 -08:00
|
|
|
if (!initial)
|
2020-01-08 15:32:13 -08:00
|
|
|
this._page.emit(Events.Page.FrameNavigated, frame);
|
2019-12-16 15:56:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
frameCommittedSameDocumentNavigation(frameId: string, url: string) {
|
|
|
|
const frame = this._frames.get(frameId);
|
|
|
|
if (!frame)
|
|
|
|
return;
|
|
|
|
frame._url = url;
|
2020-02-10 18:35:47 -08:00
|
|
|
for (const watcher of frame._sameDocumentNavigationWatchers)
|
|
|
|
watcher();
|
2019-12-16 15:56:11 -08:00
|
|
|
this._page.emit(Events.Page.FrameNavigated, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
frameDetached(frameId: string) {
|
|
|
|
const frame = this._frames.get(frameId);
|
|
|
|
if (frame)
|
|
|
|
this._removeFramesRecursively(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
frameStoppedLoading(frameId: string) {
|
|
|
|
const frame = this._frames.get(frameId);
|
|
|
|
if (!frame)
|
|
|
|
return;
|
|
|
|
const hasDOMContentLoaded = frame._firedLifecycleEvents.has('domcontentloaded');
|
|
|
|
const hasLoad = frame._firedLifecycleEvents.has('load');
|
2020-03-06 08:24:32 -08:00
|
|
|
frame._firedLifecycleEvents.add('commit');
|
2019-12-16 15:56:11 -08:00
|
|
|
frame._firedLifecycleEvents.add('domcontentloaded');
|
|
|
|
frame._firedLifecycleEvents.add('load');
|
|
|
|
for (const watcher of this._lifecycleWatchers)
|
2020-02-10 18:35:47 -08:00
|
|
|
watcher();
|
2019-12-16 15:56:11 -08:00
|
|
|
if (frame === this.mainFrame() && !hasDOMContentLoaded)
|
|
|
|
this._page.emit(Events.Page.DOMContentLoaded);
|
|
|
|
if (frame === this.mainFrame() && !hasLoad)
|
|
|
|
this._page.emit(Events.Page.Load);
|
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
frameLifecycleEvent(frameId: string, event: types.LifecycleEvent) {
|
2019-12-16 15:56:11 -08:00
|
|
|
const frame = this._frames.get(frameId);
|
|
|
|
if (!frame)
|
|
|
|
return;
|
2020-01-27 16:51:52 -08:00
|
|
|
frame._firedLifecycleEvents.add(event);
|
|
|
|
for (const watcher of this._lifecycleWatchers)
|
2020-02-10 18:35:47 -08:00
|
|
|
watcher();
|
2019-12-16 15:56:11 -08:00
|
|
|
if (frame === this._mainFrame && event === 'load')
|
|
|
|
this._page.emit(Events.Page.Load);
|
|
|
|
if (frame === this._mainFrame && event === 'domcontentloaded')
|
|
|
|
this._page.emit(Events.Page.DOMContentLoaded);
|
|
|
|
}
|
|
|
|
|
2020-01-27 16:51:52 -08:00
|
|
|
clearFrameLifecycle(frame: Frame) {
|
|
|
|
frame._firedLifecycleEvents.clear();
|
2020-03-06 08:24:32 -08:00
|
|
|
frame._firedLifecycleEvents.add('commit');
|
2020-01-03 12:59:27 -08:00
|
|
|
// Keep the current navigation request if any.
|
|
|
|
frame._inflightRequests = new Set(Array.from(frame._inflightRequests).filter(request => request._documentId === frame._lastDocumentId));
|
|
|
|
this._stopNetworkIdleTimer(frame, 'networkidle0');
|
|
|
|
if (frame._inflightRequests.size === 0)
|
|
|
|
this._startNetworkIdleTimer(frame, 'networkidle0');
|
|
|
|
this._stopNetworkIdleTimer(frame, 'networkidle2');
|
|
|
|
if (frame._inflightRequests.size <= 2)
|
|
|
|
this._startNetworkIdleTimer(frame, 'networkidle2');
|
|
|
|
}
|
|
|
|
|
2019-12-16 16:32:04 -08:00
|
|
|
requestStarted(request: network.Request) {
|
2020-01-03 12:59:27 -08:00
|
|
|
this._inflightRequestStarted(request);
|
2020-01-13 13:33:25 -08:00
|
|
|
const frame = request.frame();
|
2020-02-10 18:35:47 -08:00
|
|
|
if (frame) {
|
|
|
|
for (const watcher of frame._requestWatchers)
|
|
|
|
watcher(request);
|
2019-12-16 16:32:04 -08:00
|
|
|
}
|
2020-01-17 17:14:39 -08:00
|
|
|
if (!request._isFavicon)
|
2020-02-03 14:23:24 -08:00
|
|
|
this._page._requestStarted(request);
|
2019-12-16 16:32:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
requestReceivedResponse(response: network.Response) {
|
2020-01-17 17:14:39 -08:00
|
|
|
if (!response.request()._isFavicon)
|
|
|
|
this._page.emit(Events.Page.Response, response);
|
2019-12-16 16:32:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
requestFinished(request: network.Request) {
|
2020-01-03 12:59:27 -08:00
|
|
|
this._inflightRequestFinished(request);
|
2020-01-17 17:14:39 -08:00
|
|
|
if (!request._isFavicon)
|
|
|
|
this._page.emit(Events.Page.RequestFinished, request);
|
2019-12-16 16:32:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
requestFailed(request: network.Request, canceled: boolean) {
|
2020-01-03 12:59:27 -08:00
|
|
|
this._inflightRequestFinished(request);
|
2020-01-13 13:33:25 -08:00
|
|
|
const frame = request.frame();
|
|
|
|
if (request._documentId && frame) {
|
|
|
|
const isCurrentDocument = frame._lastDocumentId === request._documentId;
|
2019-12-16 16:32:04 -08:00
|
|
|
if (!isCurrentDocument) {
|
2020-01-13 13:33:25 -08:00
|
|
|
let errorText = request.failure()!.errorText;
|
2019-12-16 16:32:04 -08:00
|
|
|
if (canceled)
|
|
|
|
errorText += '; maybe frame was detached?';
|
2020-02-10 18:35:47 -08:00
|
|
|
for (const watcher of frame._documentWatchers)
|
|
|
|
watcher(request._documentId, new Error(errorText));
|
2019-12-16 16:32:04 -08:00
|
|
|
}
|
|
|
|
}
|
2020-01-17 17:14:39 -08:00
|
|
|
if (!request._isFavicon)
|
|
|
|
this._page.emit(Events.Page.RequestFailed, request);
|
2019-12-16 16:32:04 -08:00
|
|
|
}
|
|
|
|
|
2020-02-10 18:35:47 -08:00
|
|
|
provisionalLoadFailed(frame: Frame, documentId: string, error: string) {
|
|
|
|
for (const watcher of frame._documentWatchers)
|
|
|
|
watcher(documentId, new Error(error));
|
2020-01-14 11:46:08 -08:00
|
|
|
}
|
|
|
|
|
2019-12-16 15:56:11 -08:00
|
|
|
private _removeFramesRecursively(frame: Frame) {
|
|
|
|
for (const child of frame.childFrames())
|
|
|
|
this._removeFramesRecursively(child);
|
|
|
|
frame._onDetached();
|
|
|
|
this._frames.delete(frame._id);
|
|
|
|
this._page.emit(Events.Page.FrameDetached, frame);
|
|
|
|
}
|
2019-12-16 16:32:04 -08:00
|
|
|
|
2020-01-03 12:59:27 -08:00
|
|
|
private _inflightRequestFinished(request: network.Request) {
|
|
|
|
const frame = request.frame();
|
2020-01-17 17:14:39 -08:00
|
|
|
if (!frame || request._isFavicon)
|
2020-01-13 13:33:25 -08:00
|
|
|
return;
|
2020-01-03 12:59:27 -08:00
|
|
|
if (!frame._inflightRequests.has(request))
|
|
|
|
return;
|
|
|
|
frame._inflightRequests.delete(request);
|
|
|
|
if (frame._inflightRequests.size === 0)
|
2019-12-16 16:32:04 -08:00
|
|
|
this._startNetworkIdleTimer(frame, 'networkidle0');
|
2020-01-03 12:59:27 -08:00
|
|
|
if (frame._inflightRequests.size === 2)
|
2019-12-16 16:32:04 -08:00
|
|
|
this._startNetworkIdleTimer(frame, 'networkidle2');
|
|
|
|
}
|
|
|
|
|
2020-01-03 12:59:27 -08:00
|
|
|
private _inflightRequestStarted(request: network.Request) {
|
|
|
|
const frame = request.frame();
|
2020-01-17 17:14:39 -08:00
|
|
|
if (!frame || request._isFavicon)
|
2020-01-13 13:33:25 -08:00
|
|
|
return;
|
2020-01-03 12:59:27 -08:00
|
|
|
frame._inflightRequests.add(request);
|
|
|
|
if (frame._inflightRequests.size === 1)
|
2019-12-16 16:32:04 -08:00
|
|
|
this._stopNetworkIdleTimer(frame, 'networkidle0');
|
2020-01-03 12:59:27 -08:00
|
|
|
if (frame._inflightRequests.size === 3)
|
2019-12-16 16:32:04 -08:00
|
|
|
this._stopNetworkIdleTimer(frame, 'networkidle2');
|
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
private _startNetworkIdleTimer(frame: Frame, event: types.LifecycleEvent) {
|
2019-12-16 16:32:04 -08:00
|
|
|
assert(!frame._networkIdleTimers.has(event));
|
|
|
|
if (frame._firedLifecycleEvents.has(event))
|
|
|
|
return;
|
|
|
|
frame._networkIdleTimers.set(event, setTimeout(() => {
|
|
|
|
this.frameLifecycleEvent(frame._id, event);
|
|
|
|
}, 500));
|
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
private _stopNetworkIdleTimer(frame: Frame, event: types.LifecycleEvent) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const timeoutId = frame._networkIdleTimers.get(event);
|
|
|
|
if (timeoutId)
|
|
|
|
clearTimeout(timeoutId);
|
2019-12-16 16:32:04 -08:00
|
|
|
frame._networkIdleTimers.delete(event);
|
|
|
|
}
|
2020-01-27 16:51:52 -08:00
|
|
|
|
|
|
|
interceptConsoleMessage(message: ConsoleMessage): boolean {
|
|
|
|
if (message.type() !== 'debug')
|
|
|
|
return false;
|
|
|
|
const tag = message.text();
|
|
|
|
const handler = this._consoleMessageTags.get(tag);
|
|
|
|
if (!handler)
|
|
|
|
return false;
|
|
|
|
this._consoleMessageTags.delete(tag);
|
|
|
|
handler();
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-16 15:56:11 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
export class Frame {
|
2019-12-13 10:52:33 -08:00
|
|
|
_id: string;
|
2020-03-06 08:24:32 -08:00
|
|
|
readonly _firedLifecycleEvents: Set<types.LifecycleEvent>;
|
2020-02-10 18:35:47 -08:00
|
|
|
_lastDocumentId = '';
|
|
|
|
_requestWatchers = new Set<(request: network.Request) => void>();
|
|
|
|
_documentWatchers = new Set<(documentId: string, error?: Error) => void>();
|
|
|
|
_sameDocumentNavigationWatchers = new Set<() => void>();
|
2019-12-11 07:17:32 -08:00
|
|
|
readonly _page: Page;
|
2020-01-13 13:33:25 -08:00
|
|
|
private _parentFrame: Frame | null;
|
2019-12-16 15:56:11 -08:00
|
|
|
_url = '';
|
2019-11-18 18:18:28 -08:00
|
|
|
private _detached = false;
|
2019-12-12 21:11:52 -08:00
|
|
|
private _contextData = new Map<ContextType, ContextData>();
|
2019-11-27 16:02:31 -08:00
|
|
|
private _childFrames = new Set<Frame>();
|
2020-01-13 13:33:25 -08:00
|
|
|
_name = '';
|
2020-01-03 12:59:27 -08:00
|
|
|
_inflightRequests = new Set<network.Request>();
|
2020-03-06 08:24:32 -08:00
|
|
|
readonly _networkIdleTimers = new Map<types.LifecycleEvent, NodeJS.Timer>();
|
2020-01-27 16:51:52 -08:00
|
|
|
private _setContentCounter = 0;
|
2020-02-10 18:35:47 -08:00
|
|
|
private _detachedPromise: Promise<void>;
|
|
|
|
private _detachedCallback = () => {};
|
2019-11-18 18:18:28 -08:00
|
|
|
|
2019-12-13 10:52:33 -08:00
|
|
|
constructor(page: Page, id: string, parentFrame: Frame | null) {
|
|
|
|
this._id = id;
|
2019-12-09 16:34:42 -08:00
|
|
|
this._firedLifecycleEvents = new Set();
|
2019-12-11 07:17:32 -08:00
|
|
|
this._page = page;
|
2019-11-18 18:18:28 -08:00
|
|
|
this._parentFrame = parentFrame;
|
|
|
|
|
2020-02-10 18:35:47 -08:00
|
|
|
this._detachedPromise = new Promise<void>(x => this._detachedCallback = x);
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
this._contextData.set('main', { contextPromise: new Promise(() => {}), contextResolveCallback: () => {}, context: null, rerunnableTasks: new Set() });
|
|
|
|
this._contextData.set('utility', { contextPromise: new Promise(() => {}), contextResolveCallback: () => {}, context: null, rerunnableTasks: new Set() });
|
2019-11-26 15:37:25 -08:00
|
|
|
this._setContext('main', null);
|
|
|
|
this._setContext('utility', null);
|
2019-11-18 18:18:28 -08:00
|
|
|
|
|
|
|
if (this._parentFrame)
|
|
|
|
this._parentFrame._childFrames.add(this);
|
|
|
|
}
|
|
|
|
|
2020-02-10 18:35:47 -08:00
|
|
|
async goto(url: string, options: GotoOptions = {}): Promise<network.Response | null> {
|
2020-02-13 12:24:17 -08:00
|
|
|
const headers = (this._page._state.extraHTTPHeaders || {});
|
|
|
|
let referer = headers['referer'] || headers['Referer'];
|
2020-02-10 18:35:47 -08:00
|
|
|
if (options.referer !== undefined) {
|
2020-01-14 11:46:08 -08:00
|
|
|
if (referer !== undefined && referer !== options.referer)
|
|
|
|
throw new Error('"referer" is already specified as extra HTTP header');
|
2019-12-18 18:03:02 -08:00
|
|
|
referer = options.referer;
|
2020-01-14 11:46:08 -08:00
|
|
|
}
|
2020-02-04 19:39:52 -08:00
|
|
|
url = helper.completeUserURL(url);
|
2020-02-10 18:35:47 -08:00
|
|
|
const { timeout = this._page._timeoutSettings.navigationTimeout() } = options;
|
|
|
|
const disposer = new Disposer();
|
|
|
|
|
|
|
|
const timeoutPromise = disposer.add(createTimeoutPromise(timeout));
|
|
|
|
const frameDestroyedPromise = this._createFrameDestroyedPromise();
|
|
|
|
const sameDocumentPromise = disposer.add(this._waitForSameDocumentNavigation());
|
|
|
|
const requestWatcher = disposer.add(this._trackDocumentRequests());
|
2019-12-16 22:02:33 -08:00
|
|
|
let navigateResult: GotoResult;
|
|
|
|
const navigate = async () => {
|
|
|
|
try {
|
|
|
|
navigateResult = await this._page._delegate.navigateFrame(this, url, referer);
|
|
|
|
} catch (error) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-10 18:35:47 -08:00
|
|
|
throwIfError(await Promise.race([
|
2019-12-16 22:02:33 -08:00
|
|
|
navigate(),
|
2020-02-10 18:35:47 -08:00
|
|
|
timeoutPromise,
|
|
|
|
frameDestroyedPromise,
|
|
|
|
]));
|
|
|
|
|
|
|
|
const promises: Promise<Error|void>[] = [timeoutPromise, frameDestroyedPromise];
|
|
|
|
if (navigateResult!.newDocumentId)
|
|
|
|
promises.push(disposer.add(this._waitForSpecificDocument(navigateResult!.newDocumentId)));
|
|
|
|
else
|
|
|
|
promises.push(sameDocumentPromise);
|
|
|
|
throwIfError(await Promise.race(promises));
|
|
|
|
|
|
|
|
const request = (navigateResult! && navigateResult!.newDocumentId) ? requestWatcher.get(navigateResult!.newDocumentId) : null;
|
|
|
|
const waitForLifecyclePromise = disposer.add(this._waitForLifecycle(options.waitUntil));
|
|
|
|
throwIfError(await Promise.race([timeoutPromise, frameDestroyedPromise, waitForLifecyclePromise]));
|
|
|
|
|
|
|
|
disposer.dispose();
|
|
|
|
|
|
|
|
return request ? request._finalRequest._waitForResponse() : null;
|
|
|
|
|
|
|
|
function throwIfError(error: Error|void): asserts error is void {
|
|
|
|
if (!error)
|
|
|
|
return;
|
|
|
|
disposer.dispose();
|
|
|
|
const message = `While navigating to ${url}: ${error.message}`;
|
|
|
|
if (error instanceof TimeoutError)
|
|
|
|
throw new TimeoutError(message);
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
async waitForNavigation(options: types.WaitForNavigationOptions = {}): Promise<network.Response | null> {
|
2020-02-10 18:35:47 -08:00
|
|
|
const disposer = new Disposer();
|
|
|
|
const requestWatcher = disposer.add(this._trackDocumentRequests());
|
|
|
|
const {timeout = this._page._timeoutSettings.navigationTimeout()} = options;
|
|
|
|
|
|
|
|
const failurePromise = Promise.race([
|
|
|
|
this._createFrameDestroyedPromise(),
|
|
|
|
disposer.add(createTimeoutPromise(timeout)),
|
2019-12-16 22:02:33 -08:00
|
|
|
]);
|
2020-02-10 18:35:47 -08:00
|
|
|
let documentId: string|null = null;
|
|
|
|
let error: void|Error = await Promise.race([
|
|
|
|
failurePromise,
|
|
|
|
disposer.add(this._waitForNewDocument(options.url)).then(result => {
|
|
|
|
if (result.error)
|
|
|
|
return result.error;
|
|
|
|
documentId = result.documentId;
|
|
|
|
}),
|
|
|
|
disposer.add(this._waitForSameDocumentNavigation(options.url)),
|
|
|
|
]);
|
|
|
|
const request = requestWatcher.get(documentId!);
|
2019-12-16 22:02:33 -08:00
|
|
|
if (!error) {
|
2020-02-10 18:35:47 -08:00
|
|
|
error = await Promise.race([
|
|
|
|
failurePromise,
|
|
|
|
disposer.add(this._waitForLifecycle(options.waitUntil)),
|
|
|
|
]);
|
2019-12-16 22:02:33 -08:00
|
|
|
}
|
2020-02-10 18:35:47 -08:00
|
|
|
disposer.dispose();
|
2019-12-16 22:02:33 -08:00
|
|
|
if (error)
|
|
|
|
throw error;
|
2020-03-04 19:15:01 -08:00
|
|
|
|
2020-02-10 18:35:47 -08:00
|
|
|
return request ? request._finalRequest._waitForResponse() : null;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
async waitForLoadState(options: types.NavigateOptions = {}): Promise<void> {
|
2020-02-10 18:35:47 -08:00
|
|
|
const {timeout = this._page._timeoutSettings.navigationTimeout()} = options;
|
|
|
|
const disposer = new Disposer();
|
2019-12-16 22:02:33 -08:00
|
|
|
const error = await Promise.race([
|
2020-02-10 18:35:47 -08:00
|
|
|
this._createFrameDestroyedPromise(),
|
|
|
|
disposer.add(createTimeoutPromise(timeout)),
|
|
|
|
disposer.add(this._waitForLifecycle(options.waitUntil)),
|
2019-12-16 22:02:33 -08:00
|
|
|
]);
|
2020-02-10 18:35:47 -08:00
|
|
|
disposer.dispose();
|
2019-12-16 22:02:33 -08:00
|
|
|
if (error)
|
|
|
|
throw error;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-02-10 18:35:47 -08:00
|
|
|
_waitForSpecificDocument(expectedDocumentId: string): Disposable<Promise<Error|void>> {
|
|
|
|
let resolve: (error: Error|void) => void;
|
|
|
|
const promise = new Promise<Error|void>(x => resolve = x);
|
|
|
|
const watch = (documentId: string, error?: Error) => {
|
2020-02-18 19:58:04 -08:00
|
|
|
if (documentId === expectedDocumentId)
|
|
|
|
resolve(error);
|
|
|
|
else if (!error)
|
|
|
|
resolve(new Error('Navigation interrupted by another one'));
|
2020-02-10 18:35:47 -08:00
|
|
|
};
|
|
|
|
const dispose = () => this._documentWatchers.delete(watch);
|
|
|
|
this._documentWatchers.add(watch);
|
|
|
|
return {value: promise, dispose};
|
|
|
|
}
|
|
|
|
|
|
|
|
_waitForNewDocument(url?: types.URLMatch): Disposable<Promise<{error?: Error, documentId: string}>> {
|
|
|
|
let resolve: (error: {error?: Error, documentId: string}) => void;
|
|
|
|
const promise = new Promise<{error?: Error, documentId: string}>(x => resolve = x);
|
|
|
|
const watch = (documentId: string, error?: Error) => {
|
|
|
|
if (!error && !platform.urlMatches(this.url(), url))
|
|
|
|
return;
|
|
|
|
resolve({error, documentId});
|
|
|
|
};
|
|
|
|
const dispose = () => this._documentWatchers.delete(watch);
|
|
|
|
this._documentWatchers.add(watch);
|
|
|
|
return {value: promise, dispose};
|
|
|
|
}
|
|
|
|
|
|
|
|
_waitForSameDocumentNavigation(url?: types.URLMatch): Disposable<Promise<void>> {
|
|
|
|
let resolve: () => void;
|
|
|
|
const promise = new Promise<void>(x => resolve = x);
|
|
|
|
const watch = () => {
|
|
|
|
if (platform.urlMatches(this.url(), url))
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
const dispose = () => this._sameDocumentNavigationWatchers.delete(watch);
|
|
|
|
this._sameDocumentNavigationWatchers.add(watch);
|
|
|
|
return {value: promise, dispose};
|
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
_waitForLifecycle(waitUntil: types.LifecycleEvent = 'load'): Disposable<Promise<void>> {
|
2020-02-10 18:35:47 -08:00
|
|
|
let resolve: () => void;
|
2020-03-06 08:24:32 -08:00
|
|
|
if (!kLifecycleEvents.has(waitUntil))
|
|
|
|
throw new Error(`Unsupported waitUntil option ${String(waitUntil)}`);
|
2020-02-10 18:35:47 -08:00
|
|
|
|
|
|
|
const checkLifecycleComplete = () => {
|
|
|
|
if (!checkLifecycleRecursively(this))
|
|
|
|
return;
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
const promise = new Promise<void>(x => resolve = x);
|
|
|
|
const dispose = () => this._page._frameManager._lifecycleWatchers.delete(checkLifecycleComplete);
|
|
|
|
this._page._frameManager._lifecycleWatchers.add(checkLifecycleComplete);
|
|
|
|
checkLifecycleComplete();
|
|
|
|
return {value: promise, dispose};
|
|
|
|
|
|
|
|
function checkLifecycleRecursively(frame: Frame): boolean {
|
2020-03-06 08:24:32 -08:00
|
|
|
if (!frame._firedLifecycleEvents.has(waitUntil))
|
|
|
|
return false;
|
2020-02-10 18:35:47 -08:00
|
|
|
for (const child of frame.childFrames()) {
|
|
|
|
if (!checkLifecycleRecursively(child))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_trackDocumentRequests(): Disposable<Map<string, network.Request>> {
|
|
|
|
const requestMap = new Map<string, network.Request>();
|
|
|
|
const dispose = () => {
|
|
|
|
this._requestWatchers.delete(onRequest);
|
|
|
|
};
|
|
|
|
const onRequest = (request: network.Request) => {
|
|
|
|
if (!request._documentId || request.redirectChain().length)
|
|
|
|
return;
|
|
|
|
requestMap.set(request._documentId, request);
|
|
|
|
};
|
|
|
|
this._requestWatchers.add(onRequest);
|
|
|
|
return {dispose, value: requestMap};
|
|
|
|
}
|
|
|
|
|
|
|
|
_createFrameDestroyedPromise(): Promise<Error> {
|
|
|
|
return Promise.race([
|
|
|
|
this._page._disconnectedPromise.then(() => new Error('Navigation failed because browser has disconnected!')),
|
|
|
|
this._detachedPromise.then(() => new Error('Navigating frame was detached!')),
|
2019-12-20 15:32:30 -08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:20:23 -08:00
|
|
|
async frameElement(): Promise<dom.ElementHandle> {
|
|
|
|
return this._page._delegate.getFrameElement(this);
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:30:02 -08:00
|
|
|
_context(contextType: ContextType): Promise<dom.FrameExecutionContext> {
|
2019-11-26 15:37:25 -08:00
|
|
|
if (this._detached)
|
|
|
|
throw new Error(`Execution Context is not available in detached frame "${this.url()}" (are you trying to evaluate?)`);
|
2020-01-13 13:33:25 -08:00
|
|
|
return this._contextData.get(contextType)!.contextPromise;
|
2019-12-17 14:30:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_mainContext(): Promise<dom.FrameExecutionContext> {
|
|
|
|
return this._context('main');
|
2019-11-26 15:37:25 -08:00
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
_utilityContext(): Promise<dom.FrameExecutionContext> {
|
2019-12-17 14:30:02 -08:00
|
|
|
return this._context('utility');
|
2019-11-28 12:50:52 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
evaluateHandle: types.EvaluateHandle = async (pageFunction, ...args) => {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._mainContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return context.evaluateHandle(pageFunction, ...args as any);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
evaluate: types.Evaluate = async (pageFunction, ...args) => {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._mainContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return context.evaluate(pageFunction, ...args as any);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-18 14:28:16 -08:00
|
|
|
async $(selector: string): Promise<dom.ElementHandle<Element> | null> {
|
|
|
|
const utilityContext = await this._utilityContext();
|
|
|
|
const mainContext = await this._mainContext();
|
|
|
|
const handle = await utilityContext._$(selector);
|
|
|
|
if (handle && handle._context !== mainContext) {
|
|
|
|
const adopted = this._page._delegate.adoptElementHandle(handle, mainContext);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-12-18 14:28:16 -08:00
|
|
|
return adopted;
|
|
|
|
}
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:02:42 -08:00
|
|
|
async waitForSelector(selector: string, options?: types.WaitForElementOptions): Promise<dom.ElementHandle<Element> | null> {
|
2020-03-05 17:45:41 -08:00
|
|
|
if (options && (options as any).visibility)
|
|
|
|
throw new Error('options.visibility is not supported, did you mean options.waitFor?');
|
2020-03-06 16:24:21 -08:00
|
|
|
const { timeout = this._page._timeoutSettings.timeout(), waitFor = 'attached' } = (options || {});
|
|
|
|
if (!['attached', 'detached', 'visible', 'hidden'].includes(waitFor))
|
|
|
|
throw new Error(`Unsupported waitFor option "${waitFor}"`);
|
|
|
|
|
|
|
|
const task = dom.waitForSelectorTask(selector, waitFor, timeout);
|
|
|
|
const result = await this._scheduleRerunnableTask(task, 'utility', timeout, `selector "${selectorToString(selector, waitFor)}"`);
|
|
|
|
if (!result.asElement()) {
|
|
|
|
result.dispose();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const handle = result.asElement() as dom.ElementHandle<Element>;
|
2019-12-18 14:28:16 -08:00
|
|
|
const mainContext = await this._mainContext();
|
|
|
|
if (handle && handle._context !== mainContext) {
|
2020-03-06 16:24:21 -08:00
|
|
|
const adopted = await this._page._delegate.adoptElementHandle(handle, mainContext);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-12-18 14:28:16 -08:00
|
|
|
return adopted;
|
|
|
|
}
|
|
|
|
return handle;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
$eval: types.$Eval = async (selector, pageFunction, ...args) => {
|
2019-12-12 21:11:52 -08:00
|
|
|
const context = await this._mainContext();
|
2019-12-17 14:30:02 -08:00
|
|
|
const elementHandle = await context._$(selector);
|
|
|
|
if (!elementHandle)
|
|
|
|
throw new Error(`Error: failed to find element matching selector "${selector}"`);
|
|
|
|
const result = await elementHandle.evaluate(pageFunction, ...args as any);
|
2020-03-04 17:57:35 -08:00
|
|
|
elementHandle.dispose();
|
2019-12-17 14:30:02 -08:00
|
|
|
return result;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
$$eval: types.$$Eval = async (selector, pageFunction, ...args) => {
|
2019-12-12 21:11:52 -08:00
|
|
|
const context = await this._mainContext();
|
2019-12-17 14:30:02 -08:00
|
|
|
const arrayHandle = await context._$array(selector);
|
|
|
|
const result = await arrayHandle.evaluate(pageFunction, ...args as any);
|
2020-03-04 17:57:35 -08:00
|
|
|
arrayHandle.dispose();
|
2019-12-17 14:30:02 -08:00
|
|
|
return result;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:30:02 -08:00
|
|
|
async $$(selector: string): Promise<dom.ElementHandle<Element>[]> {
|
2019-12-12 21:11:52 -08:00
|
|
|
const context = await this._mainContext();
|
2019-12-17 14:30:02 -08:00
|
|
|
return context._$$(selector);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async content(): Promise<string> {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._utilityContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return context.evaluate(() => {
|
|
|
|
let retVal = '';
|
|
|
|
if (document.doctype)
|
|
|
|
retVal = new XMLSerializer().serializeToString(document.doctype);
|
|
|
|
if (document.documentElement)
|
|
|
|
retVal += document.documentElement.outerHTML;
|
|
|
|
return retVal;
|
|
|
|
});
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 08:24:32 -08:00
|
|
|
async setContent(html: string, options?: types.NavigateOptions): Promise<void> {
|
2020-01-27 16:51:52 -08:00
|
|
|
const tag = `--playwright--set--content--${this._id}--${++this._setContentCounter}--`;
|
2019-12-16 22:02:33 -08:00
|
|
|
const context = await this._utilityContext();
|
2020-02-10 18:35:47 -08:00
|
|
|
const lifecyclePromise = new Promise(resolve => {
|
|
|
|
this._page._frameManager._consoleMessageTags.set(tag, () => {
|
|
|
|
// Clear lifecycle right after document.open() - see 'tag' below.
|
|
|
|
this._page._frameManager.clearFrameLifecycle(this);
|
|
|
|
resolve(this.waitForLoadState(options));
|
|
|
|
});
|
2020-01-27 16:51:52 -08:00
|
|
|
});
|
2020-02-10 18:35:47 -08:00
|
|
|
const contentPromise = context.evaluate((html, tag) => {
|
2019-12-20 15:30:12 -08:00
|
|
|
window.stop();
|
2019-12-16 22:02:33 -08:00
|
|
|
document.open();
|
2020-01-27 16:51:52 -08:00
|
|
|
console.debug(tag); // eslint-disable-line no-console
|
2019-12-16 22:02:33 -08:00
|
|
|
document.write(html);
|
|
|
|
document.close();
|
2020-01-27 16:51:52 -08:00
|
|
|
}, html, tag);
|
2020-02-10 18:35:47 -08:00
|
|
|
await Promise.all([contentPromise, lifecyclePromise]);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
name(): string {
|
|
|
|
return this._name || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
parentFrame(): Frame | null {
|
2019-11-18 18:18:28 -08:00
|
|
|
return this._parentFrame;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
childFrames(): Frame[] {
|
2019-11-18 18:18:28 -08:00
|
|
|
return Array.from(this._childFrames);
|
|
|
|
}
|
|
|
|
|
|
|
|
isDetached(): boolean {
|
|
|
|
return this._detached;
|
|
|
|
}
|
|
|
|
|
|
|
|
async addScriptTag(options: {
|
|
|
|
url?: string; path?: string;
|
|
|
|
content?: string;
|
2019-11-26 08:57:53 -08:00
|
|
|
type?: string;
|
2019-11-27 16:02:31 -08:00
|
|
|
}): Promise<dom.ElementHandle> {
|
2019-11-26 08:57:53 -08:00
|
|
|
const {
|
|
|
|
url = null,
|
|
|
|
path = null,
|
|
|
|
content = null,
|
|
|
|
type = ''
|
|
|
|
} = options;
|
2019-12-11 13:51:03 -08:00
|
|
|
if (!url && !path && !content)
|
|
|
|
throw new Error('Provide an object with a `url`, `path` or `content` property');
|
|
|
|
|
|
|
|
const context = await this._mainContext();
|
|
|
|
return this._raceWithCSPError(async () => {
|
|
|
|
if (url !== null)
|
2020-01-13 13:33:25 -08:00
|
|
|
return (await context.evaluateHandle(addScriptUrl, url, type)).asElement()!;
|
2019-12-11 13:51:03 -08:00
|
|
|
if (path !== null) {
|
2020-01-07 11:55:24 -08:00
|
|
|
let contents = await platform.readFileAsync(path, 'utf8');
|
2019-12-11 13:51:03 -08:00
|
|
|
contents += '//# sourceURL=' + path.replace(/\n/g, '');
|
2020-01-13 13:33:25 -08:00
|
|
|
return (await context.evaluateHandle(addScriptContent, contents, type)).asElement()!;
|
2019-11-26 08:57:53 -08:00
|
|
|
}
|
2020-01-13 13:33:25 -08:00
|
|
|
return (await context.evaluateHandle(addScriptContent, content!, type)).asElement()!;
|
2019-12-11 13:51:03 -08:00
|
|
|
});
|
2019-11-26 08:57:53 -08:00
|
|
|
|
|
|
|
async function addScriptUrl(url: string, type: string): Promise<HTMLElement> {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.src = url;
|
|
|
|
if (type)
|
|
|
|
script.type = type;
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
script.onload = res;
|
|
|
|
script.onerror = rej;
|
|
|
|
});
|
|
|
|
document.head.appendChild(script);
|
|
|
|
await promise;
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addScriptContent(content: string, type: string = 'text/javascript'): HTMLElement {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.type = type;
|
|
|
|
script.text = content;
|
|
|
|
let error = null;
|
|
|
|
script.onerror = e => error = e;
|
|
|
|
document.head.appendChild(script);
|
|
|
|
if (error)
|
|
|
|
throw error;
|
|
|
|
return script;
|
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
async addStyleTag(options: { url?: string; path?: string; content?: string; }): Promise<dom.ElementHandle> {
|
2019-11-26 08:57:53 -08:00
|
|
|
const {
|
|
|
|
url = null,
|
|
|
|
path = null,
|
|
|
|
content = null
|
|
|
|
} = options;
|
2019-12-11 13:51:03 -08:00
|
|
|
if (!url && !path && !content)
|
|
|
|
throw new Error('Provide an object with a `url`, `path` or `content` property');
|
|
|
|
|
|
|
|
const context = await this._mainContext();
|
|
|
|
return this._raceWithCSPError(async () => {
|
|
|
|
if (url !== null)
|
2020-01-13 13:33:25 -08:00
|
|
|
return (await context.evaluateHandle(addStyleUrl, url)).asElement()!;
|
2019-12-12 20:21:29 -08:00
|
|
|
|
2019-12-11 13:51:03 -08:00
|
|
|
if (path !== null) {
|
2020-01-07 11:55:24 -08:00
|
|
|
let contents = await platform.readFileAsync(path, 'utf8');
|
2019-12-11 13:51:03 -08:00
|
|
|
contents += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/';
|
2020-01-13 13:33:25 -08:00
|
|
|
return (await context.evaluateHandle(addStyleContent, contents)).asElement()!;
|
2019-11-26 08:57:53 -08:00
|
|
|
}
|
2019-12-12 20:21:29 -08:00
|
|
|
|
2020-01-13 13:33:25 -08:00
|
|
|
return (await context.evaluateHandle(addStyleContent, content!)).asElement()!;
|
2019-12-11 13:51:03 -08:00
|
|
|
});
|
2019-12-12 20:21:29 -08:00
|
|
|
|
2019-11-26 08:57:53 -08:00
|
|
|
async function addStyleUrl(url: string): Promise<HTMLElement> {
|
|
|
|
const link = document.createElement('link');
|
|
|
|
link.rel = 'stylesheet';
|
|
|
|
link.href = url;
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
link.onload = res;
|
|
|
|
link.onerror = rej;
|
|
|
|
});
|
|
|
|
document.head.appendChild(link);
|
|
|
|
await promise;
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addStyleContent(content: string): Promise<HTMLElement> {
|
|
|
|
const style = document.createElement('style');
|
|
|
|
style.type = 'text/css';
|
|
|
|
style.appendChild(document.createTextNode(content));
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
style.onload = res;
|
|
|
|
style.onerror = rej;
|
|
|
|
});
|
|
|
|
document.head.appendChild(style);
|
|
|
|
await promise;
|
|
|
|
return style;
|
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:51:03 -08:00
|
|
|
private async _raceWithCSPError(func: () => Promise<dom.ElementHandle>): Promise<dom.ElementHandle> {
|
|
|
|
const listeners: RegisteredListener[] = [];
|
2020-01-13 13:33:25 -08:00
|
|
|
let result: dom.ElementHandle;
|
2019-12-11 13:51:03 -08:00
|
|
|
let error: Error | undefined;
|
|
|
|
let cspMessage: ConsoleMessage | undefined;
|
2019-12-12 20:21:29 -08:00
|
|
|
const actionPromise = new Promise<dom.ElementHandle>(async resolve => {
|
2019-12-11 13:51:03 -08:00
|
|
|
try {
|
|
|
|
result = await func();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
const errorPromise = new Promise(resolve => {
|
|
|
|
listeners.push(helper.addEventListener(this._page, Events.Page.Console, (message: ConsoleMessage) => {
|
|
|
|
if (message.type() === 'error' && message.text().includes('Content Security Policy')) {
|
|
|
|
cspMessage = message;
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
await Promise.race([actionPromise, errorPromise]);
|
|
|
|
helper.removeEventListeners(listeners);
|
|
|
|
if (cspMessage)
|
|
|
|
throw new Error(cspMessage.text());
|
|
|
|
if (error)
|
|
|
|
throw error;
|
2020-01-13 13:33:25 -08:00
|
|
|
return result!;
|
2019-12-11 13:51:03 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async click(selector: string, options?: dom.ClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.click(options);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async dblclick(selector: string, options?: dom.MultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.dblclick(options);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async tripleclick(selector: string, options?: dom.MultiClickOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.tripleclick(options);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async fill(selector: string, value: string, options?: types.NavigatingActionWaitOptions) {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2020-03-06 08:24:32 -08:00
|
|
|
await handle.fill(value, options);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async focus(selector: string, options?: types.TimeoutOptions) {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.focus();
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async hover(selector: string, options?: dom.PointerActionOptions & types.PointerActionWaitOptions) {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.hover(options);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async select(selector: string, values: string | dom.ElementHandle | types.SelectOption | string[] | dom.ElementHandle[] | types.SelectOption[], options?: types.NavigatingActionWaitOptions): Promise<string[]> {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2020-03-06 08:24:32 -08:00
|
|
|
const result = await handle.select(values, options);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-11-22 14:46:34 -08:00
|
|
|
return result;
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async type(selector: string, text: string, options?: { delay?: number } & types.NavigatingActionWaitOptions) {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2019-11-22 14:46:34 -08:00
|
|
|
await handle.type(text, options);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async press(selector: string, key: string, options?: { delay?: number, text?: string } & types.NavigatingActionWaitOptions) {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2020-03-06 09:38:08 -08:00
|
|
|
await handle.press(key, options);
|
|
|
|
handle.dispose();
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async check(selector: string, options?: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2020-02-19 09:34:57 -08:00
|
|
|
await handle.check(options);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
async uncheck(selector: string, options?: types.PointerActionWaitOptions & types.NavigatingActionWaitOptions) {
|
|
|
|
const handle = await this._waitForSelectorInUtilityContext(selector, options);
|
2020-02-19 09:34:57 -08:00
|
|
|
await handle.uncheck(options);
|
2020-03-04 17:57:35 -08:00
|
|
|
handle.dispose();
|
2020-02-04 14:39:11 -08:00
|
|
|
}
|
|
|
|
|
2020-03-05 17:45:41 -08:00
|
|
|
async waitFor(selectorOrFunctionOrTimeout: (string | number | Function), options: types.WaitForFunctionOptions & types.WaitForElementOptions = {}, ...args: any[]): Promise<js.JSHandle | null> {
|
2019-12-03 10:43:13 -08:00
|
|
|
if (helper.isString(selectorOrFunctionOrTimeout))
|
2020-03-06 15:02:42 -08:00
|
|
|
return this.waitForSelector(selectorOrFunctionOrTimeout, options) as any;
|
2019-11-18 18:18:28 -08:00
|
|
|
if (helper.isNumber(selectorOrFunctionOrTimeout))
|
2020-02-07 13:38:50 -08:00
|
|
|
return new Promise(fulfill => setTimeout(fulfill, selectorOrFunctionOrTimeout));
|
2019-11-18 18:18:28 -08:00
|
|
|
if (typeof selectorOrFunctionOrTimeout === 'function')
|
|
|
|
return this.waitForFunction(selectorOrFunctionOrTimeout, options, ...args);
|
|
|
|
return Promise.reject(new Error('Unsupported target type: ' + (typeof selectorOrFunctionOrTimeout)));
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
private async _waitForSelectorInUtilityContext(selector: string, options?: types.WaitForElementOptions): Promise<dom.ElementHandle<Element>> {
|
2020-03-05 17:45:41 -08:00
|
|
|
const { timeout = this._page._timeoutSettings.timeout(), waitFor = 'attached' } = (options || {});
|
|
|
|
const task = dom.waitForSelectorTask(selector, waitFor, timeout);
|
|
|
|
const result = await this._scheduleRerunnableTask(task, 'utility', timeout, `selector "${selectorToString(selector, waitFor)}"`);
|
2019-12-18 14:28:16 -08:00
|
|
|
return result.asElement() as dom.ElementHandle<Element>;
|
|
|
|
}
|
|
|
|
|
2019-12-20 15:31:20 -08:00
|
|
|
async waitForFunction(pageFunction: Function | string, options?: types.WaitForFunctionOptions, ...args: any[]): Promise<js.JSHandle> {
|
2019-12-18 18:11:02 -08:00
|
|
|
options = { timeout: this._page._timeoutSettings.timeout(), ...(options || {}) };
|
|
|
|
const task = dom.waitForFunctionTask(undefined, pageFunction, options, ...args);
|
|
|
|
return this._scheduleRerunnableTask(task, 'main', options.timeout);
|
|
|
|
}
|
|
|
|
|
2019-11-18 18:18:28 -08:00
|
|
|
async title(): Promise<string> {
|
2019-11-26 15:37:25 -08:00
|
|
|
const context = await this._utilityContext();
|
2019-11-26 08:57:53 -08:00
|
|
|
return context.evaluate(() => document.title);
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
|
|
|
|
2019-12-11 07:17:32 -08:00
|
|
|
_onDetached() {
|
2019-11-18 18:18:28 -08:00
|
|
|
this._detached = true;
|
2020-02-10 18:35:47 -08:00
|
|
|
this._detachedCallback();
|
2019-12-12 21:11:52 -08:00
|
|
|
for (const data of this._contextData.values()) {
|
|
|
|
for (const rerunnableTask of data.rerunnableTasks)
|
2019-12-03 10:51:41 -08:00
|
|
|
rerunnableTask.terminate(new Error('waitForFunction failed: frame got detached.'));
|
2019-11-26 15:37:25 -08:00
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
if (this._parentFrame)
|
|
|
|
this._parentFrame._childFrames.delete(this);
|
|
|
|
this._parentFrame = null;
|
|
|
|
}
|
2019-11-26 15:37:25 -08:00
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
private _scheduleRerunnableTask(task: dom.Task, contextType: ContextType, timeout?: number, title?: string): Promise<js.JSHandle> {
|
2020-01-13 13:33:25 -08:00
|
|
|
const data = this._contextData.get(contextType)!;
|
2019-12-12 21:11:52 -08:00
|
|
|
const rerunnableTask = new RerunnableTask(data, task, timeout, title);
|
|
|
|
data.rerunnableTasks.add(rerunnableTask);
|
|
|
|
if (data.context)
|
|
|
|
rerunnableTask.rerun(data.context);
|
2019-12-03 10:51:41 -08:00
|
|
|
return rerunnableTask.promise;
|
2019-11-26 15:37:25 -08:00
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
private _setContext(contextType: ContextType, context: dom.FrameExecutionContext | null) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const data = this._contextData.get(contextType)!;
|
2019-12-12 21:11:52 -08:00
|
|
|
data.context = context;
|
2019-11-26 15:37:25 -08:00
|
|
|
if (context) {
|
2019-12-12 21:11:52 -08:00
|
|
|
data.contextResolveCallback.call(null, context);
|
|
|
|
for (const rerunnableTask of data.rerunnableTasks)
|
|
|
|
rerunnableTask.rerun(context);
|
2019-11-26 15:37:25 -08:00
|
|
|
} else {
|
2019-12-12 21:11:52 -08:00
|
|
|
data.contextPromise = new Promise(fulfill => {
|
|
|
|
data.contextResolveCallback = fulfill;
|
2019-11-26 15:37:25 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
_contextCreated(contextType: ContextType, context: dom.FrameExecutionContext) {
|
2020-01-13 13:33:25 -08:00
|
|
|
const data = this._contextData.get(contextType)!;
|
2019-11-26 15:37:25 -08:00
|
|
|
// In case of multiple sessions to the same target, there's a race between
|
|
|
|
// connections so we might end up creating multiple isolated worlds.
|
|
|
|
// We can use either.
|
2019-12-12 21:11:52 -08:00
|
|
|
if (data.context)
|
|
|
|
this._setContext(contextType, null);
|
|
|
|
this._setContext(contextType, context);
|
2019-11-26 15:37:25 -08:00
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
_contextDestroyed(context: dom.FrameExecutionContext) {
|
|
|
|
for (const [contextType, data] of this._contextData) {
|
|
|
|
if (data.context === context)
|
|
|
|
this._setContext(contextType, null);
|
2019-11-26 15:37:25 -08:00
|
|
|
}
|
|
|
|
}
|
2019-11-18 18:18:28 -08:00
|
|
|
}
|
2019-12-03 10:51:41 -08:00
|
|
|
|
|
|
|
class RerunnableTask {
|
|
|
|
readonly promise: Promise<js.JSHandle>;
|
2019-12-12 21:11:52 -08:00
|
|
|
private _contextData: ContextData;
|
2019-12-03 10:51:41 -08:00
|
|
|
private _task: dom.Task;
|
|
|
|
private _runCount: number;
|
2020-01-13 13:33:25 -08:00
|
|
|
private _resolve: (result: js.JSHandle) => void = () => {};
|
|
|
|
private _reject: (reason: Error) => void = () => {};
|
|
|
|
private _timeoutTimer?: NodeJS.Timer;
|
|
|
|
private _terminated = false;
|
2019-12-03 10:51:41 -08:00
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
constructor(data: ContextData, task: dom.Task, timeout?: number, title?: string) {
|
|
|
|
this._contextData = data;
|
2019-12-03 10:51:41 -08:00
|
|
|
this._task = task;
|
|
|
|
this._runCount = 0;
|
|
|
|
this.promise = new Promise<js.JSHandle>((resolve, reject) => {
|
|
|
|
this._resolve = resolve;
|
|
|
|
this._reject = reject;
|
|
|
|
});
|
|
|
|
// Since page navigation requires us to re-install the pageScript, we should track
|
|
|
|
// timeout on our end.
|
|
|
|
if (timeout) {
|
|
|
|
const timeoutError = new TimeoutError(`waiting for ${title || 'function'} failed: timeout ${timeout}ms exceeded`);
|
|
|
|
this._timeoutTimer = setTimeout(() => this.terminate(timeoutError), timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
terminate(error: Error) {
|
|
|
|
this._terminated = true;
|
|
|
|
this._reject(error);
|
|
|
|
this._doCleanup();
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:11:52 -08:00
|
|
|
async rerun(context: dom.FrameExecutionContext) {
|
2019-12-03 10:51:41 -08:00
|
|
|
const runCount = ++this._runCount;
|
|
|
|
let success: js.JSHandle | null = null;
|
|
|
|
let error = null;
|
|
|
|
try {
|
2019-12-12 21:11:52 -08:00
|
|
|
success = await this._task(context);
|
2019-12-03 10:51:41 -08:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._terminated || runCount !== this._runCount) {
|
|
|
|
if (success)
|
2020-03-04 17:57:35 -08:00
|
|
|
success.dispose();
|
2019-12-03 10:51:41 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore timeouts in pageScript - we track timeouts ourselves.
|
|
|
|
// If execution context has been already destroyed, `context.evaluate` will
|
|
|
|
// throw an error - ignore this predicate run altogether.
|
2019-12-12 21:11:52 -08:00
|
|
|
if (!error && await context.evaluate(s => !s, success).catch(e => true)) {
|
2020-03-04 17:57:35 -08:00
|
|
|
success!.dispose();
|
2019-12-03 10:51:41 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the page is navigated, the promise is rejected.
|
|
|
|
// We will try again in the new execution context.
|
|
|
|
if (error && error.message.includes('Execution context was destroyed'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We could have tried to evaluate in a context which was already
|
|
|
|
// destroyed.
|
|
|
|
if (error && error.message.includes('Cannot find context with specified id'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
this._reject(error);
|
|
|
|
else
|
2020-01-13 13:33:25 -08:00
|
|
|
this._resolve(success!);
|
2019-12-03 10:51:41 -08:00
|
|
|
|
|
|
|
this._doCleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
_doCleanup() {
|
2020-01-13 13:33:25 -08:00
|
|
|
if (this._timeoutTimer)
|
|
|
|
clearTimeout(this._timeoutTimer);
|
2019-12-12 21:11:52 -08:00
|
|
|
this._contextData.rerunnableTasks.delete(this);
|
2019-12-03 10:51:41 -08:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 07:17:32 -08:00
|
|
|
|
2020-02-10 18:35:47 -08:00
|
|
|
type Disposable<T> = {value: T, dispose: () => void};
|
|
|
|
class Disposer {
|
|
|
|
private _disposes: (() => void)[] = [];
|
|
|
|
add<T>({value, dispose}: Disposable<T>) {
|
|
|
|
this._disposes.push(dispose);
|
|
|
|
return value;
|
2019-12-11 07:17:32 -08:00
|
|
|
}
|
|
|
|
dispose() {
|
2020-02-10 18:35:47 -08:00
|
|
|
for (const dispose of this._disposes)
|
|
|
|
dispose();
|
|
|
|
this._disposes = [];
|
2019-12-11 07:17:32 -08:00
|
|
|
}
|
|
|
|
}
|
2019-12-17 14:30:02 -08:00
|
|
|
|
2020-02-10 18:35:47 -08:00
|
|
|
function createTimeoutPromise(timeout: number): Disposable<Promise<TimeoutError>> {
|
|
|
|
if (!timeout)
|
|
|
|
return { value: new Promise(() => {}), dispose: () => void 0 };
|
|
|
|
|
|
|
|
let timer: NodeJS.Timer;
|
|
|
|
const errorMessage = 'Navigation timeout of ' + timeout + ' ms exceeded';
|
|
|
|
const promise = new Promise(fulfill => timer = setTimeout(fulfill, timeout))
|
|
|
|
.then(() => new TimeoutError(errorMessage));
|
|
|
|
const dispose = () => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
value: promise,
|
|
|
|
dispose
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-05 17:45:41 -08:00
|
|
|
function selectorToString(selector: string, waitFor: 'attached' | 'detached' | 'visible' | 'hidden'): string {
|
2019-12-17 14:30:02 -08:00
|
|
|
let label;
|
2020-03-05 17:45:41 -08:00
|
|
|
switch (waitFor) {
|
2019-12-17 14:30:02 -08:00
|
|
|
case 'visible': label = '[visible] '; break;
|
|
|
|
case 'hidden': label = '[hidden] '; break;
|
2020-03-05 17:45:41 -08:00
|
|
|
case 'attached': label = ''; break;
|
|
|
|
case 'detached': label = '[detached]'; break;
|
2019-12-17 14:30:02 -08:00
|
|
|
}
|
|
|
|
return `${label}${selector}`;
|
2020-01-27 16:51:52 -08:00
|
|
|
}
|
2020-03-05 14:47:04 -08:00
|
|
|
|
|
|
|
class PendingNavigationBarrier {
|
|
|
|
private _frameIds = new Map<string, number>();
|
2020-03-06 16:24:21 -08:00
|
|
|
private _options: types.NavigatingActionWaitOptions | undefined;
|
2020-03-05 14:47:04 -08:00
|
|
|
private _protectCount = 0;
|
|
|
|
private _promise: Promise<void>;
|
|
|
|
private _promiseCallback = () => {};
|
|
|
|
|
2020-03-06 16:24:21 -08:00
|
|
|
constructor(options: types.NavigatingActionWaitOptions | undefined) {
|
2020-03-06 14:32:15 -08:00
|
|
|
this._options = options;
|
2020-03-05 14:47:04 -08:00
|
|
|
this._promise = new Promise(f => this._promiseCallback = f);
|
|
|
|
this.retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
waitFor(): Promise<void> {
|
|
|
|
this.release();
|
|
|
|
return this._promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
async addFrame(frame: Frame) {
|
|
|
|
this.retain();
|
2020-03-06 14:32:15 -08:00
|
|
|
await frame.waitForNavigation(this._options as types.NavigateOptions).catch(e => {});
|
2020-03-05 14:47:04 -08:00
|
|
|
this.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
retain() {
|
|
|
|
++this._protectCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
release() {
|
|
|
|
--this._protectCount;
|
|
|
|
this._maybeResolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _maybeResolve() {
|
|
|
|
if (!this._protectCount && !this._frameIds.size)
|
|
|
|
this._promiseCallback();
|
|
|
|
}
|
|
|
|
}
|