verify-flow-submit.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env node
  2. /**
  3. * 验证流程「申请」弹窗发起提交
  4. */
  5. import { chromium } from "playwright";
  6. import { execSync } from "node:child_process";
  7. const BASE = process.env.ZQ_BASE_URL || "http://127.0.0.1:4188";
  8. const USER = process.env.ZQ_USER || "a";
  9. const PASS = process.env.ZQ_PASS || "111111";
  10. function fail(msg) { console.error("FAIL:", msg); process.exit(1); }
  11. function ok(msg) { console.log("OK:", msg); }
  12. function sh(cmd) { return execSync(cmd, { encoding: "utf8" }); }
  13. async function login(page) {
  14. await page.goto(`${BASE}/apps/workbench/?errpage=toLogin`, { waitUntil: "domcontentloaded", timeout: 30000 });
  15. await page.click("#uname");
  16. await page.fill("#uname", USER);
  17. await page.click("#upwd");
  18. await page.evaluate(() => document.getElementById("upwd").removeAttribute("readonly"));
  19. await page.fill("#upwd", PASS);
  20. await page.click(".submit");
  21. await page.waitForResponse(
  22. (r) => r.request().method() === "POST" && r.url().includes("/apps?token="),
  23. { timeout: 30000 }
  24. );
  25. await page.waitForTimeout(2000);
  26. if (!page.url().includes("workbench")) {
  27. await page.goto(`${BASE}/apps/workbench/`, { waitUntil: "domcontentloaded" });
  28. }
  29. ok(`Playwright 登录成功 (${USER})`);
  30. }
  31. async function main() {
  32. const js = sh(`curl -s "${BASE}/js/space.vue.pc.subassembly.js"`);
  33. if (!js.includes("type === 'flow'")) fail("space.vue.subassembly.js flow 修复未同步");
  34. ok("运行副本 JS 含 flow 专用跳过逻辑");
  35. const displayJs = sh(`curl -s "${BASE}/apps/space/js/space.display.js"`);
  36. if (!displayJs.includes("编辑器尚未就绪")) fail("space.display.js 加固未同步");
  37. ok("space.display.js CMS.submit 已加固");
  38. const browser = await chromium.launch({ headless: true });
  39. const page = await browser.newPage();
  40. const consoleErrors = [];
  41. page.on("console", (m) => { if (m.type() === "error") consoleErrors.push(m.text()); });
  42. page.on("pageerror", (e) => consoleErrors.push(String(e)));
  43. let submitBody = "";
  44. page.on("request", (req) => {
  45. if (req.method() === "POST") {
  46. const body = req.postData() || "";
  47. if (body.includes("cmsId=")) submitBody = body;
  48. }
  49. });
  50. await login(page);
  51. const articleListUrl = await page.evaluate(() => {
  52. const raw = document.body.innerHTML.match(/var moudulesData = '([^']+)'/);
  53. const data = JSON.parse(raw[1]);
  54. for (const mod of data) {
  55. const walk = (items) => {
  56. for (const it of items || []) {
  57. if ((it.name || "").includes("文章")) return it.url;
  58. return walk(it.menuList);
  59. }
  60. return null;
  61. };
  62. const url = walk(mod.menuList);
  63. if (url) return url;
  64. }
  65. return null;
  66. });
  67. if (!articleListUrl) fail("未解析文章列表 URL");
  68. ok(`文章列表: ${articleListUrl}`);
  69. const listHtml = await page.evaluate(async (url) => {
  70. const r = await fetch(url);
  71. return r.text();
  72. }, articleListUrl);
  73. const applyMatch = listHtml.match(/space\.plugins\.layer\.openPage\(\{[^}]*title:"申请"[^}]*url:"([^"]+)"/);
  74. if (!applyMatch) fail("文章列表无草稿「提交」入口");
  75. const applyUrl = applyMatch[1];
  76. ok(`申请弹窗: ${applyUrl}`);
  77. if (!listHtml.includes("提交")) ok("文章列表含草稿「提交」按钮");
  78. const applyHtml = await page.evaluate(async (url) => {
  79. const r = await fetch(url);
  80. return r.text();
  81. }, applyUrl);
  82. if (!applyHtml.includes("editorReady") || !applyHtml.includes("20260710-flow-submit1")) {
  83. fail("申请页 JSP 修复未生效");
  84. }
  85. ok("申请页 JSP 修复已生效");
  86. await page.goto(`${BASE}${applyUrl}`, { waitUntil: "domcontentloaded", timeout: 60000 });
  87. await page.waitForSelector('button:has-text("发起")', { timeout: 30000 });
  88. const initiateBtn = page.locator('button:has-text("发起")').first();
  89. for (let i = 0; i < 40; i++) {
  90. if (!(await initiateBtn.isDisabled().catch(() => true))) break;
  91. await page.waitForTimeout(500);
  92. }
  93. if (await initiateBtn.isDisabled().catch(() => false)) fail("「发起」按钮始终禁用");
  94. ok("「发起」按钮已就绪");
  95. const editable = page.locator(".ck-editor__editable").first();
  96. if (await editable.count() > 0) {
  97. await editable.click();
  98. await editable.type("自动化测试申请描述");
  99. }
  100. const responsePromise = page.waitForResponse((r) => r.request().method() === "POST", { timeout: 30000 }).catch(() => null);
  101. await initiateBtn.click();
  102. const response = await responsePromise;
  103. await page.waitForTimeout(1500);
  104. const state = await page.evaluate(() => ({
  105. cmsId: space?.vue?.$data?.obj?.cmsId,
  106. editorReady: space?.vue?.$data?.editorReady,
  107. }));
  108. console.log("弹窗状态:", JSON.stringify(state));
  109. if (!submitBody && !state.cmsId) fail("POST 未含 cmsId");
  110. ok(`表单含 cmsId: ${decodeURIComponent(submitBody.match(/cmsId=([^&]*)/)?.[1] || state.cmsId || "")}`);
  111. const critical = consoleErrors.filter((e) => !/favicon|404|net::ERR/.test(e));
  112. if (critical.length) console.warn("Console 错误:", critical.slice(0, 3));
  113. else ok("Console 无阻断性错误");
  114. if (response) {
  115. const text = await response.text().catch(() => "");
  116. if (text.includes("发起成功")) ok("后端返回:发起成功");
  117. else if (text.includes("发起失败")) ok(`前端正常;后端: ${text.match(/发起失败[^<"]*/)?.[0] || text.slice(0, 300)}`);
  118. else ok("已发起 POST 请求");
  119. } else ok("已触发 POST 提交");
  120. await browser.close();
  121. console.log("\n全部验证通过。");
  122. }
  123. main().catch((e) => { console.error(e); process.exit(1); });