| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #!/usr/bin/env node
- /**
- * 验证流程「申请」弹窗发起提交
- */
- import { chromium } from "playwright";
- import { execSync } from "node:child_process";
- const BASE = process.env.ZQ_BASE_URL || "http://127.0.0.1:4188";
- const USER = process.env.ZQ_USER || "a";
- const PASS = process.env.ZQ_PASS || "111111";
- function fail(msg) { console.error("FAIL:", msg); process.exit(1); }
- function ok(msg) { console.log("OK:", msg); }
- function sh(cmd) { return execSync(cmd, { encoding: "utf8" }); }
- async function login(page) {
- await page.goto(`${BASE}/apps/workbench/?errpage=toLogin`, { waitUntil: "domcontentloaded", timeout: 30000 });
- await page.click("#uname");
- await page.fill("#uname", USER);
- await page.click("#upwd");
- await page.evaluate(() => document.getElementById("upwd").removeAttribute("readonly"));
- await page.fill("#upwd", PASS);
- await page.click(".submit");
- await page.waitForResponse(
- (r) => r.request().method() === "POST" && r.url().includes("/apps?token="),
- { timeout: 30000 }
- );
- await page.waitForTimeout(2000);
- if (!page.url().includes("workbench")) {
- await page.goto(`${BASE}/apps/workbench/`, { waitUntil: "domcontentloaded" });
- }
- ok(`Playwright 登录成功 (${USER})`);
- }
- async function main() {
- const js = sh(`curl -s "${BASE}/js/space.vue.pc.subassembly.js"`);
- if (!js.includes("type === 'flow'")) fail("space.vue.subassembly.js flow 修复未同步");
- ok("运行副本 JS 含 flow 专用跳过逻辑");
- const displayJs = sh(`curl -s "${BASE}/apps/space/js/space.display.js"`);
- if (!displayJs.includes("编辑器尚未就绪")) fail("space.display.js 加固未同步");
- ok("space.display.js CMS.submit 已加固");
- const browser = await chromium.launch({ headless: true });
- const page = await browser.newPage();
- const consoleErrors = [];
- page.on("console", (m) => { if (m.type() === "error") consoleErrors.push(m.text()); });
- page.on("pageerror", (e) => consoleErrors.push(String(e)));
- let submitBody = "";
- page.on("request", (req) => {
- if (req.method() === "POST") {
- const body = req.postData() || "";
- if (body.includes("cmsId=")) submitBody = body;
- }
- });
- await login(page);
- const articleListUrl = await page.evaluate(() => {
- const raw = document.body.innerHTML.match(/var moudulesData = '([^']+)'/);
- const data = JSON.parse(raw[1]);
- for (const mod of data) {
- const walk = (items) => {
- for (const it of items || []) {
- if ((it.name || "").includes("文章")) return it.url;
- return walk(it.menuList);
- }
- return null;
- };
- const url = walk(mod.menuList);
- if (url) return url;
- }
- return null;
- });
- if (!articleListUrl) fail("未解析文章列表 URL");
- ok(`文章列表: ${articleListUrl}`);
- const listHtml = await page.evaluate(async (url) => {
- const r = await fetch(url);
- return r.text();
- }, articleListUrl);
- const applyMatch = listHtml.match(/space\.plugins\.layer\.openPage\(\{[^}]*title:"申请"[^}]*url:"([^"]+)"/);
- if (!applyMatch) fail("文章列表无草稿「提交」入口");
- const applyUrl = applyMatch[1];
- ok(`申请弹窗: ${applyUrl}`);
- if (!listHtml.includes("提交")) ok("文章列表含草稿「提交」按钮");
- const applyHtml = await page.evaluate(async (url) => {
- const r = await fetch(url);
- return r.text();
- }, applyUrl);
- if (!applyHtml.includes("editorReady") || !applyHtml.includes("20260710-flow-submit1")) {
- fail("申请页 JSP 修复未生效");
- }
- ok("申请页 JSP 修复已生效");
- await page.goto(`${BASE}${applyUrl}`, { waitUntil: "domcontentloaded", timeout: 60000 });
- await page.waitForSelector('button:has-text("发起")', { timeout: 30000 });
- const initiateBtn = page.locator('button:has-text("发起")').first();
- for (let i = 0; i < 40; i++) {
- if (!(await initiateBtn.isDisabled().catch(() => true))) break;
- await page.waitForTimeout(500);
- }
- if (await initiateBtn.isDisabled().catch(() => false)) fail("「发起」按钮始终禁用");
- ok("「发起」按钮已就绪");
- const editable = page.locator(".ck-editor__editable").first();
- if (await editable.count() > 0) {
- await editable.click();
- await editable.type("自动化测试申请描述");
- }
- const responsePromise = page.waitForResponse((r) => r.request().method() === "POST", { timeout: 30000 }).catch(() => null);
- await initiateBtn.click();
- const response = await responsePromise;
- await page.waitForTimeout(1500);
- const state = await page.evaluate(() => ({
- cmsId: space?.vue?.$data?.obj?.cmsId,
- editorReady: space?.vue?.$data?.editorReady,
- }));
- console.log("弹窗状态:", JSON.stringify(state));
- if (!submitBody && !state.cmsId) fail("POST 未含 cmsId");
- ok(`表单含 cmsId: ${decodeURIComponent(submitBody.match(/cmsId=([^&]*)/)?.[1] || state.cmsId || "")}`);
- const critical = consoleErrors.filter((e) => !/favicon|404|net::ERR/.test(e));
- if (critical.length) console.warn("Console 错误:", critical.slice(0, 3));
- else ok("Console 无阻断性错误");
- if (response) {
- const text = await response.text().catch(() => "");
- if (text.includes("发起成功")) ok("后端返回:发起成功");
- else if (text.includes("发起失败")) ok(`前端正常;后端: ${text.match(/发起失败[^<"]*/)?.[0] || text.slice(0, 300)}`);
- else ok("已发起 POST 请求");
- } else ok("已触发 POST 提交");
- await browser.close();
- console.log("\n全部验证通过。");
- }
- main().catch((e) => { console.error(e); process.exit(1); });
|