How to use _checkNavigationComplete method in Puppeteer

Best JavaScript code snippet using puppeteer

Run Puppeteer automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

NavigationWatchdog.js

Source: NavigationWatchdog.js Github

copy
1const {helper} = require('./helper');
2const {Events} = require('./Events');
3
4/**
5 * @internal
6 */
7class NextNavigationWatchdog {
8  constructor(session, navigatedFrame) {
9    this._navigatedFrame = navigatedFrame;
10    this._promise = new Promise(x => this._resolveCallback = x);
11    this._navigation = null;
12    this._eventListeners = [
13      helper.addEventListener(session, 'Page.navigationStarted', this._onNavigationStarted.bind(this)),
14      helper.addEventListener(session, 'Page.sameDocumentNavigation', this._onSameDocumentNavigation.bind(this)),
15    ];
16  }
17
18  promise() {
19    return this._promise;
20  }
21
22  navigation() {
23    return this._navigation;
24  }
25
26  _onNavigationStarted(params) {
27    if (params.frameId === this._navigatedFrame._frameId) {
28      this._navigation = {
29        navigationId: params.navigationId,
30        url: params.url,
31      };
32      this._resolveCallback();
33    }
34  }
35
36  _onSameDocumentNavigation(params) {
37    if (params.frameId === this._navigatedFrame._frameId) {
38      this._navigation = {
39        navigationId: null,
40      };
41      this._resolveCallback();
42    }
43  }
44
45  dispose() {
46    helper.removeEventListeners(this._eventListeners);
47  }
48}
49
50/**
51 * @internal
52 */
53class NavigationWatchdog {
54  constructor(session, navigatedFrame, networkManager, targetNavigationId, targetURL, firedEvents) {
55    this._navigatedFrame = navigatedFrame;
56    this._targetNavigationId = targetNavigationId;
57    this._firedEvents = firedEvents;
58    this._targetURL = targetURL;
59
60    this._promise = new Promise(x => this._resolveCallback = x);
61    this._navigationRequest = null;
62
63    const check = this._checkNavigationComplete.bind(this);
64    this._eventListeners = [
65      helper.addEventListener(session, Events.JugglerSession.Disconnected, () => this._resolveCallback(new Error('Navigation failed because browser has disconnected!'))),
66      helper.addEventListener(session, 'Page.eventFired', check),
67      helper.addEventListener(session, 'Page.frameAttached', check),
68      helper.addEventListener(session, 'Page.frameDetached', check),
69      helper.addEventListener(session, 'Page.navigationStarted', check),
70      helper.addEventListener(session, 'Page.navigationCommitted', check),
71      helper.addEventListener(session, 'Page.navigationAborted', this._onNavigationAborted.bind(this)),
72      helper.addEventListener(networkManager, Events.NetworkManager.Request, this._onRequest.bind(this)),
73      helper.addEventListener(navigatedFrame._frameManager, Events.FrameManager.FrameDetached, check),
74    ];
75    check();
76  }
77
78  _onRequest(request) {
79    if (request.frame() !== this._navigatedFrame || !request.isNavigationRequest())
80      return;
81    this._navigationRequest = request;
82  }
83
84  navigationResponse() {
85    return this._navigationRequest ? this._navigationRequest.response() : null;
86  }
87
88  _checkNavigationComplete() {
89    if (this._navigatedFrame.isDetached()) {
90      this._resolveCallback(new Error('Navigating frame was detached'));
91    } else if (this._navigatedFrame._lastCommittedNavigationId === this._targetNavigationId
92        && checkFiredEvents(this._navigatedFrame, this._firedEvents)) {
93      this._resolveCallback(null);
94    }
95
96    function checkFiredEvents(frame, firedEvents) {
97      for (const subframe of frame._children) {
98        if (!checkFiredEvents(subframe, firedEvents))
99          return false;
100      }
101      return firedEvents.every(event => frame._firedEvents.has(event));
102    }
103  }
104
105  _onNavigationAborted(params) {
106    if (params.frameId === this._navigatedFrame._frameId && params.navigationId === this._targetNavigationId)
107      this._resolveCallback(new Error('Navigation to ' + this._targetURL + ' failed: ' + params.errorText));
108  }
109
110  promise() {
111    return this._promise;
112  }
113
114  dispose() {
115    helper.removeEventListeners(this._eventListeners);
116  }
117}
118
119module.exports = {NavigationWatchdog, NextNavigationWatchdog};
120
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Puppeteer on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)