#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 快速登录与基础页面验证探针 """ import os import sys import time import subprocess from playwright.sync_api import sync_playwright def load_qa_env(): env_file = os.path.expanduser('~/.railwaysystem-qa.env') env = {} if os.path.exists(env_file): with open(env_file, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if line and not line.startswith('#') and '=' in line: k, v = line.split('=', 1) env[k.strip()] = v.strip() return env def get_captcha_code_from_redis(uuid): res = subprocess.check_output(['redis-cli', 'get', f'captcha_codes:{uuid}']).decode('utf-8').strip() return res.strip('\"') def main(): env = load_qa_env() username = env.get('QA_ADMIN_USERNAME', 'admin') password = env.get('QA_ADMIN_PASSWORD', 'admin123') base_url = 'http://127.0.0.1:1024' with sync_playwright() as p: browser = p.chromium.launch(channel='chrome', headless=True) context = browser.new_context(viewport={'width': 1440, 'height': 900}) page = context.new_page() last_captcha_uuid = [None] def on_response(response): if 'captchaImage' in response.url: try: data = response.json() last_captcha_uuid[0] = data.get('uuid') except Exception: pass elif '/login' in response.url and response.request.method == 'POST': try: print(f"Login API response: status={response.status}, body={response.text()}", flush=True) except Exception as e: print(f"Login API response read err: {e}", flush=True) page.on('response', on_response) page.on('console', lambda msg: print(f"[CONSOLE {msg.type}] {msg.text}", flush=True)) print(f"Navigating to {base_url}/login...", flush=True) page.goto(f"{base_url}/login", wait_until='domcontentloaded') # Wait for captcha uuid for i in range(50): if last_captcha_uuid[0]: break page.wait_for_timeout(200) uuid = last_captcha_uuid[0] print(f"Got captcha uuid: {uuid}", flush=True) code = get_captcha_code_from_redis(uuid) print(f"Got captcha code from redis: {code}", flush=True) page.wait_for_selector('input[type="password"]', timeout=10000) page.fill('input[placeholder="账号"]', username) page.fill('input[placeholder="密码"]', password) page.fill('input[placeholder="验证码"]', code) print("Submitting login form...", flush=True) page.click('button:has-text("登 录")') page.wait_for_timeout(3000) print(f"Current page URL: {page.url}", flush=True) # Check if dashboard loaded time.sleep(2) title = page.title() print(f"Dashboard page title: {title}") browser.close() print("Login probe SUCCESS!") if __name__ == '__main__': main()