fix(e2e): 修复测试登录状态问题
问题: - 测试用例之间没有共享登录状态 - 每个测试都创建新的浏览器上下文 - 导致后续测试无法访问已登录的页面 修复: - 添加auth.setup.ts文件保存登录状态 - 在playwright.config.ts中配置setup项目 - 配置storageState恢复登录状态 - 移除重复的登录测试 - 添加页面加载等待 优势: - 测试之间共享登录状态 - 减少重复登录操作 - 提高测试执行效率 - 更符合实际使用场景
This commit is contained in:
@@ -8,43 +8,10 @@ test.describe('管理员完整工作流', () => {
|
||||
const roleKey = `test_role_${timestamp}`;
|
||||
const username = `testuser_${timestamp}`;
|
||||
|
||||
test('管理员登录', async ({ page }) => {
|
||||
await test.step('访问登录页面', async () => {
|
||||
await page.goto('/login');
|
||||
await expect(page).toHaveTitle(/登录/);
|
||||
});
|
||||
|
||||
await test.step('等待页面加载完成', async () => {
|
||||
await page.waitForLoadState('networkidle');
|
||||
await expect(page.locator('input[placeholder*="用户名"]')).toBeVisible({ timeout: 10000 });
|
||||
});
|
||||
|
||||
await test.step('输入管理员凭证', async () => {
|
||||
const usernameInput = page.locator('input[placeholder*="用户名"]');
|
||||
const passwordInput = page.locator('input[placeholder*="密码"]');
|
||||
|
||||
await usernameInput.waitFor({ state: 'visible' });
|
||||
await usernameInput.fill('admin');
|
||||
|
||||
await passwordInput.waitFor({ state: 'visible' });
|
||||
await passwordInput.fill('admin123');
|
||||
});
|
||||
|
||||
await test.step('点击登录按钮', async () => {
|
||||
const loginButton = page.locator('button:has-text("登录")');
|
||||
await loginButton.waitFor({ state: 'visible' });
|
||||
await loginButton.click();
|
||||
});
|
||||
|
||||
await test.step('验证登录成功', async () => {
|
||||
await page.waitForURL('**/dashboard', { timeout: 30000 });
|
||||
await expect(page).toHaveURL(/.*dashboard/);
|
||||
});
|
||||
});
|
||||
|
||||
test('创建角色并分配权限', async ({ page }) => {
|
||||
await test.step('导航到角色管理', async () => {
|
||||
await page.goto('/dashboard');
|
||||
await page.waitForLoadState('networkidle');
|
||||
await page.locator('text=系统管理').click();
|
||||
await page.locator('text=角色管理').click();
|
||||
await expect(page).toHaveURL(/.*roles/);
|
||||
|
||||
Reference in New Issue
Block a user