fix: 适配 hash 路由登录页 (/#/login)

This commit is contained in:
xst
2026-07-15 10:55:08 +08:00
parent 7c5a4c5de7
commit 49eb9bcd2b
5 changed files with 34 additions and 26 deletions
+7 -8
View File
@@ -116,7 +116,9 @@ def ensure_login(web_config, page, config):
if auth_role == role:
# 验证登录态是否仍然有效
page.goto(base_url)
if "/login" not in page.url:
# Hash 路由模式下判断是否重定向到登录页
page.wait_for_timeout(2000) # 等 hash 路由完成
if "/#/login" not in page.url and "/login" not in page.url:
return # 登录态有效
# 登录态过期,给出明确指引
raise RuntimeError(
@@ -137,22 +139,19 @@ def ensure_login(web_config, page, config):
)
# ── skip_login: false,执行 UI 登录(可能有验证码,会失败) ──
page.goto(f"{base_url}/login")
page.goto(f"{base_url}/#/login")
page.get_by_placeholder("请输入用户名").fill(account["phone"])
page.get_by_placeholder("请输入密码").fill(account["password"])
page.get_by_role("button", name="登录").click()
# 如果有图形验证码,这里会超时
try:
page.wait_for_url(f"{base_url}/**", timeout=10_000)
except Exception:
# 等几秒让页面处理路由跳转
page.wait_for_timeout(3000)
if "/#/login" in page.url or "/login" in page.url:
raise RuntimeError(
"登录失败!可能存在图形验证码。\n"
"请改为手动登录生成 auth 文件: python tests/web/save_auth.py --role {role}"
)
return _ensure_login
def _load_auth_state(context_kwargs: dict, storage_dir: Path, browser: str) -> str | None:
"""尝试加载登录态,返回角色名或 None。"""