From fe2f7265d3b72f8d2d4ca87214ce1ecc460d499b Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Tue, 5 May 2020 15:27:16 +0900 Subject: [PATCH 1/7] Add retries to find the text on page for e2e tests The page may not be fully loaded on the first go. Hence, we wait for some time and then retry. --- packages/cli/e2e-test/helpers.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/cli/e2e-test/helpers.js b/packages/cli/e2e-test/helpers.js index 7e34f10a63..2745c3269b 100644 --- a/packages/cli/e2e-test/helpers.js +++ b/packages/cli/e2e-test/helpers.js @@ -97,7 +97,7 @@ async function waitForPageWithText( text, { intervalMs = 1000, maxAttempts = 240 } = {}, ) { - let attempts = 0; + let attemptsToLoad = 0; for (;;) { try { await new Promise(resolve => setTimeout(resolve, intervalMs)); @@ -105,8 +105,8 @@ async function waitForPageWithText( break; } catch (error) { if (error.message.match(EXPECTED_LOAD_ERRORS)) { - attempts++; - if (attempts > maxAttempts) { + attemptsToLoad++; + if (attemptsToLoad > maxAttempts) { throw new Error( `Failed to load page '${path}', max number of attempts reached`, ); @@ -117,12 +117,28 @@ async function waitForPageWithText( } } + // The page may not be fully loaded and hence we need to retry. + const maxAttemptsToSearchText = 3; + let attemptsToSearchText = 0; const escapedText = text.replace(/"/g, '\\"'); - browser.assert.evaluate( - `Array.from(document.querySelectorAll("*")).some(el => el.textContent === "${escapedText}")`, - true, - `expected to find text ${text}`, - ); + for (;;) { + try { + browser.assert.evaluate( + `Array.from(document.querySelectorAll("*")).some(el => el.textContent === "${escapedText}")`, + true, + `expected to find text ${text}`, + ); + } catch (error) { + if (error instanceof browser.assert.AssertionError) { + attemptsToSearchText++; + if (attemptsToSearchText <= maxAttemptsToSearchText) { + await new Promise(resolve => setTimeout(resolve, intervalMs)); + continue + } + } + throw error; + } + } } function print(msg) { From a93f194cb6526f66d88420d15ecfdf888a3ad28f Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Tue, 5 May 2020 17:28:43 +0900 Subject: [PATCH 2/7] Break from loop if the test is successful Making this mistake since while loops were created :( --- packages/cli/e2e-test/helpers.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/cli/e2e-test/helpers.js b/packages/cli/e2e-test/helpers.js index 2745c3269b..d676662de9 100644 --- a/packages/cli/e2e-test/helpers.js +++ b/packages/cli/e2e-test/helpers.js @@ -128,6 +128,7 @@ async function waitForPageWithText( true, `expected to find text ${text}`, ); + break; } catch (error) { if (error instanceof browser.assert.AssertionError) { attemptsToSearchText++; From fe088038d6e9f1592d08f1f6c4088fa89ebab72a Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Tue, 5 May 2020 20:06:16 +0900 Subject: [PATCH 3/7] Better variable names attemptsToLoad -> loadAttempts maxAttempts -> maxLoadAttempts attemptsToSearchText -> findTextAttempts maxAttemptsToSearchText -> maxFindTextAttempts --- packages/cli/e2e-test/helpers.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/cli/e2e-test/helpers.js b/packages/cli/e2e-test/helpers.js index d676662de9..08febada30 100644 --- a/packages/cli/e2e-test/helpers.js +++ b/packages/cli/e2e-test/helpers.js @@ -95,9 +95,9 @@ async function waitForPageWithText( browser, path, text, - { intervalMs = 1000, maxAttempts = 240 } = {}, + { intervalMs = 1000, maxLoadAttempts = 240 } = {}, ) { - let attemptsToLoad = 0; + let loadAttempts = 0; for (;;) { try { await new Promise(resolve => setTimeout(resolve, intervalMs)); @@ -105,8 +105,8 @@ async function waitForPageWithText( break; } catch (error) { if (error.message.match(EXPECTED_LOAD_ERRORS)) { - attemptsToLoad++; - if (attemptsToLoad > maxAttempts) { + loadAttempts++; + if (loadAttempts > maxLoadAttempts) { throw new Error( `Failed to load page '${path}', max number of attempts reached`, ); @@ -118,8 +118,8 @@ async function waitForPageWithText( } // The page may not be fully loaded and hence we need to retry. - const maxAttemptsToSearchText = 3; - let attemptsToSearchText = 0; + const maxFindTextAttempts = 3; + let findTextAttempts = 0; const escapedText = text.replace(/"/g, '\\"'); for (;;) { try { @@ -131,8 +131,8 @@ async function waitForPageWithText( break; } catch (error) { if (error instanceof browser.assert.AssertionError) { - attemptsToSearchText++; - if (attemptsToSearchText <= maxAttemptsToSearchText) { + findTextAttempts++; + if (findTextAttempts <= maxFindTextAttempts) { await new Promise(resolve => setTimeout(resolve, intervalMs)); continue } From 9ea93173f0f7b996e1c49fbc962a831053482461 Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Tue, 5 May 2020 20:07:43 +0900 Subject: [PATCH 4/7] Do browser.visit again after getting error in finding text --- packages/cli/e2e-test/helpers.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/cli/e2e-test/helpers.js b/packages/cli/e2e-test/helpers.js index 08febada30..073248789e 100644 --- a/packages/cli/e2e-test/helpers.js +++ b/packages/cli/e2e-test/helpers.js @@ -133,6 +133,7 @@ async function waitForPageWithText( if (error instanceof browser.assert.AssertionError) { findTextAttempts++; if (findTextAttempts <= maxFindTextAttempts) { + await browser.visit(path); await new Promise(resolve => setTimeout(resolve, intervalMs)); continue } From 82b6f1e4cba98a2b86c8f303bf95213259831428 Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Tue, 5 May 2020 20:09:03 +0900 Subject: [PATCH 5/7] Retry search text for any type of error --- packages/cli/e2e-test/helpers.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/cli/e2e-test/helpers.js b/packages/cli/e2e-test/helpers.js index 073248789e..4d3f4f4661 100644 --- a/packages/cli/e2e-test/helpers.js +++ b/packages/cli/e2e-test/helpers.js @@ -130,15 +130,14 @@ async function waitForPageWithText( ); break; } catch (error) { - if (error instanceof browser.assert.AssertionError) { - findTextAttempts++; - if (findTextAttempts <= maxFindTextAttempts) { - await browser.visit(path); - await new Promise(resolve => setTimeout(resolve, intervalMs)); - continue - } + findTextAttempts++; + if (findTextAttempts <= maxFindTextAttempts) { + await browser.visit(path); + await new Promise(resolve => setTimeout(resolve, intervalMs)); + continue + } else { + throw error; } - throw error; } } } From 5d5d093b20185f8464b5a410507c0e8e9bbb681d Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Tue, 5 May 2020 20:38:57 +0900 Subject: [PATCH 6/7] Set maxFindTextAttempts as a function argument --- packages/cli/e2e-test/helpers.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/cli/e2e-test/helpers.js b/packages/cli/e2e-test/helpers.js index 4d3f4f4661..30e4770c01 100644 --- a/packages/cli/e2e-test/helpers.js +++ b/packages/cli/e2e-test/helpers.js @@ -95,7 +95,7 @@ async function waitForPageWithText( browser, path, text, - { intervalMs = 1000, maxLoadAttempts = 240 } = {}, + { intervalMs = 1000, maxLoadAttempts = 240, maxFindTextAttempts = 3 } = {}, ) { let loadAttempts = 0; for (;;) { @@ -118,7 +118,6 @@ async function waitForPageWithText( } // The page may not be fully loaded and hence we need to retry. - const maxFindTextAttempts = 3; let findTextAttempts = 0; const escapedText = text.replace(/"/g, '\\"'); for (;;) { From b36bf68d7da20ecc5d7bf1dd886611aaeaafb5f9 Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Tue, 5 May 2020 20:42:44 +0900 Subject: [PATCH 7/7] 240 attempts should not just be 239 :D --- packages/cli/e2e-test/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/e2e-test/helpers.js b/packages/cli/e2e-test/helpers.js index 30e4770c01..ed3b7914cc 100644 --- a/packages/cli/e2e-test/helpers.js +++ b/packages/cli/e2e-test/helpers.js @@ -106,7 +106,7 @@ async function waitForPageWithText( } catch (error) { if (error.message.match(EXPECTED_LOAD_ERRORS)) { loadAttempts++; - if (loadAttempts > maxLoadAttempts) { + if (loadAttempts >= maxLoadAttempts) { throw new Error( `Failed to load page '${path}', max number of attempts reached`, );