74 lines
3.0 KiB
Python
74 lines
3.0 KiB
Python
"""
|
|
e2e_r3_v2.py — 入口脚本: 调引擎跑 20 控件端到端 (v2: 2026-06-17)
|
|
|
|
用法:
|
|
cd .claude/skills/jnpf-browser-automation/scripts
|
|
python3 e2e_r3_v2.py # 默认 controls_config.json
|
|
python3 e2e_r3_v2.py my_config.json # 自定义 config
|
|
"""
|
|
import os, sys, json, time
|
|
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = "/home/ubuntu/.cache/ms-playwright"
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
# 允许从同目录 import 引擎
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from e2e_r3_v2_engine import ControlEngine
|
|
|
|
CONFIG_PATH = sys.argv[1] if len(sys.argv) > 1 else os.path.join(
|
|
os.path.dirname(os.path.abspath(__file__)), "controls_config.json"
|
|
)
|
|
# v2 (2026-06-17): 20 控件 (含 v1 原有 10 + v2 新增 10)
|
|
OUT_DIR = f"/home/ubuntu/ccWorkDir/docs/tdd/e2e-controls-{time.strftime('%Y-%m-%d')}-r20"
|
|
|
|
def main():
|
|
print(f">>> 配置: {CONFIG_PATH}", flush=True)
|
|
print(f">>> 输出: {OUT_DIR}", flush=True)
|
|
|
|
with sync_playwright() as p:
|
|
browser = p.chromium.launch(headless=True)
|
|
ctx = browser.new_context(viewport={"width": 1440, "height": 900})
|
|
|
|
# 注入 token (复用 e2e_r3.py 注入脚本)
|
|
ctx.add_init_script("""
|
|
(function() {
|
|
const origFetch = window.fetch;
|
|
window.fetch = function(input, init) {
|
|
init = init || {};
|
|
init.headers = init.headers || {};
|
|
const u = (typeof input === 'string') ? input : (input.url || '');
|
|
if (!u.includes('/api/oauth/Login') && !init.headers['Authorization'] && !init.headers['authorization']) {
|
|
try {
|
|
const dacliTok = localStorage.getItem('__dacli_token');
|
|
if (dacliTok) init.headers['Authorization'] = dacliTok;
|
|
} catch(e) {}
|
|
}
|
|
return origFetch.apply(this, arguments);
|
|
};
|
|
})();
|
|
""")
|
|
page = ctx.new_page()
|
|
with open("/home/ubuntu/ccWorkDir/.dacli-token/token.json") as f:
|
|
tok = json.load(f)["token"]
|
|
# 盲点 #43 修复: 先 goto 真实 origin, 避免 about:blank SecurityError
|
|
# 2026-06-17 v2: timeout 15s → 60s, 容器内 nginx 冷启动会慢
|
|
page.goto("http://nginx/", timeout=60000)
|
|
page.evaluate(f"localStorage.setItem('__dacli_token', '{tok}')")
|
|
# 跳回 about:blank 状态,让后续 _login_and_open_designer 正常控制导航
|
|
page.goto("about:blank")
|
|
|
|
engine = ControlEngine(page, CONFIG_PATH)
|
|
summary = engine.run_all()
|
|
|
|
print(f"\n=== 总结 ===", flush=True)
|
|
print(f"total={summary['total']}, ok={summary['ok']}, fail={summary['fail']}", flush=True)
|
|
# 写 summary.json
|
|
os.makedirs(OUT_DIR, exist_ok=True)
|
|
with open(f"{OUT_DIR}/summary.json", "w", encoding="utf-8") as f:
|
|
json.dump(summary, f, ensure_ascii=False, indent=2)
|
|
|
|
browser.close()
|
|
return 0 if summary["fail"] == 0 else 1
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|