Error: function timed out, ensure the promise resolves within XXX, due to promises in SEQUENTIAL using reduce/Promise.resolve(nightwatch cucumber)
23:35 08 Aug 2024

Nightwatch with Cucumber. I want to check if the elements listed in an Excel sheet are present on the webpage. To do this, a sequential promise(using reduce) is written to ensure that the page loads and each element is verified.

When test is ran, the element verification works fine using waitForElementVisible if the element is present . When an element is not present, "Timed out while waiting for element to be present for 60000 milliseconds"" is to be encountered.

Since reduce/Promise.resolve() is used, unless it iterates over all the records, the promise is not resolved. If sheetData is small, the below code works. However, for more records it leads to this function time out.

const sheetData = file[sheetName];
        return sheetData
            .reduce((promiseChain, record, index) => {
                return promiseChain.then(() => {
                    return page.load(param)// Adjust timeout as needed
                        .then(() => {
                            return page.waitForPageLoad();
                        })
                        .then(async() => {
                            await client.waitForElementVisible(record["Locator"]);


                    })
                    .then(() => {
                        results.push({ "status": "fulfilled", index: index + 1 });
                    })
                    .catch((error) => {
                        results.push({ "status": "rejected", index: index + 1, error});
                        return Promise.resolve(); 
                    });
            });
        }, Promise.resolve())
        .then(() => {
            const failures = results.filter((result) => result.status === "rejected");
            if (failures.length > 0) return Promise.reject(failures);
            return Promise.resolve(true); 
        }).catch((finalError) => {
            console.error("Final errors encountered: ", finalError);
        });

As the list of elements/pages to verify are dynamically changes(adobe analytics), I cannot give timeout at step level(as suggested in few answers), as this depends on the excel records and any step level time out may stop reading records.

Can you please suggest how to bypass the "function timeout" error and instead only track the elements that are not available, continuing to check for the remaining elements even if one is not found?

javascript async-await promise cucumber nightwatch.js