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-07-13 09:22:26 +00:00
|
|
|
import { assert } from './assert.js';
|
|
|
|
import { helper, PuppeteerEventListener } from './helper.js';
|
|
|
|
import { TimeoutError } from './Errors.js';
|
|
|
|
import {
|
|
|
|
FrameManager,
|
|
|
|
Frame,
|
|
|
|
FrameManagerEmittedEvents,
|
|
|
|
} from './FrameManager.js';
|
|
|
|
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 {
|
|
|
|
_expectedLifecycle: ProtocolLifeCycleEvent[];
|
2020-04-29 11:28:16 +00:00
|
|
|
_frameManager: FrameManager;
|
|
|
|
_frame: Frame;
|
2020-04-27 09:03:33 +00:00
|
|
|
_timeout: number;
|
2022-05-24 11:14:19 +00:00
|
|
|
_navigationRequest: HTTPRequest | null = null;
|
2020-04-27 09:03:33 +00:00
|
|
|
_eventListeners: PuppeteerEventListener[];
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
_sameDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
|
|
|
|
_sameDocumentNavigationPromise = new Promise<Error | undefined>((fulfill) => {
|
|
|
|
this._sameDocumentNavigationCompleteCallback = fulfill;
|
|
|
|
});
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
_lifecycleCallback: () => void = noop;
|
|
|
|
_lifecyclePromise: Promise<void> = new Promise((fulfill) => {
|
|
|
|
this._lifecycleCallback = fulfill;
|
|
|
|
});
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
_newDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
|
|
|
|
_newDocumentNavigationPromise: Promise<Error | undefined> = new Promise(
|
|
|
|
(fulfill) => {
|
|
|
|
this._newDocumentNavigationCompleteCallback = fulfill;
|
|
|
|
}
|
|
|
|
);
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
_terminationCallback: (x?: Error) => void = noop;
|
|
|
|
_terminationPromise: Promise<Error | undefined> = new Promise((fulfill) => {
|
|
|
|
this._terminationCallback = fulfill;
|
|
|
|
});
|
2020-04-27 09:03:33 +00:00
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
_timeoutPromise: Promise<TimeoutError | undefined>;
|
2020-04-27 09:03:33 +00:00
|
|
|
|
|
|
|
_maximumTimer?: NodeJS.Timeout;
|
|
|
|
_hasSameDocumentNavigation?: boolean;
|
2022-05-30 08:30:30 +00:00
|
|
|
_newDocumentNavigation?: boolean;
|
2022-03-09 11:24:17 +00:00
|
|
|
_swapped?: boolean;
|
2020-04-27 09:03:33 +00:00
|
|
|
|
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);
|
2022-05-24 11:14:19 +00:00
|
|
|
return protocolEvent as ProtocolLifeCycleEvent;
|
2019-01-15 04:34:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this._frameManager = frameManager;
|
|
|
|
this._frame = frame;
|
|
|
|
this._timeout = timeout;
|
|
|
|
this._eventListeners = [
|
2020-05-07 10:54:55 +00:00
|
|
|
helper.addEventListener(
|
|
|
|
frameManager._client,
|
2020-07-08 10:11:01 +00:00
|
|
|
CDPSessionEmittedEvents.Disconnected,
|
2022-06-09 17:00:50 +00:00
|
|
|
this._terminate.bind(
|
|
|
|
this,
|
|
|
|
new Error('Navigation failed because browser has disconnected!')
|
|
|
|
)
|
2020-05-07 10:54:55 +00:00
|
|
|
),
|
|
|
|
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)
|
|
|
|
),
|
2022-05-30 08:30:30 +00:00
|
|
|
helper.addEventListener(
|
|
|
|
this._frameManager,
|
|
|
|
FrameManagerEmittedEvents.FrameNavigated,
|
|
|
|
this._navigated.bind(this)
|
|
|
|
),
|
2022-03-09 11:24:17 +00:00
|
|
|
helper.addEventListener(
|
|
|
|
this._frameManager,
|
|
|
|
FrameManagerEmittedEvents.FrameSwapped,
|
|
|
|
this._frameSwapped.bind(this)
|
|
|
|
),
|
2020-05-07 10:54:55 +00:00
|
|
|
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
|
|
|
];
|
|
|
|
|
|
|
|
this._timeoutPromise = this._createTimeoutPromise();
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-11-23 07:19:14 +00:00
|
|
|
async navigationResponse(): Promise<HTTPResponse | null> {
|
|
|
|
// We may need to wait for ExtraInfo events before the request is complete.
|
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);
|
|
|
|
}
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
sameDocumentNavigationPromise(): Promise<Error | undefined> {
|
2019-01-15 04:34:50 +00:00
|
|
|
return this._sameDocumentNavigationPromise;
|
|
|
|
}
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
newDocumentNavigationPromise(): Promise<Error | undefined> {
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
timeoutOrTerminationPromise(): Promise<Error | TimeoutError | undefined> {
|
2019-01-15 04:34:50 +00:00
|
|
|
return Promise.race([this._timeoutPromise, this._terminationPromise]);
|
|
|
|
}
|
|
|
|
|
2022-05-24 11:14:19 +00:00
|
|
|
_createTimeoutPromise(): Promise<TimeoutError | undefined> {
|
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();
|
|
|
|
}
|
|
|
|
|
2022-05-30 08:30:30 +00:00
|
|
|
_navigated(frame: Frame): void {
|
|
|
|
if (frame !== this._frame) return;
|
|
|
|
this._newDocumentNavigation = true;
|
|
|
|
this._checkLifecycleComplete();
|
|
|
|
}
|
|
|
|
|
2022-03-09 11:24:17 +00:00
|
|
|
_frameSwapped(frame: Frame): void {
|
|
|
|
if (frame !== this._frame) return;
|
|
|
|
this._swapped = 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();
|
|
|
|
if (this._hasSameDocumentNavigation)
|
|
|
|
this._sameDocumentNavigationCompleteCallback();
|
2022-05-30 08:30:30 +00:00
|
|
|
if (this._swapped || this._newDocumentNavigation)
|
2019-01-15 04:34:50 +00:00
|
|
|
this._newDocumentNavigationCompleteCallback();
|
|
|
|
|
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()) {
|
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 {
|
2019-01-15 04:34:50 +00:00
|
|
|
helper.removeEventListeners(this._eventListeners);
|
2022-05-24 11:14:19 +00:00
|
|
|
this._maximumTimer !== undefined && clearTimeout(this._maximumTimer);
|
2019-01-15 04:34:50 +00:00
|
|
|
}
|
|
|
|
}
|