import { test, expect } from "@playwright/test"; import { E2E_NEW_USER, FIXED_CAPTCHA } from "../fixtures/test-users"; // 生成短用户名(不超过20字符):e2e_ + 基于时间戳的短hash function shortUsername(prefix: string): string { const suffix = Date.now().toString(36).slice(-6); // 6位base36 return `${prefix}_${suffix}`; // e2e_reg_xxxxxx = 14字符 } test.describe("注册流程", () => { test("UI 注册成功后跳转登录页", async ({ page }) => { const username = shortUsername("e2e_reg"); const password = E2E_NEW_USER.password; await page.goto("/auth/register"); await page.fill("#register-username", username); await page.fill("#register-password", password); await page.fill("#register-confirm", password); await page.fill("#register-captcha", FIXED_CAPTCHA); await page.click('button[type="submit"]'); // 等待 toast 出现 await expect(page.locator(".Toastify__toast").first()).toBeVisible({ timeout: 10000 }); await expect(page.locator(".Toastify__toast").first()).toContainText("注册成功"); // 等待跳转到登录页(有 1.5s setTimeout) await page.waitForURL(/\/auth\/login/, { timeout: 10000 }); }); test("两次密码不一致前端拦截", async ({ page }) => { await page.goto("/auth/register"); await page.fill("#register-username", "e2e_mismatch"); await page.fill("#register-password", "password_one"); await page.fill("#register-confirm", "password_two"); await page.fill("#register-captcha", FIXED_CAPTCHA); await page.click('button[type="submit"]'); // 应显示错误 toast await expect(page.locator(".Toastify__toast").first()).toBeVisible({ timeout: 10000 }); await expect(page.locator(".Toastify__toast").first()).toContainText("不一致"); // 应停留在注册页 await expect(page).toHaveURL(/\/auth\/register/); }); test("重复用户名注册失败", async ({ page }) => { // 先注册一个用户 const username = shortUsername("e2e_dup"); const password = E2E_NEW_USER.password; await page.goto("/auth/register"); await page.fill("#register-username", username); await page.fill("#register-password", password); await page.fill("#register-confirm", password); await page.fill("#register-captcha", FIXED_CAPTCHA); await page.click('button[type="submit"]'); await page.waitForURL(/\/auth\/login/, { timeout: 10000 }); // 再用同一用户名注册 await page.goto("/auth/register"); await page.fill("#register-username", username); await page.fill("#register-password", password); await page.fill("#register-confirm", password); await page.fill("#register-captcha", FIXED_CAPTCHA); await page.click('button[type="submit"]'); // 应显示错误 toast await expect(page.locator(".Toastify__toast").first()).toBeVisible({ timeout: 10000 }); // 应停留在注册页 await expect(page).toHaveURL(/\/auth\/register/); }); test("注册后可用新账号登录", async ({ page }) => { const username = shortUsername("e2e_lr"); const password = E2E_NEW_USER.password; // 注册 await page.goto("/auth/register"); await page.fill("#register-username", username); await page.fill("#register-password", password); await page.fill("#register-confirm", password); await page.fill("#register-captcha", FIXED_CAPTCHA); await page.click('button[type="submit"]'); await page.waitForURL(/\/auth\/login/, { timeout: 10000 }); // 登录 await page.fill("#login-username", username); await page.fill("#login-password", password); await page.fill("#login-captcha", FIXED_CAPTCHA); await page.click('button[type="submit"]'); await page.waitForURL("/", { timeout: 15000 }); await expect(page).toHaveURL("/"); }); });