153 lines
6.5 KiB
Python
153 lines
6.5 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""决策 247 常驻防线:「打开项目」弹框打开即清空——字段清单自动盘点式回归。
|
||
|
||
防的是整个「弹框残留」类毛病(决策 50 → 247 同类三犯:清值链靠手工枚举,加新字段、
|
||
换层次就漏)。所以本测试**不枚举固定字段**,而是自己盘点弹框里当前的全部人可填字段,
|
||
并且连隐藏层一起管:
|
||
- comboAttach 组合框:select 本体值 + 镜像框显示(247 后台漏的层)
|
||
- comboFree 自由输入:值 + 清空小叉显隐(决策 50 的「空框带小叉」死角)
|
||
- 期次控件:value + dataset.pf/pt(决策 50 的「假清空」,PVAL 读 dataset)
|
||
以后往弹框加新字段:盘点自动纳入,漏清就红灯——不需要改本测试。
|
||
|
||
流程(两侧同一路径):打开弹框 → 逐字段注入假值 → 点「取消」→ 重开 → 逐字段断言空。
|
||
唯一白名单 = 单价类型回默认「预算综合单价」(值 1,决策 245)。
|
||
跑法:随全量 pytest;复用 api_harness 的 session 级 server 夹具(临时副本服务,
|
||
生产库零接触),本模块只多花一次开浏览器的钱。
|
||
"""
|
||
import pytest
|
||
|
||
pytest.importorskip("playwright.sync_api", reason="无头浏览器防线需要 playwright")
|
||
from playwright.sync_api import sync_playwright # noqa: E402
|
||
|
||
from api_harness import server # noqa: F401,E402 (夹具须经导入注册)
|
||
|
||
SENTINEL = "残留试探247"
|
||
|
||
# 允许重开后的非空默认值(决策 245:单价类型默认预算综合单价)
|
||
ALLOW_DEFAULT = {"okind": "1", "ifk": "1"}
|
||
|
||
# 盘点:弹框内全部人可填字段。comboAttach 的 select 本体是 display:none 也要管(残留就在镜像层);
|
||
# 折叠面板(导入Excel 树区)、价格类专属行、file/hidden 不在本次弹框形态里,按不可见跳过。
|
||
INVENTORY = r"""
|
||
(root) => {
|
||
const out = [];
|
||
for (const el of root.querySelectorAll('input,select,textarea')) {
|
||
if (el.type === 'hidden' || el.type === 'file') continue;
|
||
const per = el.classList.contains('perinp'); // 期次控件
|
||
const csel = !!(el.tagName === 'SELECT' && el._combo); // comboAttach 组合框本体
|
||
const cfree = el.tagName === 'INPUT' && !!el.parentNode.classList?.contains('combo'); // comboFree
|
||
if (el.offsetParent === null && !csel) continue; // 不可见(折叠/隐藏行)跳过
|
||
if (!el.id) {
|
||
// comboAttach 的镜像 input(无 id)已归其 select 本体的 mir 层管;其余无 id 字段锚不定,红线报错
|
||
if (el.parentNode.classList?.contains('combo')) continue;
|
||
throw new Error('弹框里有无 id 的字段,盘点无法锚定:' + el.outerHTML.slice(0, 80));
|
||
}
|
||
out.push({ id: el.id, kind: per ? 'per' : (csel ? 'csel' : (cfree ? 'cfree' : el.tagName.toLowerCase())) });
|
||
}
|
||
return out;
|
||
}"""
|
||
|
||
# 注入假值(按层写:本体/镜像/期次 dataset/小叉)
|
||
INJECT = r"""
|
||
([root, flds]) => {
|
||
const V = "残留试探247";
|
||
for (const f of flds) {
|
||
const el = root.querySelector('[id="' + f.id + '"]');
|
||
if (f.kind === 'per') { setPeriod(el, '2026-11', '2026-12'); continue; }
|
||
if (f.kind === 'csel' || f.kind === 'select') {
|
||
if (![...el.options].some(o => o.value === V)) el.add(new Option(V, V));
|
||
el.value = V;
|
||
if (el._combo) el._combo.refresh();
|
||
continue;
|
||
}
|
||
el.value = V;
|
||
el.dispatchEvent(new Event('input', {bubbles: true}));
|
||
}
|
||
}"""
|
||
|
||
# 重开后逐层读回
|
||
READ = r"""
|
||
([root, flds]) => {
|
||
const out = {};
|
||
for (const f of flds) {
|
||
const el = root.querySelector('[id="' + f.id + '"]');
|
||
const st = {};
|
||
if (f.kind === 'per') {
|
||
st.v = el.value; st.pf = el.dataset.pf || ''; st.pt = el.dataset.pt || '';
|
||
} else if (f.kind === 'csel') {
|
||
st.v = el.value; st.mir = el._combo.inp.value;
|
||
} else if (f.kind === 'cfree') {
|
||
st.v = el.value;
|
||
const x = el.parentNode.querySelector('.combo-x');
|
||
st.fork = x ? x.style.display : null;
|
||
} else {
|
||
st.v = el.value;
|
||
}
|
||
out[f.id] = st;
|
||
}
|
||
return out;
|
||
}"""
|
||
|
||
DIALOGS = [
|
||
dict(name="工作台-打开项目", page="/workbench.html",
|
||
open_btn="button:has-text('打开项目')", cancel_btn="#omask button:has-text('取消')",
|
||
root="#odlg", combo_ready="#oprov"),
|
||
dict(name="后台-打开项目", page="/admin.html",
|
||
open_btn="#btnImp", cancel_btn="#imask button:has-text('取消')",
|
||
root="#imask", combo_ready="#iprov"),
|
||
]
|
||
|
||
|
||
@pytest.fixture(scope="module")
|
||
def pg(server):
|
||
with sync_playwright() as p:
|
||
br = p.chromium.launch(channel="msedge", headless=True)
|
||
page = br.new_page()
|
||
yield page, server["port"]
|
||
br.close()
|
||
|
||
|
||
def _open(pg, dlg):
|
||
pg.click(dlg["open_btn"])
|
||
pg.wait_for_selector(dlg["root"], state="visible")
|
||
pg.wait_for_function(
|
||
"document.querySelector('%s') && document.querySelector('%s')._combo" % (
|
||
dlg["combo_ready"], dlg["combo_ready"]))
|
||
pg.wait_for_timeout(500) # 等开框清值链 / loadImpFilters 异步收尾
|
||
|
||
|
||
@pytest.mark.parametrize("dlg", DIALOGS, ids=[d["name"] for d in DIALOGS])
|
||
def test_dialog_reopens_fresh(pg, dlg):
|
||
page, port = pg
|
||
page.goto("http://127.0.0.1:%d%s" % (port, dlg["page"]))
|
||
page.wait_for_load_state("networkidle")
|
||
|
||
# ① 盘点字段(盘点不到字段本身就是防线失效,直接红灯)
|
||
_open(page, dlg)
|
||
root = page.locator(dlg["root"]).element_handle()
|
||
flds = page.evaluate(INVENTORY, root)
|
||
assert len(flds) >= 5, "盘点到 %d 个字段,弹框结构可能变了:%s" % (len(flds), flds)
|
||
|
||
# ② 注入假值 → 取消 → 重开
|
||
page.evaluate(INJECT, [root, flds])
|
||
page.click(dlg["cancel_btn"])
|
||
page.wait_for_timeout(200)
|
||
_open(page, dlg)
|
||
root = page.locator(dlg["root"]).element_handle()
|
||
state = page.evaluate(READ, [root, flds])
|
||
|
||
# ③ 逐字段逐层断言:空 / 允许的默认值
|
||
bad = []
|
||
for f in flds:
|
||
st = state[f["id"]]
|
||
allow = ALLOW_DEFAULT.get(f["id"])
|
||
for layer, v in st.items():
|
||
if layer == "fork": # 小叉:none=空框已收叉(干净),亮着=残留
|
||
if v != "none":
|
||
bad.append("%s.%s=%r" % (f["id"], layer, v))
|
||
continue
|
||
if v == allow or v in ("", None):
|
||
continue
|
||
bad.append("%s.%s=%r" % (f["id"], layer, v))
|
||
assert not bad, "重开弹框残留(决策 247 类毛病又犯了):字段=%s,打开函数清值链漏了这些层" % ";".join(bad)
|