geo/frontend/e2e/fixtures/auth.ts

54 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { test as base, type Page } from "@playwright/test";
import { LoginPage } from "../pages/login.page";
const TEST_USER = {
email: process.env.E2E_TEST_EMAIL || "admin@example.com",
password: process.env.E2E_TEST_PASSWORD || "admin@123",
};
type AuthFixtures = {
authenticatedPage: Page;
};
/**
* 扩展 Playwright test提供 authenticatedPage fixture。
* authenticatedPage 是一个已完成登录认证的 Page 对象,
* 自动处理登录流程、onboarding 跳过、以及错误重试。
*/
export const test = base.extend<AuthFixtures>({
authenticatedPage: async ({ page }, use) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.login(TEST_USER.email, TEST_USER.password);
try {
await page.waitForURL(/\/(dashboard|onboarding)/, { timeout: 30000 });
} catch {
const errorMsg = page.getByText("邮箱或密码错误");
if (await errorMsg.isVisible({ timeout: 2000 }).catch(() => false)) {
await loginPage.login(TEST_USER.email, TEST_USER.password);
await page.waitForURL(/\/(dashboard|onboarding)/, { timeout: 30000 });
} else {
await page.goto("/dashboard");
await page.waitForURL(/\/dashboard/, { timeout: 15000 });
}
}
if (page.url().includes("/onboarding")) {
const skipBtn = page.getByRole("button", { name: /跳过/ }).first();
if (await skipBtn.isVisible({ timeout: 5000 }).catch(() => false)) {
await skipBtn.click();
await page.waitForURL(/\/dashboard/, { timeout: 30000 });
} else {
await page.goto("/dashboard");
await page.waitForURL(/\/dashboard/, { timeout: 30000 });
}
}
await page.waitForLoadState("networkidle");
await use(page);
},
});
export { expect } from "@playwright/test";