08ea5fbe98
添加用户管理视图、API和状态管理文件
117 lines
3.4 KiB
TypeScript
117 lines
3.4 KiB
TypeScript
import { test, expect } from '@playwright/test';
|
|
|
|
/**
|
|
* API登录功能调试测试
|
|
* 直接测试后端API登录接口
|
|
*/
|
|
|
|
test.describe('API登录功能调试', () => {
|
|
test('应该直接调用后端登录API', async ({ request }) => {
|
|
console.log('🧪 测试后端登录API...');
|
|
|
|
// 直接调用后端登录API
|
|
const response = await request.post('http://127.0.0.1:8080/api/sys/auth/login', {
|
|
data: {
|
|
username: 'admin',
|
|
password: 'admin123456'
|
|
},
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
|
|
console.log('📥 响应状态:', response.status());
|
|
console.log('📥 响应头:', JSON.stringify(response.headers(), null, 2));
|
|
|
|
const responseBody = await response.text();
|
|
console.log('📥 响应体:', responseBody);
|
|
|
|
// 验证响应
|
|
expect(response.status()).toBe(200);
|
|
|
|
// 解析响应
|
|
let data;
|
|
try {
|
|
data = JSON.parse(responseBody);
|
|
console.log('📦 解析后的数据:', JSON.stringify(data, null, 2));
|
|
} catch (e) {
|
|
console.log('⚠️ 响应不是JSON格式');
|
|
}
|
|
|
|
// 检查是否包含token
|
|
if (data && (data.token || (data.data && data.data.token))) {
|
|
console.log('✅ 登录成功,获取到token');
|
|
} else {
|
|
console.log('❌ 登录失败,未获取到token');
|
|
}
|
|
});
|
|
|
|
test('应该通过前端代理调用登录API', async ({ page }) => {
|
|
console.log('🧪 测试前端代理登录...');
|
|
|
|
// 监听网络请求
|
|
let loginRequest: any = null;
|
|
let loginResponse: any = null;
|
|
|
|
page.on('request', request => {
|
|
if (request.url().includes('login')) {
|
|
loginRequest = {
|
|
url: request.url(),
|
|
method: request.method(),
|
|
headers: request.headers(),
|
|
postData: request.postData()
|
|
};
|
|
console.log('📤 登录请求:', JSON.stringify(loginRequest, null, 2));
|
|
}
|
|
});
|
|
|
|
page.on('response', async response => {
|
|
if (response.url().includes('login')) {
|
|
try {
|
|
const body = await response.text();
|
|
loginResponse = {
|
|
url: response.url(),
|
|
status: response.status(),
|
|
body: body
|
|
};
|
|
console.log('📥 登录响应:', JSON.stringify(loginResponse, null, 2));
|
|
} catch (e) {
|
|
console.log('⚠️ 无法读取响应体');
|
|
}
|
|
}
|
|
});
|
|
|
|
// 访问登录页面
|
|
await page.goto('http://localhost:5174/login');
|
|
await page.waitForLoadState('networkidle');
|
|
|
|
// 填写登录表单
|
|
await page.fill('input[placeholder="请输入用户名"]', 'admin');
|
|
await page.fill('input[placeholder="请输入密码"]', 'admin123456');
|
|
|
|
// 点击登录按钮
|
|
await page.click('button:has-text("登录")');
|
|
|
|
// 等待响应
|
|
await page.waitForTimeout(3000);
|
|
|
|
// 验证结果
|
|
expect(loginRequest).not.toBeNull();
|
|
expect(loginResponse).not.toBeNull();
|
|
|
|
// 检查响应是否包含token
|
|
if (loginResponse && loginResponse.body) {
|
|
try {
|
|
const data = JSON.parse(loginResponse.body);
|
|
if (data.token || (data.data && data.data.token)) {
|
|
console.log('✅ 通过前端代理登录成功');
|
|
} else {
|
|
console.log('❌ 通过前端代理登录失败,响应:', loginResponse.body);
|
|
}
|
|
} catch (e) {
|
|
console.log('⚠️ 响应解析失败:', loginResponse.body);
|
|
}
|
|
}
|
|
});
|
|
});
|