Files
jianli-boss/client/debug_input_live_check.py
T

74 lines
2.3 KiB
Python

# -*- coding: utf-8 -*-
"""只读实测:在真实聊天页执行 activator.py 内的 _INPUT_BOX_JS / _FOCUS_INPUT_JS / _SEND_BTN_JS
(聚焦只验证可达性,绝不输入、绝不点击、绝不发送)"""
import json
import re
import time
from pathlib import Path
import requests
import websocket
CDP = "http://127.0.0.1:9222"
src = Path(__file__).resolve().parent / "browser" / "activator.py"
text = src.read_text(encoding="utf-8")
def extract(name):
return re.search(rf'{name} = """(.*?)"""', text, re.S).group(1)
INPUT_JS = extract("_INPUT_BOX_JS")
SEND_JS = extract("_SEND_BTN_JS")
# 聚焦验证:聚焦后记录并还原(只读会话内容不变,聚焦无副作用)
FOCUS_CHECK_JS = extract("_FOCUS_INPUT_JS")
_msg_id = 0
def call(ws, method, params=None):
global _msg_id
_msg_id += 1
ws.send(json.dumps({"id": _msg_id, "method": method, "params": params or {}}))
while True:
m = json.loads(ws.recv())
if m.get("id") == _msg_id:
return m.get("result") or {}
def ev(ws, expr):
return call(ws, "Runtime.evaluate",
{"expression": expr, "returnByValue": True}) \
.get("result", {}).get("value")
def main():
tab = None
for t in requests.get(f"{CDP}/json", timeout=5).json():
if "/web/geek/chat" in (t.get("url") or "") and t.get("webSocketDebuggerUrl"):
tab = t
break
if not tab:
print("FAIL 未找到聊天页标签")
return
ws = websocket.create_connection(tab["webSocketDebuggerUrl"], timeout=30)
try:
r1 = ev(ws, INPUT_JS)
print("输入框定位:", r1)
ok1 = False
if r1:
p = json.loads(r1)
# 命中真实输入框:可见的 contenteditable div,坐标为正(坐标每次实时计算)
ok1 = (p.get("visible") is True and p.get("kind") == "div"
and p["x"] > 0 and p["y"] > 0)
print("PASS" if ok1 else "FAIL", "真实页面输入框可定位且可见")
r2 = ev(ws, SEND_JS)
print("发送按钮:", r2)
ok2 = bool(r2)
print("PASS" if ok2 else "FAIL", "真实页面发送按钮可定位")
r3 = ev(ws, FOCUS_CHECK_JS)
print("聚焦检查:", r3)
print("PASS" if r3 is True else "FAIL", "输入框可聚焦")
finally:
ws.close()
if __name__ == "__main__":
main()