"""博客管理浏览器验收:真实登录 → 新建/编辑/发布/预览/筛选/搜索/删除,校验刷新后持久化。""" import json import re import sys from playwright.sync_api import sync_playwright, expect BASE = "http://127.0.0.1:5173" USER = "admin" PASSWORD = "MySpace-local-2026" TAG = "验收-" + __import__("datetime").datetime.now().strftime("%H%M%S") DRAFT_TITLE = TAG + "-草稿文章" PUB_TITLE = TAG + "-已发布文章" OUT = "/Users/xzh/Documents/project/MySpace_Hifox/output/playwright/" results = [] def check(name, ok, detail=""): results.append((name, bool(ok), detail)) print(("PASS " if ok else "FAIL ") + name + ((" — " + detail) if detail and not ok else "")) def page_body(page): return page.locator(".app").inner_text() with sync_playwright() as p: browser = p.chromium.launch(channel="chrome", headless=True) page = browser.new_page(viewport={"width": 1440, "height": 1000}) errors = [] page.on("pageerror", lambda e: errors.append("PAGEERROR " + str(e))) page.on("crash", lambda: errors.append("CRASH")) page.on("console", lambda m: errors.append(m.text) if m.type == "error" else None) page.goto(BASE) page.fill('input[autocomplete="username"]', USER) page.fill('input[autocomplete="current-password"]', PASSWORD) page.get_by_role("button", name="进入我的空间").click() page.wait_for_selector(".sidebar") page.locator(".navitem", has_text="博客管理").click() page.wait_for_selector(".pageheading h1:has-text('博客管理')") check("导航进入博客管理且顶部按钮为写文章", page.get_by_role("button", name="+ 写文章").count() == 1 or page.get_by_role("button", name=re.compile("写文章")).count() >= 1) check("本地发布提示可见", "不会生成公网博客" in page.locator(".notice").inner_text()) check("侧栏不再标记待开发", "待开发" not in page.locator(".navitem", has_text="博客管理").inner_text()) check("筛选标签为 全部/草稿/已发布", [t.strip() for t in page.locator(".tabs .tab").all_inner_texts()] == ["全部", "草稿", "已发布"]) # 1. 新建文章,默认草稿 page.get_by_role("button", name=re.compile("写文章")).first.click() dialog = page.locator(".el-dialog:has-text('写一篇新文章')") dialog.wait_for() check("新建表单含标题/摘要/正文/状态", all(dialog.locator(s).count() == 1 for s in ["#f-blog-title", "#f-blog-summary", "#f-blog-body", "#f-blog-status"])) check("新建默认状态为草稿", dialog.locator("#f-blog-status").input_value() == "draft") check("正文默认值同原型", dialog.locator("#f-blog-body").input_value() == "# 新的故事\n\n从这里开始。") dialog.locator("#f-blog-title").fill(DRAFT_TITLE) dialog.locator("#f-blog-summary").fill("这是一条**验收**摘要") script_body = "正文段落。\n\n## 小节\n\n- 列表项\n> 引用\n\n`代码` 与 " dialog.locator("#f-blog-body").fill(script_body) dialog.get_by_role("button", name="保存文章").click() card = page.locator(".content-card", has_text=DRAFT_TITLE) card.wait_for() check("新建后列表出现文章且徽标为草稿", "草稿" in card.locator(".tag").first.inner_text()) page.wait_for_timeout(400) # run() 在保存请求期间置忙,等它释放 check("刷新前草稿列表仍含新文章", page.locator(".content-card", has_text=DRAFT_TITLE).count() == 1, str(page.locator(".content-card h3").all_inner_texts())) check("列表显示摘要", "这是一条**验收**摘要" in card.inner_text()) check("新建文章后状态筛选仍为全部", "active" in page.locator(".tabs .tab", has_text="全部").get_attribute("class")) page.screenshot(path=OUT + "blog-list.png", full_page=True) # 2. 编辑 → 状态改为已发布 card.get_by_role("button", name="编辑").click() edit = page.locator(".el-dialog:has-text('编辑文章')") edit.wait_for() check("编辑表单回填标题", edit.locator("#f-blog-title").input_value() == DRAFT_TITLE) edit.locator("#f-blog-title").fill(PUB_TITLE) edit.locator("#f-blog-status").select_option("published") edit.get_by_role("button", name="保存文章").click() pub_card = page.locator(".content-card", has_text=PUB_TITLE) pub_card.wait_for() page.wait_for_timeout(400) check("切换已发布后徽标为已发布", "已发布" in pub_card.locator(".tag").first.inner_text()) check("已发布徽标使用绿色标签", "tag green" in pub_card.locator(".tag").get_attribute("class")) # 3. 刷新后状态保留 page.reload() page.wait_for_selector(".sidebar") page.wait_for_timeout(500) # 等 onMounted 读取会话并渲染工作台(落地页为工作台) page.locator(".navitem", has_text="博客管理").click() page.wait_for_selector(".pageheading h1:has-text('博客管理')") pub_card = page.locator(".content-card", has_text=PUB_TITLE) pub_card.wait_for() check("刷新后文章与已发布状态保留", "已发布" in pub_card.inner_text()) page.locator(".content-card", has_text=PUB_TITLE).first.wait_for() page.wait_for_timeout(800) # 等 onMounted 读取会话、切换到博客页后再取列表 titles = page.locator(".content-card h3").all_inner_texts() check("刷新后该文章仍在", PUB_TITLE in titles, str(titles)) # 4. 状态筛选 page.locator(".tabs .tab", has_text="已发布").click() titles = page.locator(".content-card h3").all_inner_texts() check("已发布筛选只含已发布文章", PUB_TITLE in titles and DRAFT_TITLE not in titles, str(titles)) badges = page.locator(".content-card > .flex.between > .tag").all_inner_texts() check("已发布筛选下徽标一致", badges and all("已发布" in b for b in badges), str(badges)) page.locator(".tabs .tab", has_text="草稿").click() titles = page.locator(".content-card h3").all_inner_texts() check("草稿筛选排除已发布文章", PUB_TITLE not in titles, str(titles)) badges = page.locator(".content-card > .flex.between > .tag").all_inner_texts() check("草稿筛选下徽标一致", all("草稿" in b for b in badges), str(badges)) page.locator(".tabs .tab", has_text="全部").click() page.locator(".tabs .tab", has_text="全部").click() check("全部筛选包含已发布文章", PUB_TITLE in page.locator(".content-card h3").all_inner_texts()) # 5. 搜索过滤 page.fill('input[placeholder="搜索当前内容…"]', PUB_TITLE) titles = page.locator(".content-card h3").all_inner_texts() check("页内搜索只匹配标题", titles == [PUB_TITLE], str(titles)) page.fill('input[placeholder="搜索当前内容…"]', "列表项") titles = page.locator(".content-card h3").all_inner_texts() check("页内搜索覆盖正文", PUB_TITLE in titles, str(titles)) page.fill('input[placeholder="搜索当前内容…"]', "不存在的关键词zzz") check("无匹配显示空态", page.locator(".empty", has_text="这里还没有匹配的文章").count() == 1) page.fill('input[placeholder="搜索当前内容…"]', "") # 6. 阅读预览:Markdown 渲染与 XSS 清理 page.locator(".content-card", has_text=PUB_TITLE).get_by_role("button", name=re.compile("阅读全文")).click() preview = page.locator(".el-dialog:has-text('阅读文章')") md = preview.locator("article.markdown") try: page.wait_for_function("document.querySelectorAll('.el-dialog article.markdown h2').length > 0", timeout=10000) except Exception: print("DEBUG errors", errors[-5:]) for d in page.locator(".el-dialog").all(): print("DEBUG dialog:", d.get_attribute("class"), "|", d.inner_text()[:160].replace("\n", " / ")) print("DEBUG markdown html:", page.evaluate("document.querySelectorAll('.el-dialog article.markdown').length"), page.evaluate("(document.querySelector('.el-dialog article.markdown')||{}).innerHTML")) print("DEBUG dialogs", page.locator(".el-dialog").count()) raise check("预览显示标题", preview.locator(".preview-title").inner_text() == PUB_TITLE) check("预览显示状态与摘要", "已发布 · 本地状态" in preview.locator(".preview-meta").inner_text() and "这是一条**验收**摘要" in preview.locator(".preview-head .subtitle").inner_text()) check("预览渲染 Markdown 标题", md.locator("h2:text-is('小节')").count() == 1) check("预览正文不带 id(避免与对话框标题重复)", md.locator("[id]").count() == 0) check("预览渲染列表/引用/行内代码", md.locator("ul li").count() == 1 and md.locator("blockquote").count() == 1 and md.locator("code").count() >= 1) check("预览显示正文(含原始 HTML 文本)", "代码" in md.inner_text()) print("DEBUG img", md.locator("img").count(), "script", md.locator("script").count(), [el.evaluate("e => e.outerHTML.slice(0,120)") for el in md.locator("script").all()]) check("预览清理 XSS(文章区域无 img / script)", md.locator("img").count() == 0 and md.locator("script").count() == 0) check("预览中无脚本/事件属性", md.locator("[onerror]").count() == 0) page.screenshot(path=OUT + "blog-preview.png") preview.locator(".el-dialog__headerbtn").click() # 7. 全局搜索覆盖文章并打开预览 page.get_by_role("button", name=re.compile("⌕ 搜索笔记")).click() page.fill('textarea, input[aria-label="全局搜索关键词"]', PUB_TITLE) page.locator(".el-dialog").get_by_role("button", name="搜索").click() row = page.locator(".el-dialog .note-row", has_text=PUB_TITLE) row.wait_for() check("全局搜索命中文章", "文章" in row.inner_text()) row.click() check("点击搜索结果打开文章预览", page.locator(".el-dialog:has-text('阅读文章')").count() == 1) # 8. 删除(原型无删除,按任务要求提供的删除) preview = page.locator(".el-dialog:has-text('阅读文章')") preview.get_by_role("button", name="删除文章").click() page.locator(".el-message-box").get_by_role("button", name="确定删除").click() page.wait_for_timeout(900) check("删除后列表移除文章", page.locator(".content-card", has_text=PUB_TITLE).count() == 0) page.reload() page.wait_for_selector(".sidebar") page.wait_for_timeout(500) page.locator(".navitem", has_text="博客管理").click() page.wait_for_selector(".pageheading h1:has-text('博客管理')") page.wait_for_timeout(600) check("删除在刷新后保持", page.locator(".content-card", has_text=PUB_TITLE).count() == 0) # 9. 清理:删除本次验收创建的文章(只有被验收脚本重命名为 PUB_TITLE 的那篇) check("验收数据清理完成", page.locator(".content-card", has_text=TAG).count() == 0) # 10. 手机宽度无横向溢出 page.set_viewport_size({"width": 390, "height": 844}) page.wait_for_timeout(300) check("390px 无整页横向溢出", page.evaluate("document.documentElement.scrollWidth <= window.innerWidth"), str(page.evaluate("[document.documentElement.scrollWidth, window.innerWidth]"))) page.screenshot(path=OUT + "blog-mobile.png", full_page=True) check("无 JavaScript 运行时错误", not [e for e in errors if "401" not in e], str(errors[:3])) browser.close() failed = [r for r in results if not r[1]] print(json.dumps({"total": len(results), "failed": len(failed), "failures": [r[0] + " " + r[2] for r in failed]}, ensure_ascii=False)) sys.exit(1 if failed else 0)