2019-01-15 04:34:50 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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-06-15 15:34:50 +00:00
|
|
|
import { assert } from './assert';
|
|
|
|
import { helper, PuppeteerEventListener } from './helper';
|
2020-05-07 10:54:55 +00:00
|
|
|
import { Events } from './Events';
|
|
|
|
import { TimeoutError } from './Errors';
|
2020-07-08 10:00:11 +00:00
|
|
|
import { FrameManager, Frame, FrameManagerEmittedEvents } from './FrameManager';
|
2020-05-29 08:38:40 +00:00
|
|
|
import { HTTPRequest } from './HTTPRequest';
|
2020-05-29 10:49:30 +00:00
|
|
|
import { HTTPResponse } from './HTTPResponse';
|
2020-07-07 15:43:55 +00:00
|
|
|
import { NetworkManagerEmittedEvents } from './NetworkManager';
|
2020-05-07 10:54:55 +00:00
|
|
|
|
|
|
|
export type PuppeteerLifeCycleEvent =
|
|
|
|
| 'load'
|
|
|
|
| 'domcontentloaded'
|
|
|
|
| 'networkidle0'
|
|
|
|
| 'networkidle2';
|
|
|
|
type ProtocolLifeCycleEvent =
|
|
|
|
| 'load'
|
|
|
|
| 'DOMContentLoaded'
|
|
|
|
| 'networkIdle'
|
|
|
|
| 'networkAlmostIdle';
|
|
|
|
|
|
|
|
const puppeteerToProtocolLifecycle = new Map<
|
|
|
|
PuppeteerLifeCycleEvent,
|
|
|
|
ProtocolLifeCycleEvent
|
|
|
|
>([
|
2020-04-27 09:03:33 +00:00
|
|
|
['load', 'load'],
|
|
|
|
['domcontentloaded', 'DOMContentLoaded'],
|
|
|
|
['networkidle0', 'networkIdle'],
|
|
|
|
['networkidle2', 'networkAlmostIdle'],
|
|
|
|
]);
|
|
|
|
|
2020-07-06 11:37:16 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-27 09:03:33 +00:00
|
|
|
export class LifecycleWatcher {
|
|
|
|
_expectedLifecycle: ProtocolLifeCycleEvent[];
|
2020-04-29 11:28:16 +00:00
|
|
|
_frameManager: FrameManager;
|
|
|
|
_frame: Frame;
|
2020-04-27 09:03:33 +00:00
|
|
|
_timeout: number;
|
2020-05-29 08:38:40 +00:00
|
|
|
_navigationRequest?: HTTPRequest;
|
2020-04-27 09:03:33 +00:00
|
|
|
_eventListeners: PuppeteerEventListener[];
|
|
|
|
_initialLoaderId: string;
|
|
|
|
|
|
|
|
_sameDocumentNavigationPromise: Promise<Error | null>;
|
|
|
|
_sameDocumentNavigationCompleteCallback: (x?: Error) => void;
|
|
|
|
|
|
|
|
_lifecyclePromise: Promise<void>;
|
|
|
|
_lifecycleCallback: () => void;
|
|
|
|
|
|
|
|
_newDocumentNavigationPromise: Promise<Error | null>;
|
|
|
|
_newDocumentNavigationCompleteCallback: (x?: Error) => void;
|
|
|
|
|
|
|
|
_terminationPromise: Promise<Error | null>;
|
|
|
|
_terminationCallback: (x?: Error) => void;
|
|
|
|
|
|
|
|
_timeoutPromise: Promise<TimeoutError | null>;
|
|
|
|
|
|
|
|
_maximumTimer?: NodeJS.Timeout;
|
|
|
|
_hasSameDocumentNavigation?: boolean;
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
constructor(
|
|
|
|
frameManager: FrameManager,
|
|
|
|
frame: Frame,
|
|
|
|
waitUntil: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[],
|
|
|
|
timeout: number
|
|
|
|
) {
|
|
|
|
if (Array.isArray(waitUntil)) waitUntil = waitUntil.slice();
|
|
|
|
else if (typeof waitUntil === 'string') waitUntil = [waitUntil];
|
|
|
|
this._expectedLifecycle = waitUntil.map((value) => {
|
2019-10-23 11:12:33 +00:00
|
|
|
const protocolEvent = puppeteerToProtocolLifecycle.get(value);
|
2019-01-15 04:34:50 +00:00
|
|
|
assert(protocolEvent, 'Unknown value for options.waitUntil: ' + value);
|
|
|
|
return protocolEvent;
|
|
|
|
});
|
|
|
|
|
|
|
|
this._frameManager = frameManager;
|
|
|
|
this._frame = frame;
|
|
|
|
this._initialLoaderId = frame._loaderId;
|
|
|
|
this._timeout = timeout;
|
|
|
|
this._navigationRequest = null;
|
|
|
|
this._eventListeners = [
|
2020-05-07 10:54:55 +00:00
|
|
|
helper.addEventListener(
|
|
|
|
frameManager._client,
|
|
|
|
Events.CDPSession.Disconnected,
|
|
|
|
() =>
|
|
|
|
this._terminate(
|
|
|
|
new Error('Navigation failed because browser has disconnected!')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
helper.addEventListener(
|
|
|
|
this._frameManager,
|
2020-07-08 10:00:11 +00:00
|
|
|
FrameManagerEmittedEvents.LifecycleEvent,
|
2020-05-07 10:54:55 +00:00
|
|
|
this._checkLifecycleComplete.bind(this)
|
|
|
|
),
|
|
|
|
helper.addEventListener(
|
|
|
|
this._frameManager,
|
2020-07-08 10:00:11 +00:00
|
|
|
FrameManagerEmittedEvents.FrameNavigatedWithinDocument,
|
2020-05-07 10:54:55 +00:00
|
|
|
this._navigatedWithinDocument.bind(this)
|
|
|
|
),
|
|
|
|
helper.addEventListener(
|
|
|
|
this._frameManager,
|
2020-07-08 10:00:11 +00:00
|
|
|
FrameManagerEmittedEvents.FrameDetached,
|
2020-05-07 10:54:55 +00:00
|
|
|
this._onFrameDetached.bind(this)
|
|
|
|
),
|
|
|
|
helper.addEventListener(
|
|
|
|
this._frameManager.networkManager(),
|
2020-07-07 15:43:55 +00:00
|
|
|
NetworkManagerEmittedEvents.Request,
|
2020-05-07 10:54:55 +00:00
|
|
|
this._onRequest.bind(this)
|
|
|
|
),
|
2019-01-15 04:34:50 +00:00
|
|
|
];
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
this._sameDocumentNavigationPromise = new Promise<Error | null>(
|
|
|
|
(fulfill) => {
|
|
|
|
this._sameDocumentNavigationCompleteCallback = fulfill;
|
|
|
|
}
|
|
|
|
);
|
2019-01-15 04:34:50 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
this._lifecyclePromise = new Promise((fulfill) => {
|
2019-01-15 04:34:50 +00:00
|
|
|
this._lifecycleCallback = fulfill;
|
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
this._newDocumentNavigationPromise = new Promise((fulfill) => {
|
2019-01-15 04:34:50 +00:00
|
|
|
this._newDocumentNavigationCompleteCallback = fulfill;
|
|
|
|
});
|
|
|
|
|
|
|
|
this._timeoutPromise = this._createTimeoutPromise();
|
2020-05-07 10:54:55 +00:00
|
|
|
this._terminationPromise = new Promise((fulfill) => {
|
2019-01-15 04:34:50 +00:00
|
|
|
this._terminationCallback = fulfill;
|
|
|
|
});
|
|
|
|
this._checkLifecycleComplete();
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:38:40 +00:00
|
|
|
_onRequest(request: HTTPRequest): void {
|
2019-01-15 04:34:50 +00:00
|
|
|
if (request.frame() !== this._frame || !request.isNavigationRequest())
|
|
|
|
return;
|
|
|
|
this._navigationRequest = request;
|
|
|
|
}
|
|
|
|
|
2020-04-29 11:28:16 +00:00
|
|
|
_onFrameDetached(frame: Frame): void {
|
2019-01-15 04:34:50 +00:00
|
|
|
if (this._frame === frame) {
|
2020-05-07 10:54:55 +00:00
|
|
|
this._terminationCallback.call(
|
|
|
|
null,
|
|
|
|
new Error('Navigating frame was detached')
|
|
|
|
);
|
2019-01-15 04:34:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._checkLifecycleComplete();
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:49:30 +00:00
|
|
|
navigationResponse(): HTTPResponse | null {
|
2019-01-15 04:34:50 +00:00
|
|
|
return this._navigationRequest ? this._navigationRequest.response() : null;
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
_terminate(error: Error): void {
|
2019-01-15 04:34:50 +00:00
|
|
|
this._terminationCallback.call(null, error);
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
sameDocumentNavigationPromise(): Promise<Error | null> {
|
2019-01-15 04:34:50 +00:00
|
|
|
return this._sameDocumentNavigationPromise;
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
newDocumentNavigationPromise(): Promise<Error | null> {
|
2019-01-15 04:34:50 +00:00
|
|
|
return this._newDocumentNavigationPromise;
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
lifecyclePromise(): Promise<void> {
|
2019-01-15 04:34:50 +00:00
|
|
|
return this._lifecyclePromise;
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
timeoutOrTerminationPromise(): Promise<Error | TimeoutError | null> {
|
2019-01-15 04:34:50 +00:00
|
|
|
return Promise.race([this._timeoutPromise, this._terminationPromise]);
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
_createTimeoutPromise(): Promise<TimeoutError | null> {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!this._timeout) return new Promise(() => {});
|
|
|
|
const errorMessage =
|
|
|
|
'Navigation timeout of ' + this._timeout + ' ms exceeded';
|
|
|
|
return new Promise(
|
|
|
|
(fulfill) => (this._maximumTimer = setTimeout(fulfill, this._timeout))
|
|
|
|
).then(() => new TimeoutError(errorMessage));
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 11:28:16 +00:00
|
|
|
_navigatedWithinDocument(frame: Frame): void {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (frame !== this._frame) return;
|
2019-01-15 04:34:50 +00:00
|
|
|
this._hasSameDocumentNavigation = true;
|
|
|
|
this._checkLifecycleComplete();
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
_checkLifecycleComplete(): void {
|
2019-01-15 04:34:50 +00:00
|
|
|
// We expect navigation to commit.
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!checkLifecycle(this._frame, this._expectedLifecycle)) return;
|
2019-01-15 04:34:50 +00:00
|
|
|
this._lifecycleCallback();
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
this._frame._loaderId === this._initialLoaderId &&
|
|
|
|
!this._hasSameDocumentNavigation
|
|
|
|
)
|
2019-01-15 04:34:50 +00:00
|
|
|
return;
|
|
|
|
if (this._hasSameDocumentNavigation)
|
|
|
|
this._sameDocumentNavigationCompleteCallback();
|
|
|
|
if (this._frame._loaderId !== this._initialLoaderId)
|
|
|
|
this._newDocumentNavigationCompleteCallback();
|
|
|
|
|
|
|
|
/**
|
2020-04-29 11:28:16 +00:00
|
|
|
* @param {!Frame} frame
|
2019-01-15 04:34:50 +00:00
|
|
|
* @param {!Array<string>} expectedLifecycle
|
2020-06-12 10:38:24 +00:00
|
|
|
* @returns {boolean}
|
2019-01-15 04:34:50 +00:00
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
function checkLifecycle(
|
|
|
|
frame: Frame,
|
|
|
|
expectedLifecycle: ProtocolLifeCycleEvent[]
|
|
|
|
): boolean {
|
2019-01-15 04:34:50 +00:00
|
|
|
for (const event of expectedLifecycle) {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!frame._lifecycleEvents.has(event)) return false;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
for (const child of frame.childFrames()) {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!checkLifecycle(child, expectedLifecycle)) return false;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
dispose(): void {
|
2019-01-15 04:34:50 +00:00
|
|
|
helper.removeEventListeners(this._eventListeners);
|
|
|
|
clearTimeout(this._maximumTimer);
|
|
|
|
}
|
|
|
|
}
|