""" E2E登录功能完整验证 验证登录成功后的所有状态 """ from playwright.sync_api import sync_playwright import time def test_login_complete(): """完整测试登录功能""" with sync_playwright() as p: browser = p.chromium.launch(headless=True) context = browser.new_context() page = context.new_page() try: print("=" * 60) print("E2E登录功能完整验证") print("=" * 60) print("\n1. 访问登录页面...") page.goto('http://localhost:3002/login') page.wait_for_load_state('networkidle') time.sleep(1) print("\n2. 填写登录表单...") page.fill('input[type="text"], input[placeholder*="用户名"]', 'admin') page.fill('input[type="password"]', 'admin123') print(" 用户名: admin") print(" 密码: admin123") print("\n3. 点击登录按钮...") with page.expect_navigation(timeout=10000): page.click('button:has-text("登录")') print("\n4. 等待页面加载...") time.sleep(3) page.wait_for_load_state('networkidle') print("\n5. 检查登录状态...") current_url = page.url print(f" 当前URL: {current_url}") token = page.evaluate('() => localStorage.getItem("token")') userId = page.evaluate('() => localStorage.getItem("userId")') username = page.evaluate('() => localStorage.getItem("username")') print(f" Token: {token[:50] if token else 'None'}...") print(f" UserId: {userId}") print(f" Username: {username}") print("\n6. 检查页面内容...") page.screenshot(path='/tmp/login_complete.png', full_page=True) print(" 截图已保存到 /tmp/login_complete.png") page_title = page.title() print(f" 页面标题: {page_title}") has_dashboard = page.locator('text=Dashboard, text=仪表盘, text=首页').count() > 0 print(f" 包含Dashboard内容: {has_dashboard}") print("\n" + "=" * 60) print("验证结果:") print("=" * 60) success = True if token and userId and username: print("✅ localStorage数据正确") else: print("❌ localStorage数据缺失") success = False if '/login' not in current_url: print("✅ 已跳转离开登录页") else: print("⚠️ 仍在登录页(可能是路由问题)") if has_dashboard: print("✅ Dashboard内容已加载") else: print("⚠️ Dashboard内容未找到") print("=" * 60) if success: print("\n🎉 登录功能测试通过!") else: print("\n❌ 登录功能测试失败") return success except Exception as e: print(f"\n❌ 测试错误: {str(e)}") import traceback traceback.print_exc() page.screenshot(path='/tmp/login_error_complete.png', full_page=True) return False finally: browser.close() if __name__ == "__main__": test_login_complete()