import { chromium } from 'playwright'; async function finalAuthTest() { const browser = await chromium.launch({ headless: false, slowMo: 500 }); const context = await browser.newContext(); const page = await context.newPage(); const apiCalls = []; // 监听API调用 page.on('request', (request) => { if (request.url().includes('/api/')) { apiCalls.push({ method: request.method(), url: request.url(), timestamp: new Date().toISOString(), }); console.log(`🔵 API请求: ${request.method()} ${request.url()}`); } }); page.on('response', async (response) => { if (response.url().includes('/api/')) { const status = response.status(); console.log(`🟢 API响应: ${status} ${response.url()}`); if (status === 401) { console.log('❌ 401认证错误!'); } else if (status === 200) { console.log('✅ API调用成功'); } } }); try { console.log('🚀 开始最终认证测试...'); // 1. 访问登录页面 await page.goto('http://localhost:5173/auth/login'); await page.waitForTimeout(2000); // 2. 填写并提交登录表单 await page.fill('input[name="username"]', 'admin'); await page.fill('input[name="password"]', '111111'); // 点击登录按钮 await page.click('button:has-text("登录")'); console.log('✅ 登录表单已提交'); // 等待登录处理 await page.waitForTimeout(3000); // 3. 检查是否跳转到dashboard const currentUrl = page.url(); console.log(`📍 当前URL: ${currentUrl}`); if (currentUrl.includes('/dashboard')) { console.log('✅ 成功跳转到dashboard'); // 等待dashboard数据加载 await page.waitForTimeout(3000); // 检查是否有数据显示 const pageText = await page.textContent('body'); const hasData = pageText.includes('数据') || pageText.includes('统计') || pageText.includes('总数'); if (hasData) { console.log('✅ Dashboard显示数据'); } else { console.log('⚠️ Dashboard可能没有显示数据'); } } else { console.log('❌ 登录失败,未跳转到dashboard'); } // 4. 检查token存储 const token = await page.evaluate( () => localStorage.getItem('accessToken') || localStorage.getItem('token'), ); if (token) { console.log('✅ Token已存储'); } else { console.log('❌ 未找到token'); } // 5. 总结API调用 console.log(`\n📊 总共发起了 ${apiCalls.length} 个API调用:`); apiCalls.forEach((call, index) => { console.log(`${index + 1}. ${call.method} ${call.url}`); }); if (apiCalls.some((call) => call.url.includes('/api/auth/login'))) { console.log('✅ 发现登录API调用'); } else { console.log('❌ 未发现登录API调用'); } await page.waitForTimeout(10000); } catch (error) { console.error('❌ 测试失败:', error); } await browser.close(); } finalAuthTest().catch(console.error);