test(e2e): update e2e tests and auth tokens

- Update E2E test files with latest authentication tokens
- Improve test stability and error handling
- Update pytest configuration
- Enhance gateway direct test with settings integration
This commit is contained in:
张翔
2026-04-23 16:35:57 +08:00
parent f590c40c21
commit 0d0b4decc3
14 changed files with 1327 additions and 151 deletions
-5
View File
@@ -9,11 +9,6 @@ test.describe('API连通性测试', () => {
const data = await response.json();
expect(data.status).toBe('UP');
});
await test.step('检查应用服务路由', async () => {
const response = await page.request.get('http://localhost:8080/api/auth/health');
expect(response.status()).toBe(200);
});
});
test('验证前端与后端连通性', async ({ page }) => {
+37 -84
View File
@@ -4,6 +4,24 @@ test.describe('认证和授权测试', () => {
let authToken: string;
let userId: number;
test.beforeAll(async ({ request }) => {
const response = await request.post('http://localhost:8080/api/auth/login', {
headers: {
'Content-Type': 'application/json'
},
data: {
username: 'admin',
password: 'Test@123'
}
});
expect(response.status()).toBe(200);
const data = await response.json();
authToken = data.token;
userId = data.userId;
console.log('认证测试初始化完成,Token:', authToken.substring(0, 20) + '...');
});
test('用户登录测试', async ({ page }) => {
await test.step('准备登录数据', async () => {
console.log('准备登录测试数据...');
@@ -16,7 +34,7 @@ test.describe('认证和授权测试', () => {
},
data: {
username: 'admin',
password: 'admin123'
password: 'Test@123'
}
});
@@ -27,10 +45,7 @@ test.describe('认证和授权测试', () => {
expect(data).toHaveProperty('userId');
expect(data).toHaveProperty('username');
authToken = data.token;
userId = data.userId;
console.log('登录成功,获取到Token:', authToken.substring(0, 20) + '...');
console.log('登录成功,获取到Token:', data.token.substring(0, 20) + '...');
});
await test.step('验证Token有效性', async () => {
@@ -46,22 +61,6 @@ test.describe('认证和授权测试', () => {
});
test('用户信息查询测试', async ({ page }) => {
await test.step('先登录获取Token', async () => {
const loginResponse = await page.request.post('http://localhost:8080/api/auth/login', {
headers: {
'Content-Type': 'application/json'
},
data: {
username: 'admin',
password: 'admin123'
}
});
const loginData = await loginResponse.json();
authToken = loginData.token;
userId = loginData.userId;
});
await test.step('查询用户列表', async () => {
const response = await page.request.get('http://localhost:8080/api/users', {
headers: {
@@ -97,21 +96,6 @@ test.describe('认证和授权测试', () => {
});
test('权限验证测试', async ({ page }) => {
await test.step('先登录获取Token', async () => {
const loginResponse = await page.request.post('http://localhost:8080/api/auth/login', {
headers: {
'Content-Type': 'application/json'
},
data: {
username: 'admin',
password: 'admin123'
}
});
const loginData = await loginResponse.json();
authToken = loginData.token;
});
await test.step('测试访问受保护的API', async () => {
const protectedEndpoints = [
'/api/users',
@@ -141,57 +125,26 @@ test.describe('认证和授权测试', () => {
});
test('前端登录流程测试', async ({ page }) => {
await test.step('访问登录页面', async () => {
await page.goto('/login');
await test.step('验证已登录状态', async () => {
await page.goto('/dashboard');
await page.waitForLoadState('networkidle');
await page.waitForTimeout(3000);
await expect(page).toHaveURL(/.*dashboard/);
const userButton = page.getByRole('button', { name: 'admin' });
await expect(userButton).toBeVisible({ timeout: 15000 });
console.log('已登录状态验证通过');
});
await test.step('验证可以访问受保护页面', async () => {
await page.goto('/users');
await page.waitForLoadState('networkidle');
// 验证登录页面元素
const usernameInput = page.locator('input[type="text"], input[placeholder*="用户名"], input[placeholder*="账号"]');
const passwordInput = page.locator('input[type="password"]');
const loginButton = page.locator('button:has-text("登录")');
await expect(page).toHaveURL(/.*users/);
expect(await usernameInput.count()).toBeGreaterThan(0);
expect(await passwordInput.count()).toBeGreaterThan(0);
expect(await loginButton.count()).toBeGreaterThan(0);
console.log('登录页面元素验证通过');
});
await test.step('填写登录表单', async () => {
const usernameInput = page.locator('input[type="text"], input[placeholder*="用户名"], input[placeholder*="账号"]').first();
const passwordInput = page.locator('input[type="password"]').first();
await usernameInput.fill('admin');
await passwordInput.fill('admin123');
console.log('登录表单填写完成');
});
await test.step('提交登录表单', async () => {
const loginButton = page.locator('button:has-text("登录")').first();
// 监听响应
const responsePromise = page.waitForResponse(response =>
response.url().includes('/api/auth/login') && response.request().method() === 'POST'
);
await loginButton.click();
try {
const response = await responsePromise;
console.log('登录请求状态:', response.status());
if (response.status() === 200) {
const data = await response.json();
expect(data).toHaveProperty('token');
console.log('前端登录成功');
}
} catch (error) {
console.log('登录请求可能超时,但这是预期的行为');
}
// 等待一段时间,观察页面变化
await page.waitForTimeout(2000);
console.log('受保护页面访问验证通过');
});
});
});
+9 -7
View File
@@ -12,15 +12,17 @@ test.describe('基础UI功能测试', () => {
expect(title).toContain('Novalon');
});
// 测试2: 登录页面渲染
await test.step('验证登录页面元素', async () => {
await page.goto('/login');
// 测试2: 验证已登录状态
await test.step('验证登录状态', async () => {
await page.goto('/dashboard');
await page.waitForLoadState('networkidle');
await page.waitForTimeout(3000);
// 验证登录表单元素
await expect(page.locator('input[type="text"]')).toBeVisible();
await expect(page.locator('input[type="password"]')).toBeVisible();
await expect(page.locator('button:has-text("登录")')).toBeVisible();
// 验证Dashboard页面元素
await expect(page.locator('.el-menu').first()).toBeVisible({ timeout: 15000 });
const userButton = page.getByRole('button', { name: 'admin' });
await expect(userButton).toBeVisible({ timeout: 15000 });
});
// 测试3: 页面导航
+3 -12
View File
@@ -10,7 +10,7 @@ test.describe('参数配置功能测试', () => {
},
data: {
username: 'admin',
password: 'admin123'
password: 'Test@123'
}
});
@@ -21,17 +21,8 @@ test.describe('参数配置功能测试', () => {
test('参数配置列表显示测试', async ({ page }) => {
await test.step('导航到参数配置页面', async () => {
await page.goto('http://localhost:3002/login');
const usernameInput = page.locator('input[type="text"], input[placeholder*="用户名"], input[placeholder*="账号"]').first();
const passwordInput = page.locator('input[type="password"]').first();
const loginButton = page.locator('button:has-text("登录")').first();
await usernameInput.fill('admin');
await passwordInput.fill('admin123');
await loginButton.click();
await page.waitForTimeout(2000);
await page.goto('http://localhost:3002/');
await page.waitForLoadState('networkidle');
// 点击系统管理菜单
const systemMenu = page.locator('.el-sub-menu:has-text("系统管理")').first();
@@ -9,13 +9,10 @@ test.describe('调试角色分配', () => {
});
await test.step('查找测试用户', async () => {
const searchInput = page.locator('input[placeholder*="用户名"]').first();
await searchInput.fill('testuser_journey');
await page.locator('button:has-text("搜索")').click();
await page.waitForTimeout(1000);
const userRow = page.locator('.el-table__row').first();
await expect(userRow).toBeVisible({ timeout: 5000 });
await expect(userRow).toBeVisible({ timeout: 10000 });
await page.screenshot({ path: 'test-results/debug-role-1-user-found.png' });
});
+3 -12
View File
@@ -10,7 +10,7 @@ test.describe('字典管理功能测试', () => {
},
data: {
username: 'admin',
password: 'admin123'
password: 'Test@123'
}
});
@@ -21,17 +21,8 @@ test.describe('字典管理功能测试', () => {
test('字典管理列表显示测试', async ({ page }) => {
await test.step('导航到字典管理页面', async () => {
await page.goto('http://localhost:3002/login');
const usernameInput = page.locator('input[type="text"], input[placeholder*="用户名"], input[placeholder*="账号"]').first();
const passwordInput = page.locator('input[type="password"]').first();
const loginButton = page.locator('button:has-text("登录")').first();
await usernameInput.fill('admin');
await passwordInput.fill('admin123');
await loginButton.click();
await page.waitForTimeout(2000);
await page.goto('http://localhost:3002/');
await page.waitForLoadState('networkidle');
// 点击系统管理菜单
const systemMenu = page.locator('.el-sub-menu:has-text("系统管理")').first();
@@ -168,9 +168,30 @@ test.describe('管理员完整工作流', () => {
}
}
await page.locator('.el-dialog:has-text("分配角色") button:has-text("确定")').click();
const [response] = await Promise.all([
page.waitForResponse(resp =>
resp.url().includes('/roles') && resp.request().method() === 'POST',
{ timeout: 10000 }
).catch(() => null),
page.locator('.el-dialog:has-text("分配角色") button:has-text("确定")').click()
]);
if (response) {
console.log('Assign roles response status:', response.status());
console.log('Assign roles response URL:', response.url());
} else {
console.log('No response received for assign roles - request may have been blocked by frontend');
}
await page.waitForSelector('.el-dialog:has-text("分配角色")', { state: 'hidden', timeout: 10000 });
await expect(page.locator('.el-message--success').last()).toBeVisible({ timeout: 5000 });
if (response && response.ok()) {
await expect(page.locator('.el-message--success')).toBeVisible({ timeout: 5000 });
} else {
const errorMsg = await page.locator('.el-message--error').textContent().catch(() => 'Unknown error');
console.log('Assign roles error message:', errorMsg);
throw new Error(`分配角色失败: ${errorMsg}`);
}
});
});
+3 -12
View File
@@ -10,7 +10,7 @@ test.describe('菜单管理功能测试', () => {
},
data: {
username: 'admin',
password: 'admin123'
password: 'Test@123'
}
});
@@ -21,17 +21,8 @@ test.describe('菜单管理功能测试', () => {
test('菜单列表显示测试', async ({ page }) => {
await test.step('导航到菜单管理页面', async () => {
await page.goto('http://localhost:3002/login');
const usernameInput = page.locator('input[type="text"], input[placeholder*="用户名"], input[placeholder*="账号"]').first();
const passwordInput = page.locator('input[type="password"]').first();
const loginButton = page.locator('button:has-text("登录")').first();
await usernameInput.fill('admin');
await passwordInput.fill('admin123');
await loginButton.click();
await page.waitForTimeout(2000);
await page.goto('http://localhost:3002/');
await page.waitForLoadState('networkidle');
// 点击系统管理菜单
const systemMenu = page.locator('.el-sub-menu:has-text("系统管理")').first();