"""
E2E登录功能测试
使用Playwright测试登录流程
"""

from playwright.sync_api import sync_playwright
import time

def test_login():
    """测试登录功能"""
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = browser.new_context()
        page = context.new_page()
        
        try:
            print("1. 访问登录页面...")
            page.goto('http://localhost:3002')
            page.wait_for_load_state('networkidle')
            time.sleep(2)
            
            print("2. 检查页面标题...")
            title = page.title()
            print(f"   页面标题: {title}")
            
            print("3. 截图保存当前页面...")
            page.screenshot(path='/tmp/login_page.png', full_page=True)
            print("   截图已保存到 /tmp/login_page.png")
            
            print("4. 查找登录表单...")
            username_input = page.locator('input[type="text"], input[placeholder*="用户名"], input[placeholder*="账号"]').first
            password_input = page.locator('input[type="password"]').first
            login_button = page.locator('button:has-text("登录"), button:has-text("Login")').first
            
            if username_input.count() == 0:
                print("   未找到用户名输入框，尝试其他选择器...")
                username_input = page.locator('input').nth(0)
            
            if password_input.count() == 0:
                print("   未找到密码输入框，尝试其他选择器...")
                password_input = page.locator('input').nth(1)
            
            print("5. 填写登录信息...")
            username_input.fill('admin')
            print("   用户名: admin")
            
            password_input.fill('admin123')
            print("   密码: admin123")
            
            print("6. 点击登录按钮...")
            login_button.click()
            
            print("7. 等待登录响应...")
            time.sleep(3)
            page.wait_for_load_state('networkidle')
            
            print("8. 检查登录结果...")
            current_url = page.url
            print(f"   当前URL: {current_url}")
            
            page.screenshot(path='/tmp/login_result.png', full_page=True)
            print("   登录结果截图已保存到 /tmp/login_result.png")
            
            if 'dashboard' in current_url.lower() or 'home' in current_url.lower():
                print("✅ 登录成功！已跳转到主页")
                return True
            elif 'login' not in current_url.lower():
                print("✅ 登录成功！已跳转离开登录页")
                return True
            else:
                print("❌ 登录可能失败，仍在登录页")
                return False
                
        except Exception as e:
            print(f"❌ 测试过程中出现错误: {str(e)}")
            page.screenshot(path='/tmp/login_error.png', full_page=True)
            print("   错误截图已保存到 /tmp/login_error.png")
            return False
        finally:
            browser.close()

if __name__ == "__main__":
    print("=" * 60)
    print("E2E登录功能测试")
    print("=" * 60)
    
    success = test_login()
    
    print("=" * 60)
    if success:
        print("测试结果: ✅ 通过")
    else:
        print("测试结果: ❌ 失败")
    print("=" * 60)
