From 415a1acd8b86a3ee369fd21eab7b829bbac6b6d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mih=C3=A1ly=20Lengyel?= Date: Fri, 24 May 2024 18:40:12 +0200 Subject: [PATCH] test: make tests wait for redirection (#822) --- test/end-to-end/passwordless.test.js | 14 ++++++-------- test/helpers.js | 10 ++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/test/end-to-end/passwordless.test.js b/test/end-to-end/passwordless.test.js index 90f33a534..dc2825591 100644 --- a/test/end-to-end/passwordless.test.js +++ b/test/end-to-end/passwordless.test.js @@ -36,6 +36,7 @@ import { getInputField, isAccountLinkingSupported, backendBeforeEach, + waitForUrl, } from "../helpers"; import { TEST_CLIENT_BASE_URL, TEST_SERVER_BASE_URL, SOMETHING_WENT_WRONG_ERROR } from "../constants"; @@ -490,8 +491,7 @@ export function getPasswordlessTestCases({ authRecipe, logId, generalErrorRecipe await page.waitForNavigation({ waitUntil: "networkidle0" }); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/redirect-here"); + await waitForUrl(page, "/redirect-here"); assert.deepStrictEqual(consoleLogs, [ "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH", "ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS", @@ -1032,8 +1032,8 @@ export function getPasswordlessTestCases({ authRecipe, logId, generalErrorRecipe await page.goto(device.codes[0].urlWithLinkCode); await page.waitForNavigation({ waitUntil: "networkidle0" }); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/redirect-here"); + await waitForUrl(page, "/redirect-here"); + assert.deepStrictEqual(consoleLogs, [ "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH", "ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS", @@ -1074,8 +1074,7 @@ export function getPasswordlessTestCases({ authRecipe, logId, generalErrorRecipe await page.goto(device.codes[0].urlWithLinkCode); await page.waitForNavigation({ waitUntil: "networkidle0" }); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/redirect-here"); + await waitForUrl(page, "/redirect-here"); assert.deepStrictEqual(consoleLogs, [ "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH", "ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS", @@ -1534,8 +1533,7 @@ export function getPasswordlessTestCases({ authRecipe, logId, generalErrorRecipe await submitForm(page); await page.waitForNavigation({ waitUntil: "networkidle0" }); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/redirect-here"); + await waitForUrl(page, "/redirect-here"); assert.deepStrictEqual(consoleLogs, [ "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH", diff --git a/test/helpers.js b/test/helpers.js index b34c5926b..dac4629d7 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -91,6 +91,16 @@ export async function waitForSTElement(page, selector, inverted = false) { return res; } +export function waitForUrl(page, pathname) { + return page.waitForFunction( + (pathname) => { + return window.location.pathname === pathname; + }, + { polling: 50 }, + pathname + ); +} + export async function waitForText(page, selector, text, timeout = 10000, pollDelay = 50) { const start = new Date().getTime();