e2e-test: improve page content checking resilience

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-10-14 19:01:29 +02:00
parent efdefe673f
commit 6a7e90c6ca
+11 -24
View File
@@ -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) {