Files

150 lines
8.3 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""人话报告:收集每一步结果,最后生成 out/report.html + out/report.md。"""
import datetime
import html
import os
ICON = {"ok": "✅", "fail": "❌", "warn": "⚠️", "new": "🆕", "skip": "⏭️", "info": "ℹ️"}
SHOT_ICON = {"same": "✅", "noise": "⚠️", "changed": "❌", "size": "❌", "new": "🆕", "updated": "🔄"}
class Report:
def __init__(self, update_mode, browser_used=""):
self.update_mode = update_mode
self.browser_used = browser_used
self.started = datetime.datetime.now()
self.pages = [] # [{id, 名称, steps:[{status,text,detail}], shots:[{...}], note}]
self._cur = None
# ---------------------------------------------------------------- 收集
def begin_page(self, pid, name, note=""):
self._cur = {"id": pid, "名称": name, "steps": [], "shots": [], "note": note}
self.pages.append(self._cur)
def step(self, status, text, detail=""):
self._cur["steps"].append({"status": status, "text": text, "detail": detail})
def shot(self, sid, name, shot_rel, base_rel, diff_rel, verdict, note=""):
self._cur["shots"].append({"sid": sid, "name": name, "shot": shot_rel,
"base": base_rel, "diff": diff_rel,
"verdict": verdict, "note": note})
# ---------------------------------------------------------------- 统计
def page_verdict(self, page):
bad = [s for s in page["steps"] if s["status"] == "fail"] + \
[s for s in page["shots"] if s["verdict"] in ("changed", "size")]
if bad:
return "fail", len(bad)
warn = [s for s in page["steps"] if s["status"] == "warn"] + \
[s for s in page["shots"] if s["verdict"] == "noise"]
if warn:
return "warn", len(warn)
return "ok", 0
def counts(self):
n_fail = sum(1 for p in self.pages if self.page_verdict(p)[0] == "fail")
n_warn = sum(1 for p in self.pages if self.page_verdict(p)[0] == "warn")
n_ok = len(self.pages) - n_fail - n_warn
steps_bad = sum(1 for p in self.pages for s in p["steps"] if s["status"] == "fail")
shots_bad = sum(1 for p in self.pages for s in p["shots"]
if s["verdict"] in ("changed", "size"))
return n_ok, n_warn, n_fail, steps_bad, shots_bad
# ---------------------------------------------------------------- 落盘
def write(self, out_dir):
os.makedirs(out_dir, exist_ok=True)
html_path = os.path.join(out_dir, "report.html")
md_path = os.path.join(out_dir, "report.md")
with open(html_path, "w", encoding="utf-8") as f:
f.write(self._html())
with open(md_path, "w", encoding="utf-8") as f:
f.write(self._md())
return html_path, md_path
# ---------------------------------------------------------------- HTML
def _html(self):
n_ok, n_warn, n_fail, _, _ = self.counts()
mode = "更新底片模式(以本次为准重新留底)" if self.update_mode else "体检模式(与底片对比)"
parts = ["""<!doctype html><html lang="zh-CN"><head><meta charset="utf-8">
<title>界面体检报告</title><style>
body{font-family:"Microsoft YaHei",system-ui,sans-serif;margin:0;background:#f5f7fa;color:#1c2733;font-size:14px}
.wrap{max-width:1500px;margin:0 auto;padding:18px}
h1{color:#123a6b;font-size:22px} h2{color:#123a6b;border-bottom:2px solid #1f5fae;padding-bottom:4px}
.sum{display:flex;gap:14px;margin:12px 0}
.card{background:#fff;border:1px solid #c9d4e2;border-radius:8px;padding:10px 18px;font-size:15px}
.card b{font-size:24px;display:block}
.page{background:#fff;border:1px solid #c9d4e2;border-radius:8px;padding:14px 18px;margin:14px 0}
table{border-collapse:collapse;width:100%;margin:8px 0}
td,th{border:1px solid #dfe7f0;padding:5px 10px;text-align:left;vertical-align:top}
th{background:#eef3fa}
.fail{color:#c0392b;font-weight:600} .warn{color:#b8860b} .ok{color:#1e7d34}
.shots{display:flex;flex-wrap:wrap;gap:12px}
.shot{border:1px solid #c9d4e2;border-radius:6px;padding:8px;background:#fafcff}
.shot img{max-width:460px;border:1px solid #eee;display:block;margin:4px 0}
.meta{color:#5a6b7f;font-size:12px}
</style></head><body><div class="wrap">"""]
verdict_all = "❌ 有问题" if n_fail else ("⚠️ 有轻微差异" if n_warn else "✅ 全部通过")
parts.append("<h1>界面及功能体检报告</h1>")
parts.append('<p class="meta">时间:%s | 模式:%s | 相机:浏览器 %s</p>'
% (self.started.strftime("%Y-%m-%d %H:%M:%S"),
html.escape(mode), html.escape(self.browser_used or "-")))
parts.append('<div class="sum"><div class="card"><b>%s</b>总结果</div>'
'<div class="card ok"><b>%d</b>通过页面</div>'
'<div class="card warn"><b>%d</b>轻微差异页</div>'
'<div class="card fail"><b>%d</b>有问题页</div></div>'
% (verdict_all, n_ok, n_warn, n_fail))
for p in self.pages:
v, n = self.page_verdict(p)
vt = {("fail",): "❌", ("warn",): "⚠️"}.get((v,), "✅")
parts.append('<div class="page"><h2>%s %s</h2>' % (vt, html.escape(p["名称"])))
if p["note"]:
parts.append('<p class="meta">%s</p>' % html.escape(p["note"]))
if p["steps"]:
parts.append("<table><tr><th style='width:52px'>结果</th><th>检查了什么</th><th>说明</th></tr>")
for s in p["steps"]:
d = html.escape(s["detail"]) if s["detail"] else ""
parts.append("<tr><td class='%s'>%s</td><td>%s</td><td class='meta'>%s</td></tr>"
% (s["status"], ICON.get(s["status"], "?"),
html.escape(s["text"]), d))
parts.append("</table>")
if p["shots"]:
parts.append("<h3>照片对比(底片 = 固定下来的样子)</h3><div class='shots'>")
for s in p["shots"]:
sv = s["verdict"]
parts.append("<div class='shot'><b>%s %s</b><span class='%s'>%s</span>"
"<span class='meta'>%s</span>" % (
SHOT_ICON.get(sv, "?"), html.escape(s["name"]),
"fail" if sv in ("changed", "size") else ("warn" if sv == "noise" else "ok"),
html.escape(s.get("note") or ""), ""))
if s["shot"]:
parts.append("<img src='%s' title='本次截图'>" % s["shot"])
if s["diff"]:
parts.append("<img src='%s' title='差异圈红(红=和底片不一样的地方)'>" % s["diff"])
parts.append("</div>")
parts.append("</div>")
parts.append("</div>")
parts.append("</div></body></html>")
return "".join(parts)
# ---------------------------------------------------------------- Markdown
def _md(self):
n_ok, n_warn, n_fail, steps_bad, shots_bad = self.counts()
L = ["# 界面体检报告(%s)" % self.started.strftime("%Y-%m-%d %H:%M"),
"",
"- 模式:%s" % ("更新底片" if self.update_mode else "体检对比"),
"- 总结果:%s(通过 %d 页 / 轻微差异 %d 页 / 有问题 %d 页;❌ 步骤 %d、❌ 照片 %d)"
% ("❌ 有问题" if n_fail else ("⚠️ 轻微差异" if n_warn else "✅ 全部通过"),
n_ok, n_warn, n_fail, steps_bad, shots_bad), ""]
for p in self.pages:
v, _ = self.page_verdict(p)
L.append("## %s %s" % (ICON["fail" if v == "fail" else ("warn" if v == "warn" else "ok")],
p["名称"]))
for s in p["steps"]:
L.append("- %s %s%s" % (ICON.get(s["status"], "?"), s["text"],
("(%s)" % s["detail"]) if s["detail"] else ""))
for s in p["shots"]:
L.append("- %s 照片【%s】%s" % (SHOT_ICON.get(s["verdict"], "?"), s["name"],
s.get("note") or ""))
L.append("")
return "\n".join(L)