From 6a7e90c6cadad80d71497b69598e236c3b19277a Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 14 Oct 2021 19:01:29 +0200 Subject: [PATCH] e2e-test: improve page content checking resilience Signed-off-by: Patrik Oldsberg --- packages/e2e-test/src/lib/helpers.ts | 35 +++++++++------------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/packages/e2e-test/src/lib/helpers.ts b/packages/e2e-test/src/lib/helpers.ts index 2182a9d324..db6c31512c 100644 --- a/packages/e2e-test/src/lib/helpers.ts +++ b/packages/e2e-test/src/lib/helpers.ts @@ -129,13 +129,22 @@ export async function waitForPageWithText( browser: any, path: string, text: string, - { intervalMs = 1000, maxLoadAttempts = 240, maxFindTextAttempts = 3 } = {}, + { intervalMs = 1000, maxLoadAttempts = 50 } = {}, ) { let loadAttempts = 0; for (;;) { try { - await new Promise(resolve => setTimeout(resolve, intervalMs)); + const waitTimeMs = intervalMs * (Math.log10(loadAttempts + 1) + 1); + console.log(`Attempting to load page at ${path}, waiting ${waitTimeMs}`); + await new Promise(resolve => setTimeout(resolve, waitTimeMs)); await browser.visit(path); + + const escapedText = text.replace(/"|\\/g, '\\$&'); + browser.assert.evaluate( + `Array.from(document.querySelectorAll("*")).some(el => el.textContent === "${escapedText}")`, + true, + `expected to find text ${text}`, + ); break; } catch (error) { if (error.message.match(EXPECTED_LOAD_ERRORS)) { @@ -150,28 +159,6 @@ export async function waitForPageWithText( } } } - - // The page may not be fully loaded and hence we need to retry. - let findTextAttempts = 0; - const escapedText = text.replace(/"|\\/g, '\\$&'); - for (;;) { - try { - browser.assert.evaluate( - `Array.from(document.querySelectorAll("*")).some(el => el.textContent === "${escapedText}")`, - true, - `expected to find text ${text}`, - ); - break; - } catch (error) { - findTextAttempts++; - if (findTextAttempts <= maxFindTextAttempts) { - await new Promise(resolve => setTimeout(resolve, intervalMs)); - continue; - } else { - throw error; - } - } - } } export function print(msg: string) {