diff --git a/test/end-to-end/signin-rrdv5.test.js b/test/end-to-end/signin-rrdv5.test.js index 060bc14cf..bb9b1ab8a 100644 --- a/test/end-to-end/signin-rrdv5.test.js +++ b/test/end-to-end/signin-rrdv5.test.js @@ -51,6 +51,7 @@ import { waitForSTElement, backendBeforeEach, setEnabledRecipes, + waitForUrl, } from "../helpers"; import fetch from "isomorphic-fetch"; import { SOMETHING_WENT_WRONG_ERROR } from "../constants"; @@ -290,9 +291,9 @@ describe("SuperTokens SignIn with react router dom v5", function () { // Redirected to onSuccessFulRedirectUrl const onSuccessFulRedirectUrl = "/dashboard"; - let pathname = await page.evaluate(() => window.location.pathname); + // Session.doesSessionExist returns true, allow to stay on /dashboard - assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl); + await waitForUrl(page, onSuccessFulRedirectUrl); await Promise.all([ page.goto(`${TEST_CLIENT_BASE_URL}/dashboard-no-auth`), @@ -330,8 +331,7 @@ describe("SuperTokens SignIn with react router dom v5", function () { page.goto(`${TEST_CLIENT_BASE_URL}/auth`), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/auth"); + await waitForUrl(page, "/auth"); cookies = await page.cookies(); assert.deepStrictEqual(cookies.length, 1); assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update"); @@ -435,9 +435,9 @@ describe("SuperTokens SignIn with react router dom v5", function () { // Redirected to onSuccessFulRedirectUrl const onSuccessFulRedirectUrl = "/dashboard"; - let pathname = await page.evaluate(() => window.location.pathname); + // Session.doesSessionExist returns true, allow to stay on /dashboard - assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl); + await waitForUrl(page, onSuccessFulRedirectUrl); // Test that sessionInfo was fetched successfully using axios and fetch (i.e. Interceptors work) const axiosUserId = await getUserIdWithAxios(page); @@ -459,8 +459,7 @@ describe("SuperTokens SignIn with react router dom v5", function () { // Logout const logoutButton = await getLogoutButton(page); await Promise.all([await logoutButton.click(), page.waitForNavigation({ waitUntil: "networkidle0" })]); - pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/auth"); + await waitForUrl(page, "/auth"); cookies = await page.cookies(); assert.deepStrictEqual(cookies.length, 1); assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update"); @@ -494,8 +493,7 @@ describe("SuperTokens SignIn with react router dom v5", function () { page.goto(`${TEST_CLIENT_BASE_URL}/auth?rid=emailpassword&redirectToPath=%2Fredirect-heree`), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/redirect-heree"); + await waitForUrl(page, "/redirect-heree"); }); it("Successful Sign In with redirect to, redirectToPath directly without trailing slash", async function () { @@ -543,11 +541,7 @@ describe("SuperTokens SignIn with react router dom v5", function () { const backBtn = await waitForSTElement(page, "[data-supertokens~=logoutButton]"); backBtn.click(); - const redirectUrl = await page.evaluate(() => { - return window.location.pathname + window.location.search; - }); - - assert.deepStrictEqual(redirectUrl, "/dashboard"); + await waitForUrl(page, "/dashboard", false); }); describe("Successful Sign In with redirect to, with EmailPasswordAuth", async function () { @@ -572,8 +566,8 @@ describe("SuperTokens SignIn with react router dom v5", function () { submitFormReturnRequestAndResponse(page, SIGN_IN_API), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/redirect-to-this-custom-path"); + await page.waitForSelector(".sessionInfo-user-id"); + await waitForUrl(page, "/redirect-to-this-custom-path"); assert.deepStrictEqual(consoleLogs, [ "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH", "ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS", @@ -611,8 +605,8 @@ describe("SuperTokens SignIn with react router dom v5", function () { submitFormReturnRequestAndResponse(page, SIGN_IN_API), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/CasE/Case-SensItive1-PAth"); + await page.waitForSelector(".sessionInfo-user-id"); + await waitForUrl(page, "/CasE/Case-SensItive1-PAth"); assert.deepStrictEqual(consoleLogs, [ "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH", "ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS", @@ -648,8 +642,7 @@ describe("SuperTokens SignIn with react router dom v5", function () { page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/dashboard"); + await waitForUrl(page, "/dashboard"); }); }); }); diff --git a/test/end-to-end/signin-rrdv6.test.js b/test/end-to-end/signin-rrdv6.test.js index 62bafd122..ab595eba7 100644 --- a/test/end-to-end/signin-rrdv6.test.js +++ b/test/end-to-end/signin-rrdv6.test.js @@ -52,6 +52,7 @@ import { backendBeforeEach, getInvalidClaimsJSON, expectErrorThrown, + waitForUrl, } from "../helpers"; import fetch from "isomorphic-fetch"; import { SOMETHING_WENT_WRONG_ERROR } from "../constants"; @@ -274,9 +275,8 @@ describe("SuperTokens SignIn with react router dom v6", function () { // Redirected to onSuccessFulRedirectUrl const onSuccessFulRedirectUrl = "/dashboard"; - let pathname = await page.evaluate(() => window.location.pathname); // Session.doesSessionExist returns true, allow to stay on /dashboard - assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl); + await waitForUrl(page, onSuccessFulRedirectUrl); await Promise.all([ page.goto(`${TEST_CLIENT_BASE_URL}/dashboard-no-auth`), @@ -314,8 +314,7 @@ describe("SuperTokens SignIn with react router dom v6", function () { page.goto(`${TEST_CLIENT_BASE_URL}/auth`), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/auth"); + await waitForUrl(page, "/auth"); cookies = await page.cookies(); assert.deepStrictEqual(cookies.length, 1); assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update"); @@ -421,9 +420,8 @@ describe("SuperTokens SignIn with react router dom v6", function () { // Redirected to onSuccessFulRedirectUrl const onSuccessFulRedirectUrl = "/dashboard"; - let pathname = await page.evaluate(() => window.location.pathname); // Session.doesSessionExist returns true, allow to stay on /dashboard - assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl); + await waitForUrl(page, onSuccessFulRedirectUrl); // Test that sessionInfo was fetched successfully using axios and fetch (i.e. Interceptors work) const axiosUserId = await getUserIdWithAxios(page); @@ -445,8 +443,7 @@ describe("SuperTokens SignIn with react router dom v6", function () { // Logout const logoutButton = await getLogoutButton(page); await Promise.all([await logoutButton.click(), page.waitForNavigation({ waitUntil: "networkidle0" })]); - pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/auth"); + await waitForUrl(page, "/auth"); cookies = await page.cookies(); assert.deepStrictEqual(cookies.length, 1); assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update"); @@ -481,8 +478,7 @@ describe("SuperTokens SignIn with react router dom v6", function () { page.goto(`${TEST_CLIENT_BASE_URL}/auth?rid=emailpassword&redirectToPath=%2Fredirect-heree`), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/redirect-heree"); + await waitForUrl(page, "/redirect-heree"); }); it("Successful Sign In with redirect to, redirectToPath directly without trailing slash", async function () { @@ -530,11 +526,7 @@ describe("SuperTokens SignIn with react router dom v6", function () { const backBtn = await waitForSTElement(page, "[data-supertokens~=logoutButton]"); backBtn.click(); - const redirectUrl = await page.evaluate(() => { - return window.location.pathname + window.location.search; - }); - - assert.deepStrictEqual(redirectUrl, "/dashboard"); + await waitForUrl(page, "/dashboard", false); }); it("Should not redirect to onFailureRedirections result if it's the current url and set the context", async function () { @@ -720,8 +712,8 @@ describe("SuperTokens SignIn with react router dom v6", function () { submitFormReturnRequestAndResponse(page, SIGN_IN_API), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/redirect-to-this-custom-path"); + await page.waitForSelector(".sessionInfo-user-id"); + await waitForUrl(page, "/redirect-to-this-custom-path"); assert.deepStrictEqual(consoleLogs, [ "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH", "ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS", @@ -760,8 +752,8 @@ describe("SuperTokens SignIn with react router dom v6", function () { submitFormReturnRequestAndResponse(page, SIGN_IN_API), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/CasE/Case-SensItive1-PAth"); + await page.waitForSelector(".sessionInfo-user-id"); + await waitForUrl(page, "/CasE/Case-SensItive1-PAth"); assert.deepStrictEqual(consoleLogs, [ "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH", "ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS", @@ -798,8 +790,7 @@ describe("SuperTokens SignIn with react router dom v6", function () { page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/dashboard"); + await waitForUrl(page, "/dashboard"); }); }); }); diff --git a/test/end-to-end/signin.test.js b/test/end-to-end/signin.test.js index c4408c78d..ccaf9f812 100644 --- a/test/end-to-end/signin.test.js +++ b/test/end-to-end/signin.test.js @@ -55,6 +55,7 @@ import { backendBeforeEach, getInputField, isReact16, + waitForUrl, } from "../helpers"; import fetch from "isomorphic-fetch"; import { SOMETHING_WENT_WRONG_ERROR } from "../constants"; @@ -102,6 +103,7 @@ describe("SuperTokens SignIn", function () { page = await browser.newPage(); page.on("console", (consoleObj) => { const log = consoleObj.text(); + // console.log(log); if (log.startsWith("ST_LOGS")) { consoleLogs.push(log); } @@ -285,9 +287,8 @@ describe("SuperTokens SignIn", function () { // Redirected to onSuccessFulRedirectUrl const onSuccessFulRedirectUrl = "/dashboard"; - let pathname = await page.evaluate(() => window.location.pathname); // Session.doesSessionExist returns true, allow to stay on /dashboard - assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl); + await waitForUrl(page, onSuccessFulRedirectUrl); await Promise.all([ page.goto(`${TEST_CLIENT_BASE_URL}/dashboard-no-auth`), @@ -325,8 +326,7 @@ describe("SuperTokens SignIn", function () { page.goto(`${TEST_CLIENT_BASE_URL}/auth`), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/auth"); + await waitForUrl(page, "/auth"); cookies = await page.cookies(); assert.deepStrictEqual(cookies.length, 1); assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update"); @@ -432,9 +432,8 @@ describe("SuperTokens SignIn", function () { // Redirected to onSuccessFulRedirectUrl const onSuccessFulRedirectUrl = "/dashboard"; - let pathname = await page.evaluate(() => window.location.pathname); // Session.doesSessionExist returns true, allow to stay on /dashboard - assert.deepStrictEqual(pathname, onSuccessFulRedirectUrl); + await waitForUrl(page, onSuccessFulRedirectUrl); // Test that sessionInfo was fetched successfully using axios and fetch (i.e. Interceptors work) const axiosUserId = await getUserIdWithAxios(page); @@ -456,8 +455,7 @@ describe("SuperTokens SignIn", function () { // Logout const logoutButton = await getLogoutButton(page); await Promise.all([await logoutButton.click(), page.waitForNavigation({ waitUntil: "networkidle0" })]); - pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/auth"); + await waitForUrl(page, "/auth"); cookies = await page.cookies(); assert.deepStrictEqual(cookies.length, 1); assert.deepStrictEqual(cookies[0].name, "st-last-access-token-update"); @@ -525,11 +523,7 @@ describe("SuperTokens SignIn", function () { const backBtn = await waitForSTElement(page, "[data-supertokens~=logoutButton]"); backBtn.click(); - const redirectUrl = await page.evaluate(() => { - return window.location.pathname + window.location.search; - }); - - assert.deepStrictEqual(redirectUrl, "/dashboard"); + await waitForUrl(page, "/dashboard", false); }); it("Successful Sign In with redirect to, redirectToPath directly", async function () { @@ -660,8 +654,8 @@ describe("SuperTokens SignIn", function () { submitFormReturnRequestAndResponse(page, SIGN_IN_API), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/redirect-to-this-custom-path"); + await page.waitForSelector(".sessionInfo-user-id"); + await waitForUrl(page, "/redirect-to-this-custom-path"); assert.deepStrictEqual(consoleLogs, [ "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH", "ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS", @@ -700,8 +694,8 @@ describe("SuperTokens SignIn", function () { submitFormReturnRequestAndResponse(page, SIGN_IN_API), page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/CasE/Case-SensItive1-PAth"); + await page.waitForSelector(".sessionInfo-user-id"); + await waitForUrl(page, "/CasE/Case-SensItive1-PAth"); assert.deepStrictEqual(consoleLogs, [ "ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH", "ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS", @@ -738,8 +732,7 @@ describe("SuperTokens SignIn", function () { page.waitForNavigation({ waitUntil: "networkidle0" }), ]); - const pathname = await page.evaluate(() => window.location.pathname); - assert.deepStrictEqual(pathname, "/dashboard"); + await waitForUrl(page, "/dashboard"); }); }); }); diff --git a/test/helpers.js b/test/helpers.js index 13e5b1423..22d6976be 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -67,28 +67,54 @@ export async function waitFor(ms) { */ export async function waitForSTElement(page, selector, inverted = false) { - await page.waitForSelector(ST_ROOT_SELECTOR); - const res = await page.waitForFunction( - (elementSelector, rootSelector, inverted) => { - const root = document.querySelector(rootSelector); - if (!root || !root.shadowRoot) { - return false; + const start = Date.now(); + while (start > Date.now() - 30000) { + try { + await page.waitForSelector(ST_ROOT_SELECTOR); + const res = await page.waitForFunction( + (elementSelector, rootSelector, inverted) => { + const root = document.querySelector(rootSelector); + if (!root || !root.shadowRoot) { + return false; + } + if (elementSelector === undefined) { + return true; + } + const elem = root.shadowRoot.querySelector(elementSelector); + return inverted ? elem === null : elem; + }, + { polling: 50 }, + selector, + ST_ROOT_SELECTOR, + inverted + ); + if (res) { + return res.asElement(); } - if (elementSelector === undefined) { - return true; + return res; + } catch (ex) { + if (start >= Date.now() - 30000) { + throw ex; } - const elem = root.shadowRoot.querySelector(elementSelector); - return inverted ? elem === null : elem; + console.log("Caught exception in waitForSTElement, but retrying until timeout", ex); + } + } + throw new Error(`Waiting for ${selector} to ${inverted ? "disappear" : "appear"} timed out`); +} + +export function waitForUrl(page, url, onlyPath = true) { + return page.waitForFunction( + (pathname, onlyPath) => { + return ( + (onlyPath + ? window.location.pathname + : window.location.pathname + window.location.search + window.location.hash) === pathname + ); }, { polling: 50 }, - selector, - ST_ROOT_SELECTOR, - inverted + url, + onlyPath ); - if (res) { - return res.asElement(); - } - return res; } export async function waitForText(page, selector, text, timeout = 10000, pollDelay = 50) {