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.
|
|
|
|
*/
|
|
|
|
|
2022-08-17 12:39:41 +00:00
|
|
|
import {assert} from '../util/assert.js';
|
2022-06-14 11:16:21 +00:00
|
|
|
import {
|
|
|
|
addEventListener,
|
|
|
|
PuppeteerEventListener,
|
|
|
|
removeEventListeners,
|
2022-08-17 12:39:41 +00:00
|
|
|
} from './util.js';
|
|
|
|
import {
|
2022-08-10 15:59:58 +00:00
|
|
|
DeferredPromise,
|
|
|
|
createDeferredPromise,
|
2022-08-17 12:39:41 +00:00
|
|
|
} from '../util/DeferredPromise.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {TimeoutError} from './Errors.js';
|
2022-08-17 15:45:45 +00:00
|
|
|
import {FrameManager, FrameManagerEmittedEvents} from './FrameManager.js';
|
|
|
|
import {Frame} from './Frame.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {HTTPRequest} from './HTTPRequest.js';
|
|
|
|
import {HTTPResponse} from './HTTPResponse.js';
|
|
|
|
import {NetworkManagerEmittedEvents} from './NetworkManager.js';
|
|
|
|
import {CDPSessionEmittedEvents} from './Connection.js';
|
2021-04-06 08:58:01 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
export type PuppeteerLifeCycleEvent =
|
|
|
|
| 'load'
|
|
|
|
| 'domcontentloaded'
|
|
|
|
| 'networkidle0'
|
|
|
|
| 'networkidle2';
|
2021-04-12 13:57:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type ProtocolLifeCycleEvent =
|
2020-05-07 10:54:55 +00:00
|
|
|
| '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'],
|
|
|
|
]);
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
const noop = (): void => {};
|
|
|
|
|
2020-07-06 11:37:16 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-27 09:03:33 +00:00
|
|
|
export class LifecycleWatcher {
|
2022-06-13 09:16:25 +00:00
|
|
|
#expectedLifecycle: ProtocolLifeCycleEvent[];
|
|
|
|
#frameManager: FrameManager;
|
|
|
|
#frame: Frame;
|
|
|
|
#timeout: number;
|
|
|
|
#navigationRequest: HTTPRequest | null = null;
|
|
|
|
#eventListeners: PuppeteerEventListener[];
|
2022-08-01 13:39:08 +00:00
|
|
|
#initialLoaderId: string;
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#sameDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
|
2022-06-22 13:25:44 +00:00
|
|
|
#sameDocumentNavigationPromise = new Promise<Error | undefined>(fulfill => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#sameDocumentNavigationCompleteCallback = fulfill;
|
2022-05-24 11:14:19 +00:00
|
|
|
});
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#lifecycleCallback: () => void = noop;
|
2022-06-22 13:25:44 +00:00
|
|
|
#lifecyclePromise: Promise<void> = new Promise(fulfill => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#lifecycleCallback = fulfill;
|
2022-05-24 11:14:19 +00:00
|
|
|
});
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#newDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
|
|
|
|
#newDocumentNavigationPromise: Promise<Error | undefined> = new Promise(
|
2022-06-22 13:25:44 +00:00
|
|
|
fulfill => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#newDocumentNavigationCompleteCallback = fulfill;
|
2022-05-24 11:14:19 +00:00
|
|
|
}
|
|
|
|
);
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#terminationCallback: (x?: Error) => void = noop;
|
2022-06-22 13:25:44 +00:00
|
|
|
#terminationPromise: Promise<Error | undefined> = new Promise(fulfill => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#terminationCallback = fulfill;
|
2022-05-24 11:14:19 +00:00
|
|
|
});
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#timeoutPromise: Promise<TimeoutError | undefined>;
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#maximumTimer?: NodeJS.Timeout;
|
|
|
|
#hasSameDocumentNavigation?: boolean;
|
|
|
|
#swapped?: boolean;
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-08-10 15:59:58 +00:00
|
|
|
#navigationResponseReceived?: DeferredPromise<void>;
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
constructor(
|
|
|
|
frameManager: FrameManager,
|
|
|
|
frame: Frame,
|
|
|
|
waitUntil: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[],
|
|
|
|
timeout: number
|
|
|
|
) {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (Array.isArray(waitUntil)) {
|
|
|
|
waitUntil = waitUntil.slice();
|
|
|
|
} else if (typeof waitUntil === 'string') {
|
|
|
|
waitUntil = [waitUntil];
|
|
|
|
}
|
2022-08-01 13:39:08 +00:00
|
|
|
this.#initialLoaderId = frame._loaderId;
|
2022-06-22 13:25:44 +00:00
|
|
|
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);
|
2022-05-24 11:14:19 +00:00
|
|
|
return protocolEvent as ProtocolLifeCycleEvent;
|
2019-01-15 04:34:50 +00:00
|
|
|
});
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager = frameManager;
|
|
|
|
this.#frame = frame;
|
|
|
|
this.#timeout = timeout;
|
|
|
|
this.#eventListeners = [
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-08-10 21:34:29 +00:00
|
|
|
frameManager.client,
|
2020-07-08 10:11:01 +00:00
|
|
|
CDPSessionEmittedEvents.Disconnected,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#terminate.bind(
|
2022-06-09 17:00:50 +00:00
|
|
|
this,
|
|
|
|
new Error('Navigation failed because browser has disconnected!')
|
|
|
|
)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2020-07-08 10:00:11 +00:00
|
|
|
FrameManagerEmittedEvents.LifecycleEvent,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#checkLifecycleComplete.bind(this)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2020-07-08 10:00:11 +00:00
|
|
|
FrameManagerEmittedEvents.FrameNavigatedWithinDocument,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#navigatedWithinDocument.bind(this)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2022-05-30 08:30:30 +00:00
|
|
|
FrameManagerEmittedEvents.FrameNavigated,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#navigated.bind(this)
|
2022-05-30 08:30:30 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2022-03-09 11:24:17 +00:00
|
|
|
FrameManagerEmittedEvents.FrameSwapped,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameSwapped.bind(this)
|
2022-03-09 11:24:17 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#frameManager,
|
2020-07-08 10:00:11 +00:00
|
|
|
FrameManagerEmittedEvents.FrameDetached,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onFrameDetached.bind(this)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(
|
2022-08-10 21:34:29 +00:00
|
|
|
this.#frameManager.networkManager,
|
2020-07-07 15:43:55 +00:00
|
|
|
NetworkManagerEmittedEvents.Request,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#onRequest.bind(this)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
2022-08-10 15:59:58 +00:00
|
|
|
addEventListener(
|
2022-08-10 21:34:29 +00:00
|
|
|
this.#frameManager.networkManager,
|
2022-08-10 15:59:58 +00:00
|
|
|
NetworkManagerEmittedEvents.Response,
|
|
|
|
this.#onResponse.bind(this)
|
|
|
|
),
|
2019-01-15 04:34:50 +00:00
|
|
|
];
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#timeoutPromise = this.#createTimeoutPromise();
|
|
|
|
this.#checkLifecycleComplete();
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onRequest(request: HTTPRequest): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (request.frame() !== this.#frame || !request.isNavigationRequest()) {
|
2019-01-15 04:34:50 +00:00
|
|
|
return;
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#navigationRequest = request;
|
2022-08-22 14:51:58 +00:00
|
|
|
// Resolve previous navigation response in case there are multiple
|
|
|
|
// navigation requests reported by the backend. This generally should not
|
|
|
|
// happen by it looks like it's possible.
|
|
|
|
this.#navigationResponseReceived?.resolve();
|
2022-08-10 15:59:58 +00:00
|
|
|
this.#navigationResponseReceived = createDeferredPromise();
|
|
|
|
if (request.response() !== null) {
|
|
|
|
this.#navigationResponseReceived?.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#onResponse(response: HTTPResponse): void {
|
|
|
|
if (this.#navigationRequest?._requestId !== response.request()._requestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.#navigationResponseReceived?.resolve();
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#onFrameDetached(frame: Frame): void {
|
|
|
|
if (this.#frame === frame) {
|
|
|
|
this.#terminationCallback.call(
|
2020-05-07 10:54:55 +00:00
|
|
|
null,
|
|
|
|
new Error('Navigating frame was detached')
|
|
|
|
);
|
2019-01-15 04:34:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#checkLifecycleComplete();
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 07:19:14 +00:00
|
|
|
async navigationResponse(): Promise<HTTPResponse | null> {
|
2022-08-10 15:59:58 +00:00
|
|
|
// Continue with a possibly null response.
|
|
|
|
await this.#navigationResponseReceived?.catch(() => {});
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#navigationRequest ? this.#navigationRequest.response() : null;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#terminate(error: Error): void {
|
|
|
|
this.#terminationCallback.call(null, error);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
sameDocumentNavigationPromise(): Promise<Error | undefined> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#sameDocumentNavigationPromise;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
newDocumentNavigationPromise(): Promise<Error | undefined> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#newDocumentNavigationPromise;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 09:03:33 +00:00
|
|
|
lifecyclePromise(): Promise<void> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#lifecyclePromise;
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
timeoutOrTerminationPromise(): Promise<Error | TimeoutError | undefined> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return Promise.race([this.#timeoutPromise, this.#terminationPromise]);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
async #createTimeoutPromise(): Promise<TimeoutError | undefined> {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!this.#timeout) {
|
|
|
|
return new Promise(noop);
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
const errorMessage =
|
2022-06-13 09:16:25 +00:00
|
|
|
'Navigation timeout of ' + this.#timeout + ' ms exceeded';
|
2022-06-22 13:25:44 +00:00
|
|
|
await new Promise(fulfill => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return (this.#maximumTimer = setTimeout(fulfill, this.#timeout));
|
|
|
|
});
|
2022-06-13 09:16:25 +00:00
|
|
|
return new TimeoutError(errorMessage);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#navigatedWithinDocument(frame: Frame): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame !== this.#frame) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#hasSameDocumentNavigation = true;
|
|
|
|
this.#checkLifecycleComplete();
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#navigated(frame: Frame): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame !== this.#frame) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#checkLifecycleComplete();
|
2022-05-30 08:30:30 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#frameSwapped(frame: Frame): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (frame !== this.#frame) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#swapped = true;
|
|
|
|
this.#checkLifecycleComplete();
|
2022-03-09 11:24:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:16:25 +00:00
|
|
|
#checkLifecycleComplete(): void {
|
2019-01-15 04:34:50 +00:00
|
|
|
// We expect navigation to commit.
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!checkLifecycle(this.#frame, this.#expectedLifecycle)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#lifecycleCallback();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#hasSameDocumentNavigation) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#sameDocumentNavigationCompleteCallback();
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-08-01 13:39:08 +00:00
|
|
|
if (this.#swapped || this.#frame._loaderId !== this.#initialLoaderId) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#newDocumentNavigationCompleteCallback();
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
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) {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!frame._lifecycleEvents.has(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
for (const child of frame.childFrames()) {
|
2022-05-17 12:15:44 +00:00
|
|
|
if (
|
|
|
|
child._hasStartedLoading &&
|
|
|
|
!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 {
|
2022-06-14 11:16:21 +00:00
|
|
|
removeEventListeners(this.#eventListeners);
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#maximumTimer !== undefined && clearTimeout(this.#maximumTimer);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
}
|