How to use _getDelayMsForRetry method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

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

protocol.js

Source: protocol.js Github

copy
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getWsTargetFor = exports.getRemoteDebuggingPort = exports._connectAsync = exports._getDelayMsForRetry = void 0;
4const tslib_1 = require("tslib");
5const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
6const chrome_remote_interface_1 = (0, tslib_1.__importDefault)(require("chrome-remote-interface"));
7const network_1 = require("../../../network");
8const bluebird_1 = (0, tslib_1.__importDefault)(require("bluebird"));
9const lazy_ass_1 = (0, tslib_1.__importDefault)(require("lazy-ass"));
10const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
11const utils_1 = (0, tslib_1.__importDefault)(require("./utils"));
12const errors = require('../errors');
13const is = require('check-more-types');
14const debug = (0, debug_1.default)('cypress:server:browsers:protocol');
15function _getDelayMsForRetry(i, browserName) {
16    if (i < 10) {
17        return 100;
18    }
19    if (i < 18) {
20        return 500;
21    }
22    if (i < 63) { // after 5 seconds, begin logging and retrying
23        errors.warning('CDP_RETRYING_CONNECTION', i, browserName);
24        return 1000;
25    }
26    return;
27}
28exports._getDelayMsForRetry = _getDelayMsForRetry;
29function _connectAsync(opts) {
30    return bluebird_1.default.fromCallback((cb) => {
31        network_1.connect.createRetryingSocket(opts, cb);
32    })
33        .then((sock) => {
34        // can be closed, just needed to test the connection
35        sock.end();
36    });
37}
38exports._connectAsync = _connectAsync;
39/**
40 * Tries to find the starting page (probably blank tab)
41 * among all targets returned by CRI.List call.
42 *
43 * @returns {string} web socket debugger url
44 */
45const findStartPage = (targets) => {
46    debug('CRI List %o', { numTargets: targets.length, targets });
47    // activate the first available id
48    // find the first target page that's a real tab
49    // and not the dev tools or background page.
50    // since we open a blank page first, it has a special url
51    const newTabTargetFields = {
52        type: 'page',
53        url: 'about:blank',
54    };
55    const target = lodash_1.default.find(targets, newTabTargetFields);
56    (0, lazy_ass_1.default)(target, 'could not find CRI target');
57    debug('found CRI target %o', target);
58    return target.webSocketDebuggerUrl;
59};
60const findStartPageTarget = (connectOpts) => {
61    debug('CRI.List %o', connectOpts);
62    // what happens if the next call throws an error?
63    // it seems to leave the browser instance open
64    // need to clone connectOpts, CRI modifies it
65    return chrome_remote_interface_1.default.List(lodash_1.default.clone(connectOpts)).then(findStartPage);
66};
67function getRemoteDebuggingPort() {
68    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
69        const port = Number(process.env.CYPRESS_REMOTE_DEBUGGING_PORT);
70        return port || utils_1.default.getPort();
71    });
72}
73exports.getRemoteDebuggingPort = getRemoteDebuggingPort;
74/**
75 * Waits for the port to respond with connection to Chrome Remote Interface
76 * @param {number} port Port number to connect to
77 * @param {string} browserName Browser name, for warning/error messages
78 */
79const getWsTargetFor = (port, browserName) => {
80    debug('Getting WS connection to CRI on port %d', port);
81    (0, lazy_ass_1.default)(is.port(port), 'expected port number', port);
82    let retryIndex = 0;
83    // force ipv4
84    // https://github.com/cypress-io/cypress/issues/5912
85    const connectOpts = {
86        host: '127.0.0.1',
87        port,
88        getDelayMsForRetry: (i) => {
89            retryIndex = i;
90            return _getDelayMsForRetry(i, browserName);
91        },
92    };
93    return _connectAsync(connectOpts)
94        .then(() => {
95        const retry = () => {
96            debug('attempting to find CRI target... %o', { retryIndex });
97            return findStartPageTarget(connectOpts)
98                .catch((err) => {
99                retryIndex++;
100                const delay = _getDelayMsForRetry(retryIndex, browserName);
101                debug('error finding CRI target, maybe retrying %o', { delay, err });
102                if (typeof delay === 'undefined') {
103                    throw err;
104                }
105                return bluebird_1.default.delay(delay)
106                    .then(retry);
107            });
108        };
109        return retry();
110    })
111        .catch((err) => {
112        debug('failed to connect to CDP %o', { connectOpts, err });
113        errors.throw('CDP_COULD_NOT_CONNECT', port, err, browserName);
114    });
115};
116exports.getWsTargetFor = getWsTargetFor;
117
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 Cypress 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)