"""端到端冒烟测试:登录门控 + 主页榜单 + 标签页 + 个人数据保存。 用法(服务器已在 127.0.0.1:8000 运行时): uv run python tests/smoke_playwright.py 截图输出到 tests/_shots/ """ from __future__ import annotations import os import sys from pathlib import Path from playwright.sync_api import sync_playwright # Windows GBK 控制台无法直接打印 emoji,统一 UTF-8 输出,避免 UnicodeEncodeError try: sys.stdout.reconfigure(encoding="utf-8", errors="replace") except Exception: pass URL = sys.argv[1] if len(sys.argv) > 1 else "http://127.0.0.1:8000" # 密码单一来源:与 config.SDE_DEFAULT_PASSWORD 保持一致,避免散落字面量 PW = os.environ.get("SDE_DEFAULT_PASSWORD", "sde2024") SHOTS = Path(__file__).parent / "_shots" SHOTS.mkdir(exist_ok=True) failures: list[str] = [] def check(cond: bool, msg: str) -> None: mark = "PASS" if cond else "FAIL" print(f" [{mark}] {msg}") if not cond: failures.append(msg) def main() -> int: with sync_playwright() as p: try: browser = p.chromium.launch(headless=True, channel="msedge") except Exception: browser = p.chromium.launch(headless=True) page = browser.new_page(viewport={"width": 1280, "height": 980}) page.goto(URL) page.wait_for_load_state("networkidle") # 1) 登录页 page.wait_for_selector("#btn_login", timeout=15000) page.screenshot(path=str(SHOTS / "01-login.png"), full_page=True) check(page.locator("#in_user").count() == 1, "登录页显示用户名输入框") check("肆带贰" in page.content(), "登录页显示队名 肆带贰") # 2) 管理员登录 -> 主页 page.fill("#in_user", "admin") page.fill("#in_pw", PW) page.click("#btn_login") page.wait_for_selector("text=球队战绩", timeout=15000) page.wait_for_timeout(600) page.screenshot(path=str(SHOTS / "02-home.png"), full_page=True) content = page.content() check("射手榜" in content and "助攻榜" in content and "出勤榜" in content, "主页显示三个榜单") check("Leo" in content, "榜单包含真实球员 Leo") check("积分" in content, "战绩卡显示积分") check(page.locator("a.nav-link:has-text('管理')").count() == 1, "管理员可见『管理』标签") check("MVP 榜" in content, "主页显示 MVP 榜") # 3) 我的数据标签 page.click("a.nav-link:has-text('我的数据')") page.wait_for_selector("text=逐场登记", timeout=8000) page.wait_for_timeout(400) page.screenshot(path=str(SHOTS / "03-mydata.png"), full_page=True) check(page.locator("#btn_change_pw").count() == 1, "我的数据页有『修改密码』表单") check(page.locator("#btn_save_mvp").count() == 1, "我的数据页有 MVP 投票表单") # 4) 管理标签 page.click("a.nav-link:has-text('管理')") page.wait_for_selector("text=录入新比赛比分", timeout=8000) page.wait_for_timeout(400) page.screenshot(path=str(SHOTS / "04-admin.png"), full_page=True) check("队员名单" in page.content(), "管理页显示队员名单") # 5) 退出 -> 球员登录(应无管理标签) page.click("#btn_logout") page.wait_for_selector("#btn_login", timeout=8000) page.fill("#in_user", "leo") page.fill("#in_pw", PW) page.click("#btn_login") page.wait_for_selector("text=球队战绩", timeout=15000) page.wait_for_timeout(500) check(page.locator("a.nav-link:has-text('管理')").count() == 0, "普通球员看不到『管理』标签") # 6) 球员保存个人数据 -> 通知 page.click("a.nav-link:has-text('我的数据')") page.wait_for_selector("#btn_save_stats", timeout=8000) page.wait_for_timeout(400) page.screenshot(path=str(SHOTS / "05-player-mydata.png"), full_page=True) page.click("#btn_save_stats") page.wait_for_selector(".shiny-notification", timeout=8000) note = page.locator(".shiny-notification").inner_text() check("已保存" in note, f"保存后出现成功通知:{note!r}") browser.close() print("\n==== 结果 ====") if failures: print(f"{len(failures)} 项失败:") for f in failures: print(" -", f) return 1 print("全部通过 ✅") return 0 if __name__ == "__main__": raise SystemExit(main())