b34c09bdaf
问题: - 测试用例之间没有共享登录状态 - 每个测试都创建新的浏览器上下文 - 导致后续测试无法访问已登录的页面 修复: - 添加auth.setup.ts文件保存登录状态 - 在playwright.config.ts中配置setup项目 - 配置storageState恢复登录状态 - 移除重复的登录测试 - 添加页面加载等待 优势: - 测试之间共享登录状态 - 减少重复登录操作 - 提高测试执行效率 - 更符合实际使用场景
17 lines
550 B
TypeScript
17 lines
550 B
TypeScript
import { test as setup } from '@playwright/test';
|
|
|
|
const authFile = 'playwright/.auth/user.json';
|
|
|
|
setup('authenticate', async ({ page }) => {
|
|
await page.goto('/login');
|
|
await page.waitForLoadState('networkidle');
|
|
|
|
await page.locator('input[placeholder*="用户名"]').fill('admin');
|
|
await page.locator('input[placeholder*="密码"]').fill('admin123');
|
|
await page.locator('button:has-text("登录")').click();
|
|
|
|
await page.waitForURL('**/dashboard', { timeout: 30000 });
|
|
|
|
await page.context().storageState({ path: authFile });
|
|
});
|