# -*- 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 = ["""
| 结果 | 检查了什么 | 说明 |
|---|---|---|
| %s | %s | %s |