diff --git a/lib/helper/Playwright.js b/lib/helper/Playwright.js index dfc7b855d..ef91c8d67 100644 --- a/lib/helper/Playwright.js +++ b/lib/helper/Playwright.js @@ -2801,22 +2801,37 @@ class Playwright extends Helper { // We apply 2 strategies here: wait for text as innert text on page (wide strategy) - older // or we use native Playwright matcher to wait for text in element (narrow strategy) - newer // If a user waits for text on a page they are mostly expect it to be there, so wide strategy can be helpful even PW strategy is available - return Promise.race([ + + // Use a flag to stop retries when race resolves + let shouldStop = false + let timeoutId + + const racePromise = Promise.race([ new Promise((_, reject) => { - setTimeout(() => reject(errorMessage), waitTimeout) + timeoutId = setTimeout(() => reject(errorMessage), waitTimeout) }), this.page.waitForFunction(text => document.body && document.body.innerText.indexOf(text) > -1, text, { timeout: timeoutGap }), promiseRetry( - async retry => { + async (retry, number) => { + // Stop retrying if race has resolved + if (shouldStop) { + throw new Error('Operation cancelled') + } const textPresent = await contextObject .locator(`:has-text(${JSON.stringify(text)})`) .first() .isVisible() if (!textPresent) retry(errorMessage) }, - { retries: 1000, minTimeout: 500, maxTimeout: 500, factor: 1 }, + { retries: 10, minTimeout: 100, maxTimeout: 500, factor: 1.5 }, ), ]) + + // Clean up when race resolves/rejects + return racePromise.finally(() => { + if (timeoutId) clearTimeout(timeoutId) + shouldStop = true + }) } /**