fix: robust auth setup and login helpers for E2E analytics tests

This commit is contained in:
ashim-hq
2026-04-23 12:08:03 +08:00
parent d6a977e149
commit d973ab61ee
3 changed files with 28 additions and 17 deletions
+12 -5
View File
@@ -8,11 +8,20 @@ import { expect, test } from "@playwright/test";
// given/declined, the user's state changes. Tests run serially and
// each builds on the state left by the previous test.
async function loginFresh(page: import("@playwright/test").Page) {
async function loginAndGetToHome(page: import("@playwright/test").Page) {
await page.goto("/login");
await page.getByLabel("Username").fill("admin");
await page.getByLabel("Password").fill("admin");
await page.getByRole("button", { name: /login/i }).click();
// May hit consent page or go straight to home
try {
const acceptBtn = page.getByRole("button", { name: /sure, sounds good/i });
await acceptBtn.waitFor({ state: "visible", timeout: 5_000 });
await acceptBtn.click();
await page.waitForURL("/", { timeout: 30_000 });
} catch {
await page.waitForURL("/", { timeout: 30_000 });
}
}
test.describe("Analytics consent page", () => {
@@ -25,8 +34,7 @@ test.describe("Analytics consent page", () => {
// The auth setup project already accepted analytics consent for the admin
// user, so a fresh browser session logging in as admin should go straight
// to the home page without being redirected to /analytics-consent.
await loginFresh(page);
await page.waitForURL("/", { timeout: 30_000 });
await loginAndGetToHome(page);
await expect(page).toHaveURL("/");
// Navigate away and back — consent page should NOT reappear
@@ -50,8 +58,7 @@ test.describe("Analytics consent page", () => {
test("settings toggle works after accepting analytics", async ({ page }) => {
// User already accepted in previous test — login should go straight to home
await loginFresh(page);
await page.waitForURL("/", { timeout: 30_000 });
await loginAndGetToHome(page);
await expect(page).toHaveURL("/");
// Open Settings dialog — look for the gear icon or settings button
@@ -36,11 +36,13 @@ async function loginFresh(page: import("@playwright/test").Page) {
await page.getByLabel("Password").fill("admin");
await page.getByRole("button", { name: /login/i }).click();
// May land on "/" or "/analytics-consent" depending on user state
await page.waitForURL(/\/(analytics-consent)?$/, { timeout: 30_000 });
if (page.url().includes("/analytics-consent")) {
// Accept consent so the test can proceed to the home page
await page.getByRole("button", { name: /sure, sounds good/i }).click();
await page.waitForURL("/", { timeout: 15_000 });
try {
const acceptBtn = page.getByRole("button", { name: /sure, sounds good/i });
await acceptBtn.waitFor({ state: "visible", timeout: 5_000 });
await acceptBtn.click();
await page.waitForURL("/", { timeout: 30_000 });
} catch {
await page.waitForURL("/", { timeout: 30_000 });
}
}
+9 -7
View File
@@ -1,9 +1,7 @@
import path from "node:path";
import { expect, test as setup } from "@playwright/test";
const authFile =
// Support both playwright.docker.config and playwright.analytics.config
path.join(__dirname, "..", "..", "test-results", ".auth", "analytics-user.json");
const authFile = path.join(__dirname, "..", "..", "test-results", ".auth", "analytics-user.json");
setup("authenticate", async ({ page }) => {
await page.goto("/login");
@@ -11,15 +9,19 @@ setup("authenticate", async ({ page }) => {
await page.getByLabel("Password").fill("admin");
await page.getByRole("button", { name: /login/i }).click();
// After login, may land on "/" or "/analytics-consent" (fresh user)
await page.waitForURL(/\/(analytics-consent)?$/, { timeout: 30_000 });
// Wait for login to complete — page leaves "/login"
await page.waitForURL((url) => !url.pathname.startsWith("/login"), {
timeout: 30_000,
});
// If redirected to consent page, accept analytics to proceed
// If we landed on the consent page, accept it
if (page.url().includes("/analytics-consent")) {
await page.getByRole("button", { name: /sure, sounds good/i }).click();
await page.waitForURL("/", { timeout: 15_000 });
// Consent page does window.location.href = "/" (full reload)
await page.waitForURL("/", { timeout: 30_000 });
}
// At this point we should be on the home page
await expect(page).toHaveURL("/");
await page.context().storageState({ path: authFile });
});