154 lines
7.6 KiB
Python
154 lines
7.6 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
阶段 11 · 悬停展开两条收口的验收件(2026-11-08)
|
||
|
||
A 组:悬停通道「收起后自己再开」缺陷(hoverArmed 保险 + 真实移动判据)
|
||
B 组:抽屉收起后 rc-drawer 焦点哨兵滞留 Tab 序列(destroyOnHidden)
|
||
|
||
读法 B:顶栏 button.nex-console 悬停 = 展开项目概况抽屉。
|
||
默认 BASE=http://127.0.0.1:5173 · 视口 1440×900 · dark。
|
||
产物:hoverfix.json(逐条 got/want + 事件时间线)、hoverfix-final.png。
|
||
退出码:0 = 全部通过。
|
||
"""
|
||
import json, os, sys
|
||
from playwright.sync_api import sync_playwright
|
||
|
||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||
BASE = os.environ.get("NEX_BASE", "http://127.0.0.1:5173")
|
||
THEME = os.environ.get("NEX_THEME", "dark")
|
||
WIDTH = int(os.environ.get("NEX_WIDTH", "1440"))
|
||
TRIG = "button.nex-console"
|
||
R = []
|
||
|
||
def ck(name, got, want):
|
||
ok = got == want
|
||
R.append({"case": name, "got": got, "want": want, "ok": ok})
|
||
print((" PASS " if ok else " FAIL ") + f"{name}: got={got!r} want={want!r}")
|
||
return ok
|
||
|
||
def isopen(pg): return pg.evaluate("() => !!document.querySelector('.ant-drawer-open')")
|
||
def sentinels(pg): return pg.evaluate("() => document.querySelectorAll('[data-sentinel]').length")
|
||
def roots(pg): return pg.evaluate("() => document.querySelectorAll('body > div.ant-drawer').length")
|
||
def focused(pg):
|
||
return pg.evaluate("""() => {const e=document.activeElement; if(!e||e===document.body) return 'BODY';
|
||
return [e.tagName,(e.getAttribute('data-sentinel')||''),(typeof e.className==='string'?e.className:'').slice(0,28),
|
||
(e.innerText||'').trim().slice(0,12)].join('|')}""")
|
||
def close_it(pg, limit=6):
|
||
"""确保抽屉收起:Esc;仍开着就用 ✕ 按钮点掉。返回最终 open 状态。"""
|
||
for _ in range(limit):
|
||
if not isopen(pg):
|
||
break
|
||
pg.keyboard.press("Escape"); pg.wait_for_timeout(700)
|
||
if isopen(pg) and pg.locator(".ant-drawer-close, .ant-drawer button[aria-label]").count():
|
||
try:
|
||
pg.locator(".ant-drawer button[aria-label]").first.click(timeout=2000)
|
||
except Exception:
|
||
pass
|
||
pg.wait_for_timeout(700)
|
||
return isopen(pg)
|
||
|
||
with sync_playwright() as pw:
|
||
b = pw.chromium.launch(); pg = b.new_page(viewport={"width": WIDTH, "height": 900})
|
||
pg.goto(BASE + "/login", wait_until="networkidle")
|
||
pg.evaluate("(t) => localStorage.setItem('nex-the', t)", THEME)
|
||
pg.fill("#login-user", "admin"); pg.fill("#login-pass", "admin1234")
|
||
pg.click("button[type=submit]"); pg.wait_for_timeout(1500)
|
||
pg.goto(BASE + "/projects/10/overview", wait_until="networkidle"); pg.wait_for_timeout(2500)
|
||
pb = pg.locator(TRIG).bounding_box()
|
||
cx, cy = pb["x"] + pb["width"] / 2, pb["y"] + pb["height"] / 2
|
||
|
||
# 事件记录仪:证明「补发的那一束里没有 mousemove」
|
||
pg.evaluate("""() => {
|
||
window.__ev = []; const t0 = performance.now();
|
||
const btn = document.querySelector('button.nex-console');
|
||
['mouseover','mouseenter','mousemove','mouseleave'].forEach(t =>
|
||
btn.addEventListener(t, e => window.__ev.push(Math.round(performance.now()-t0)+'ms '+t)));
|
||
new MutationObserver(() => window.__ev.push(Math.round(performance.now()-t0)+'ms open='
|
||
+ !!document.querySelector('.ant-drawer-open')))
|
||
.observe(document.body, {subtree:true, attributes:true, attributeFilter:['class']});
|
||
}""")
|
||
print("== 前提:抽屉从未打开 ==")
|
||
ck("初始 open", isopen(pg), False)
|
||
ck("初始哨兵数", sentinels(pg), 0)
|
||
|
||
print("== A1 悬停 700ms 展开(读法 B)==")
|
||
pg.mouse.move(2, 2); pg.wait_for_timeout(300)
|
||
pg.mouse.move(cx, cy); pg.wait_for_timeout(700)
|
||
ck("悬停 700ms 后 open", isopen(pg), True)
|
||
|
||
print("== A2 移开不自动关(既定设计)==")
|
||
pg.mouse.move(40, 700); pg.wait_for_timeout(900)
|
||
ck("移开 900ms 后仍 open", isopen(pg), True)
|
||
|
||
print("== A3 指针在胶囊外按 Esc:收起并保持,且整棵抽屉卸载 ==")
|
||
pg.keyboard.press("Escape"); pg.wait_for_timeout(1600)
|
||
ck("Esc 后 open", isopen(pg), False)
|
||
ck("收起后 .ant-drawer 根节点数", roots(pg), 0)
|
||
ck("收起后哨兵数", sentinels(pg), 0)
|
||
|
||
print("== A4 指针移出再移回:悬停仍然可用(保险没误伤正常路径)==")
|
||
pg.mouse.move(cx, cy); pg.wait_for_timeout(700)
|
||
ck("再次悬停 open", isopen(pg), True)
|
||
|
||
print("== A5 指针停在胶囊上按 Esc:不得自己再开(本轮缺陷)==")
|
||
pg.mouse.move(40, 700); pg.wait_for_timeout(500)
|
||
close_it(pg)
|
||
pg.mouse.move(cx, cy); pg.wait_for_timeout(700)
|
||
ck("A5 前置:悬停已展开", isopen(pg), True)
|
||
pg.evaluate("() => window.__ev.push('--- ESC ---')")
|
||
pg.keyboard.press("Escape"); pg.wait_for_timeout(2000)
|
||
ck("Esc 2s 后仍收起(保险生效)", isopen(pg), False)
|
||
ev = pg.evaluate("() => window.__ev")
|
||
tail = ev[ev.index('--- ESC ---'):] if '--- ESC ---' in ev else ev
|
||
kinds = [l.split('ms ')[-1] for l in tail]
|
||
ck("补发束里有 mouseenter(缺陷来源确实存在)", any(k == 'mouseenter' for k in kinds), True)
|
||
ck("补发束里没有 mousemove(真实移动判据的前提)", any(k == 'mousemove' for k in kinds), False)
|
||
print(" Esc 之后事件:", " / ".join(l for l in tail if 'open=false' not in l)[:220])
|
||
|
||
print("== A6 停在原地的指针挪一下:重新上膛并展开 ==")
|
||
pg.mouse.move(cx + 6, cy + 3); pg.wait_for_timeout(800)
|
||
ck("真实移动后悬停 open", isopen(pg), True)
|
||
|
||
print("== B1 键盘出口:Shift+Tab 回到胶囊 → Enter 收起 ==")
|
||
for _ in range(30):
|
||
pg.keyboard.press("Shift+Tab"); pg.wait_for_timeout(120)
|
||
if pg.evaluate("() => document.activeElement === document.querySelector('button.nex-console')"):
|
||
break
|
||
ck("焦点回到胶囊", pg.evaluate("() => document.activeElement === document.querySelector('button.nex-console')"), True)
|
||
pg.keyboard.press("Enter"); pg.wait_for_timeout(900)
|
||
ck("Enter 收起(点按语义不变)", isopen(pg), False)
|
||
|
||
print("== B2 收起后反向 Tab:不得停在隐形哨兵 ==")
|
||
pg.evaluate("()=>{document.body.setAttribute('tabindex','-1');document.body.focus()}"); pg.wait_for_timeout(250)
|
||
trail = []
|
||
for _ in range(6):
|
||
pg.keyboard.press("Shift+Tab"); pg.wait_for_timeout(140); trail.append(focused(pg))
|
||
ck("Shift+Tab 轨迹中的哨兵站", sum(1 for t in trail if '|start|' in t or '|end|' in t), 0)
|
||
print(" 轨迹:", trail)
|
||
|
||
print("== B3 收起后正向 Tab 40 站:不得出现 0×0 无名站 ==")
|
||
pg.evaluate("()=>{document.body.setAttribute('tabindex','-1');document.body.focus()}"); pg.wait_for_timeout(250)
|
||
stop, zero, zero0 = 0, 0, 0
|
||
for _ in range(40):
|
||
pg.keyboard.press("Tab"); pg.wait_for_timeout(110)
|
||
f = focused(pg)
|
||
if f == 'BODY': break
|
||
stop += 1
|
||
if '|start|' in f or '|end|' in f: zero += 1
|
||
if 'BODY' not in f and pg.evaluate("()=>{const r=document.activeElement.getBoundingClientRect();return r.width<2||r.height<2}"):
|
||
zero0 += 1
|
||
ck("正向 Tab 里的哨兵站", zero, 0)
|
||
ck("正向 Tab 里的 0 尺寸站", zero0, 0)
|
||
print(f" 走了 {stop} 站")
|
||
|
||
pg.screenshot(path=os.path.join(HERE, "hoverfix-final.png"))
|
||
b.close()
|
||
|
||
fails = [x for x in R if not x["ok"]]
|
||
json.dump({"pass": len(R) - len(fails), "fail": len(fails), "cases": R,
|
||
"esc_timeline": tail}, open(os.path.join(HERE, "hoverfix.json"), "w"), ensure_ascii=False, indent=1)
|
||
print(f"\nHOVERFIX {'OK' if not fails else 'FAIL'} 通过 {len(R)-len(fails)} / 失败 {len(fails)}"
|
||
f" 明细 {os.path.join(HERE,'hoverfix.json')}")
|
||
sys.exit(0 if not fails else 1)
|