235 lines
12 KiB
Python
235 lines
12 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""决策 245 无头浏览器实测:工作台/后台「打开项目」弹框统一 + 单价类型落库 + 后台 Excel 通道。
|
||
|
||
真实 UI 路径(Playwright + 临时副本服务,生产库零接触):
|
||
① 工作台弹框:单价类型下拉 4 项默认预算综合单价、按钮行提示词逐字;
|
||
② 工作台必填:地区/建设单位不填被拦(提示文案与后台同款);
|
||
③ 工作台打开工程:选投标全费用 → session.db sessions.file_kind=3;刷新台账名称仍在;
|
||
④ 后台弹框:标题「打开项目」、字段名/顺序/按钮与工作台一致、提示词逐字;
|
||
⑤ 后台 Excel 通道:「导入Excel」展开 → 项目模板真实导入 → history.db 批次落库(FileKind/清单数);
|
||
⑥ 后台刷新:批次仍在列表里(数据持久化)。
|
||
跑法:python app/tests/_e2e_dialog245.py (产物 _dbg_e2e245/ 用完自清)
|
||
"""
|
||
import base64
|
||
import os
|
||
import shutil
|
||
import sqlite3
|
||
import subprocess
|
||
import sys
|
||
import time
|
||
from pathlib import Path
|
||
|
||
HERE = Path(__file__).resolve().parent
|
||
sys.path.insert(0, str(HERE))
|
||
from api_harness import _free_port, make_cos4 # noqa: E402
|
||
|
||
ROOT = HERE.parent.parent / "_dbg_e2e245"
|
||
CAM = ("030507008001", "高清网络摄像机", "1、400万像素;2、H.265", "台", 10, 1500)
|
||
METER = ("030405006001", "智能电度表", "1、三相;2、导轨式", "只", 20, 300)
|
||
LME = ("000001", "网络摄像机", "400万", "台", "1200", "1356")
|
||
NAME = "e2e决策245工程"
|
||
XLSX_NAME = "e2e决策245Excel工程"
|
||
REMIND = ("提醒:建议在打开或导入项目时,先把工程独有的增加费(暗室、超高降效、预算包干、"
|
||
"垂直运输等)取消后再入库或工作台的审核对比,否则容易造成数据失真!")
|
||
|
||
_ok = []
|
||
|
||
|
||
def step(name, cond, detail=""):
|
||
_ok.append(bool(cond))
|
||
print((" ✓ " if cond else " ✗ ") + name + (" [%s]" % detail if detail else ""), flush=True)
|
||
|
||
|
||
def main():
|
||
if ROOT.exists():
|
||
shutil.rmtree(ROOT)
|
||
app_dst = ROOT / "CostReview" / "app"
|
||
shutil.copytree(HERE.parent, app_dst,
|
||
ignore=shutil.ignore_patterns("__pycache__", "_deprecated", "tests"))
|
||
src_dir = ROOT / "后台需要导入的项目"
|
||
src_dir.mkdir()
|
||
lic = ROOT / "lic"
|
||
lic.mkdir()
|
||
(lic / "config.json").write_text('{"enforce": false}', encoding="utf-8")
|
||
port = _free_port()
|
||
env = dict(os.environ, PR_PORT=str(port), LICENSE_DATA_DIR=str(lic))
|
||
log = open(ROOT / "server.log", "wb")
|
||
proc = subprocess.Popen([sys.executable, "run_server_headless.py"],
|
||
cwd=str(app_dst), env=env, stdout=log, stderr=subprocess.STDOUT)
|
||
base = "http://127.0.0.1:%d" % port
|
||
import httpx
|
||
cl = httpx.Client(base_url=base, timeout=120)
|
||
deadline = time.time() + 30
|
||
while time.time() < deadline:
|
||
try:
|
||
if cl.get("/api/license/status").status_code == 200:
|
||
break
|
||
except Exception:
|
||
time.sleep(0.3)
|
||
else:
|
||
proc.kill()
|
||
print(open(ROOT / "server.log", encoding="utf-8", errors="replace").read())
|
||
return 1
|
||
|
||
f1 = src_dir / "e2e245工程.cos4"
|
||
make_cos4(str(f1), NAME, [CAM, METER], [LME])
|
||
# 项目模板:走真实下载端点(带示例数据),再原样回导
|
||
tpl = src_dir / "整项目导入模板.xlsx"
|
||
tpl.write_bytes(cl.get("/api/session/manual/project_template").content)
|
||
|
||
def fill_common(pg, pre="o"):
|
||
pg.evaluate("""(pre) => {
|
||
const set=(id,v)=>{const s=document.getElementById(id); if(!s) return;
|
||
if(![...s.options].some(o=>o.value===v)){const o=document.createElement('option');
|
||
o.value=v;o.textContent=v;s.appendChild(o);} s.value=v;};
|
||
set(pre+'prov','广东省'); set(pre+'city','珠海市'); set(pre+'dist','香洲区');
|
||
document.getElementById(pre+'buid').value='E2E建设单位';
|
||
}""", pre)
|
||
|
||
from playwright.sync_api import sync_playwright
|
||
with sync_playwright() as p:
|
||
br = p.chromium.launch(channel="msedge", headless=True)
|
||
pg = br.new_page()
|
||
|
||
# ---------- ① 工作台弹框 ----------
|
||
pg.goto(base + "/workbench.html")
|
||
pg.wait_for_load_state("networkidle")
|
||
pg.click("button:has-text('打开项目')")
|
||
pg.wait_for_selector("#omask.on #ofile")
|
||
opts = pg.eval_on_selector_all("#okind option", "els=>els.map(e=>[e.value,e.textContent])")
|
||
step("① 工作台单价类型 4 项", [o[1] for o in opts] == ["预算综合单价", "投标综合单价", "预算全费用", "投标全费用"],
|
||
str(opts))
|
||
step("① 默认预算综合单价", pg.input_value("#okind") == "1", pg.input_value("#okind"))
|
||
tip = (pg.locator("#omRemind").inner_text() or "").strip()
|
||
step("① 工作台按钮行提示词逐字", tip == REMIND, tip[:20])
|
||
|
||
# ---------- ② 必填拦截 ----------
|
||
pg.set_input_files("#ofile", str(f1))
|
||
pg.fill("#oname", NAME)
|
||
pg.evaluate("setPeriod(document.querySelector('#oper'),'2026-07','2026-07')")
|
||
pg.click("#obtnGo")
|
||
pg.wait_for_timeout(300)
|
||
m = (pg.locator("#omsg").inner_text() or "").strip()
|
||
step("② 地区未填被拦", m == "请填写:地区(省/市/区)", m)
|
||
fill_common(pg,'o')
|
||
pg.evaluate("document.getElementById('obuid').value=''")
|
||
pg.click("#obtnGo")
|
||
pg.wait_for_timeout(300)
|
||
m = (pg.locator("#omsg").inner_text() or "").strip()
|
||
step("② 建设单位未填被拦", m == "请填写:建设单位", m)
|
||
|
||
# ---------- ③ 打开工程(单价类型=3 投标全费用)----------
|
||
fill_common(pg,'o')
|
||
pg.select_option("#okind", "3")
|
||
pg.click("#obtnGo")
|
||
ok_open = False
|
||
for _ in range(300):
|
||
pg.wait_for_timeout(100)
|
||
if "on" not in (pg.locator("#omask").get_attribute("class") or ""):
|
||
ok_open = True
|
||
break
|
||
step("③ 打开工程成功(弹框关闭)", ok_open,
|
||
(pg.locator("#omsg").inner_text() or "").strip())
|
||
sdb = str(ROOT / "CostReview" / "data" / "session.db")
|
||
con = sqlite3.connect(sdb)
|
||
row = con.execute("SELECT id, name, file_kind, bulid_unit FROM sessions WHERE TRIM(name)=?",
|
||
(NAME,)).fetchone()
|
||
con.close()
|
||
step("③ sessions.file_kind=3 落库", bool(row) and row[2] == 3, str(row))
|
||
# 刷新台账:名称仍在
|
||
pg.goto(base + "/sessions.html")
|
||
pg.wait_for_load_state("networkidle")
|
||
step("③ 刷新后台账仍在", pg.locator("tr", has_text=NAME).count() >= 1)
|
||
|
||
# ---------- ④ 后台弹框与工作台一致 ----------
|
||
pg.goto(base + "/admin.html")
|
||
pg.wait_for_load_state("networkidle")
|
||
pg.click("#btnImp")
|
||
pg.wait_for_selector("#imask.on #ifile")
|
||
step("④ 标题=打开项目", (pg.locator("#impTitle").inner_text() or "").strip() == "打开项目",
|
||
pg.locator("#impTitle").inner_text())
|
||
step("④ 字段名一致",
|
||
("全国电子标书" in pg.locator("#irowFile").inner_text())
|
||
and pg.locator("span:has-text('工程名称 *')").count() >= 1
|
||
and pg.locator("span:has-text('基准期次 *')").count() >= 1,
|
||
pg.locator("#ileft").inner_text().replace("\n", "|")[:60])
|
||
step("④ 按钮 取消|导入Excel|打开",
|
||
(pg.locator("#impBtnX").inner_text() or "").strip() == "导入Excel"
|
||
and (pg.locator("#ibtnGo").inner_text() or "").strip() == "打开")
|
||
tip2 = (pg.locator("#imRemind").inner_text() or "").strip()
|
||
step("④ 后台提示词逐字", tip2 == REMIND, tip2[:20])
|
||
order = pg.evaluate("""() => ['#irowFile','#irowPer','#irowFk','#irowBuid'].map(s=>s)""")
|
||
step("④ 必填项齐全(单价类型行存在)", pg.locator("#irowFk").is_visible(), str(order))
|
||
|
||
# ---------- ⑤ 后台 Excel 通道 ----------
|
||
pg.fill("#iname", XLSX_NAME)
|
||
pg.evaluate("setPeriod(document.querySelector('#iper'),'2026-07','2026-07')")
|
||
fill_common(pg,'i')
|
||
pg.click("#impBtnX")
|
||
pg.wait_for_selector("#apanel", state="visible")
|
||
step("⑤ 树区展开、按钮同款",
|
||
pg.locator("#aitree button:has-text('单位模板')").count() == 1
|
||
and pg.locator("#aitree button:has-text('项目模板')").count() == 1
|
||
and pg.locator("#aitree button:has-text('项目导入')").count() == 1
|
||
and pg.locator("#aitree button:has-text('+单项工程')").count() == 1)
|
||
step("⑤ 树区展开后主按钮=创建工程",
|
||
(pg.locator("#ibtnGo").inner_text() or "").strip() == "创建工程",
|
||
pg.locator("#ibtnGo").inner_text())
|
||
pg.set_input_files("#iprojfile", str(tpl))
|
||
ok_x = False
|
||
for _ in range(400):
|
||
pg.wait_for_timeout(100)
|
||
if "on" not in (pg.locator("#imask").get_attribute("class") or ""):
|
||
ok_x = True
|
||
break
|
||
step("⑤ 项目模板导入成功(弹框关闭)", ok_x, (pg.locator("#aimsg").inner_text() or "").strip())
|
||
hdb = str(ROOT / "CostReview" / "data" / "history.db")
|
||
con = sqlite3.connect(hdb)
|
||
b = con.execute("SELECT id, Name, FileKind, BulidUnit, x_rows_bill FROM batches "
|
||
"WHERE Name=?", (XLSX_NAME,)).fetchone()
|
||
n_uw = n_sec = n_bill = 0
|
||
if b:
|
||
n_uw = con.execute("SELECT COUNT(*) FROM unit_works WHERE batch_id=?", (b[0],)).fetchone()[0]
|
||
n_sec = con.execute("SELECT COUNT(*) FROM sections WHERE batch_id=?", (b[0],)).fetchone()[0]
|
||
n_bill = con.execute("SELECT COUNT(*) FROM bills WHERE batch_id=?", (b[0],)).fetchone()[0]
|
||
con.close()
|
||
step("⑤ 批次落库(FileKind=1/1 单项/2 单位/清单>0)",
|
||
bool(b) and b[2] == 1 and n_sec == 1 and n_uw == 2 and n_bill > 0,
|
||
"batch=%s sec=%d uw=%d bill=%d" % (b, n_sec, n_uw, n_bill))
|
||
# 分部分项/措施项目两类都在
|
||
con = sqlite3.connect(hdb)
|
||
kinds = dict(con.execute("SELECT COALESCE(Kind,'item'), COUNT(*) FROM bills WHERE batch_id=? "
|
||
"GROUP BY COALESCE(Kind,'item')", (b[0],)).fetchall())
|
||
con.close()
|
||
step("⑤ 分部分项+措施项目都有", kinds.get("item", 0) > 0 and kinds.get("prelim", 0) > 0, str(kinds))
|
||
|
||
# ---------- ⑤b 价格类弹框不受影响(发布单位/附件/备注、按钮=开始导入、无导入Excel) ----------
|
||
pg.click(".tab[data-kind='market']")
|
||
pg.wait_for_timeout(400)
|
||
pg.click("#btnImp")
|
||
pg.wait_for_selector("#imask.on #ifile")
|
||
pg.wait_for_timeout(300)
|
||
step("⑤b 价格类:发布单位可见 / 单价类型隐藏 / 按钮=开始导入 / 无导入Excel",
|
||
pg.locator("#irowPub").is_visible() and not pg.locator("#irowFk").is_visible()
|
||
and (pg.locator("#ibtnGo").inner_text() or "").strip() == "开始导入"
|
||
and not pg.locator("#impBtnX").is_visible())
|
||
pg.click("#imask .btn:has-text('取消')")
|
||
|
||
# ---------- ⑥ 刷新持久化 ----------
|
||
pg.goto(base + "/admin.html")
|
||
pg.wait_for_load_state("networkidle")
|
||
pg.wait_for_timeout(800)
|
||
step("⑥ 刷新后列表里批次仍在", pg.locator("tr", has_text=XLSX_NAME).count() >= 1)
|
||
|
||
br.close()
|
||
proc.kill()
|
||
print("\n结果:%d/%d 通过" % (sum(_ok), len(_ok)), flush=True)
|
||
return 0 if all(_ok) else 1
|
||
|
||
|
||
if __name__ == "__main__":
|
||
rc = main()
|
||
if os.environ.get("KEEP_E2E") != "1" and (rc == 0):
|
||
shutil.rmtree(ROOT, ignore_errors=True)
|
||
sys.exit(rc)
|