dingdanquanliucheng/e2e/fixtures/auth.js
2026-06-14 16:20:04 +08:00

164 lines
4.3 KiB
JavaScript
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.

/**
* 认证相关 Fixtures
*
* 提供登录、登出、获取认证状态等辅助函数。
*/
const { test: base, expect } = require('@playwright/test');
// 测试账号配置
const TEST_ACCOUNTS = {
admin: {
username: 'admin01',
password: 'admin123',
roleType: 'admin',
realName: '管理员',
},
salesman: {
username: 'sales01',
password: 'sales123',
roleType: 'salesman',
realName: '业务员A',
},
manager: {
username: 'manager01',
password: 'manager123',
roleType: 'manager',
realName: '管理层',
},
driver: {
username: 'driver01',
password: 'driver123',
roleType: 'driver',
realName: '司机A',
},
};
/**
* 执行登录操作
* @param {import('@playwright/test').Page} page - 页面对象
* @param {string} role - 角色类型admin/salesman/manager/driver
*/
async function login(page, role = 'admin') {
const account = TEST_ACCOUNTS[role];
if (!account) {
throw new Error(`未知的角色类型: ${role}`);
}
// 导航到登录页
await page.goto('/login');
// 填写登录表单
await page.fill('input[placeholder*="用户名"], input[name="username"], #username', account.username);
await page.fill('input[placeholder*="密码"], input[name="password"], #password', account.password);
// 选择角色(如果有角色选择器)
const roleSelector = page.locator('select[name="roleType"], .role-select, [data-testid="role-select"]');
if (await roleSelector.isVisible({ timeout: 2000 }).catch(() => false)) {
await roleSelector.selectOption(account.roleType);
}
// 点击登录按钮
await page.click('button[type="submit"], button:has-text("登录"), .login-btn');
// 等待登录成功(跳转到首页或 Dashboard
await page.waitForURL(/\/(dashboard|orders|home)/, { timeout: 10000 });
// 验证登录成功
await expect(page).not.toHaveURL(/\/login/);
}
/**
* 执行登出操作
* @param {import('@playwright/test').Page} page - 页面对象
*/
async function logout(page) {
// 点击用户头像或用户名
const userMenu = page.locator('.user-info, .avatar, [data-testid="user-menu"]');
if (await userMenu.isVisible({ timeout: 3000 }).catch(() => false)) {
await userMenu.click();
// 点击退出按钮
const logoutBtn = page.locator('text=退出, text=登出, text=Logout, [data-testid="logout-btn"]');
await logoutBtn.click();
// 等待跳转到登录页
await page.waitForURL(/\/login/, { timeout: 5000 });
}
}
/**
* 创建已认证状态的 Storage State
* @param {import('@playwright/test').Browser} browser - 浏览器对象
* @param {string} role - 角色类型
* @returns {Promise<Object>} Storage State 对象
*/
async function createAuthState(browser, role = 'admin') {
const context = await browser.newContext();
const page = await context.newPage();
await login(page, role);
// 保存 storage state
const storageState = await context.storageState();
await context.close();
return storageState;
}
/**
* 带认证的测试扩展
* 自动处理登录状态
*/
const test = base.extend({
// 管理员认证页面
adminPage: async ({ browser }, use) => {
const context = await browser.newContext({
storageState: await createAuthState(browser, 'admin'),
});
const page = await context.newPage();
await use(page);
await context.close();
},
// 业务员认证页面
salesmanPage: async ({ browser }, use) => {
const context = await browser.newContext({
storageState: await createAuthState(browser, 'salesman'),
});
const page = await context.newPage();
await use(page);
await context.close();
},
// 管理层认证页面
managerPage: async ({ browser }, use) => {
const context = await browser.newContext({
storageState: await createAuthState(browser, 'manager'),
});
const page = await context.newPage();
await use(page);
await context.close();
},
// 司机认证页面
driverPage: async ({ browser }, use) => {
const context = await browser.newContext({
storageState: await createAuthState(browser, 'driver'),
});
const page = await context.newPage();
await use(page);
await context.close();
},
});
module.exports = {
test,
expect,
login,
logout,
createAuthState,
TEST_ACCOUNTS,
};