/** * 全局认证 Setup * 通过 UI 登录后持久化 storageState,后续所有测试复用登录态 */ import { test as setup, expect } from '@playwright/test'; const authFile = 'e2e/.auth/user.json'; setup('authenticate', async ({ page }) => { // 1. 访问登录页 await page.goto('/login'); await expect(page.locator('.login-title')).toContainText('天津硕博霖 CRM 系统'); // 2. 填写表单 await page.getByPlaceholder('用户名').fill('admin'); await page.getByPlaceholder('密码').fill('123456'); // 3. 点击登录 await page.getByRole('button', { name: '登 录' }).click(); // 4. 等待跳转到首页 (工作台) await page.waitForURL('/', { timeout: 10000 }); await expect(page.locator('.el-menu--vertical').first()).toBeVisible({ timeout: 5000 }); // 5. 持久化认证状态 (localStorage token + cookies) await page.context().storageState({ path: authFile }); });