#!/usr/bin/env python3 # -*- coding: utf-8 -*- """BUG-001 验收:3页面×10次整页刷新,无卡"加载中"超10s(2026-09-03 修复后) 判据: location.href 整页刷新 → 轮询(0.5s)直到 body>100 且非壳页"加载中" → 耗时 <= 10s """ import sys, json, time sys.path.insert(0, "/root/.hermes/profiles/wecom-yanxue/skills/operations/cdp-browser-automation/scripts") from cdp_helpers import CDPBrowser CDP = "http://127.0.0.1:9226" BASE = "https://cma.sxbh.ltd" def pick_tab(url_needle): tabs = json.loads(__import__("urllib.request").request.urlopen(CDP + "/json", timeout=5).read()) for t in tabs: if t.get("type") == "page" and url_needle in t.get("url", ""): return t raise LookupError("无匹配tab: " + url_needle) def fresh_browser(url_needle): """复用已登录 tab(每轮重连,防导航断ws)""" b = CDPBrowser(cdp=CDP, base_url=BASE) b.use_existing_tab(url_needle) return b def reload_and_time(url, url_needle, max_wait=12): """整页刷新并计时到渲染完成。返回 (耗时秒, body_len, 超时?)""" b = fresh_browser(url_needle) t0 = time.time() try: b.js("location.href='" + url + "'") except Exception: pass # 导航即断ws属正常 body = "" waited = 0 last_err = None while waited < max_wait: time.sleep(0.5) waited += 0.5 try: body = b.js("document.body ? document.body.innerText : ''") or "" except Exception as e: last_err = e try: b = fresh_browser(url_needle) body = b.js("document.body ? document.body.innerText : ''") or "" except Exception as e2: last_err = e2 body = "" if len(body) > 100 and "加载中" not in body[:60]: return round(time.time() - t0, 2), len(body), False, "" return round(time.time() - t0, 2), len(body), True, str(last_err or "") targets = [ ("/ai-suggestions", "ai-suggestions"), ("/alerts", "alerts"), ("/my-dashboard", "my-dashboard"), ] N = 10 all_pass = True for url, needle in targets: print(f"\n===== {url} =====") # 预热 1 次(下载新版hash资源入缓存) try: t, bl, to, err = reload_and_time(url, needle) print(f"[预热] {t}s body={bl} timeout={to}") except Exception as e: print(f"[预热失败] {e} —— 检查tab是否存在/登录态") sys.exit(1) times = [] for i in range(1, N + 1): try: t, bl, to, err = reload_and_time(url, needle) except Exception as e: print(f"[{i}] 异常: {e}") all_pass = False times.append(99.0) continue flag = "❌" if to else "✅" if to: all_pass = False times.append(t) print(f"[{i:02d}] {flag} {t}s body={bl}" + (f" err={err[:60]}" if err else "")) over = [x for x in times if x > 10] print(f" 最快{min(times)}s 最慢{max(times)}s 平均{sum(times)/len(times):.2f}s 超10s次数={len(over)}") print("\n=== 验收结论 ===") print("PASS: 10连刷无卡'加载中'超10s" if all_pass else "FAIL: 存在卡'加载中'超10s")