91 lines
5.1 KiB
JavaScript
91 lines
5.1 KiB
JavaScript
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
const origin = process.argv[2] || "http://localhost:5173";
|
|
|
|
const connect = async () => {
|
|
const pages = await (await fetch("http://127.0.0.1:9222/json/list")).json();
|
|
const page = pages.find((item) => item.type === "page" && item.url.startsWith(`${origin}/`));
|
|
if (!page) throw new Error(`Chrome debugging has no ${origin} project page`);
|
|
const socket = new WebSocket(page.webSocketDebuggerUrl);
|
|
await new Promise((resolve, reject) => {
|
|
socket.addEventListener("open", resolve, { once: true });
|
|
socket.addEventListener("error", reject, { once: true });
|
|
});
|
|
let id = 0;
|
|
const pending = new Map();
|
|
socket.addEventListener("message", (event) => {
|
|
const message = JSON.parse(event.data);
|
|
const request = pending.get(message.id);
|
|
if (!request) return;
|
|
pending.delete(message.id);
|
|
message.error ? request.reject(new Error(message.error.message)) : request.resolve(message.result);
|
|
});
|
|
const send = (method, params = {}) => new Promise((resolve, reject) => {
|
|
id += 1;
|
|
pending.set(id, { resolve, reject });
|
|
socket.send(JSON.stringify({ id, method, params }));
|
|
});
|
|
return { socket, send };
|
|
};
|
|
|
|
const valueOf = async (send, expression) => (await send("Runtime.evaluate", { expression, returnByValue: true })).result?.value;
|
|
const waitFor = async (send, expression, message) => {
|
|
for (let index = 0; index < 60; index += 1) {
|
|
if (await valueOf(send, expression)) return;
|
|
await sleep(100);
|
|
}
|
|
throw new Error(message);
|
|
};
|
|
let auditId = 0;
|
|
const open = async (send, route, selector) => {
|
|
auditId += 1;
|
|
const url = `${origin}/?fBusiness=${auditId}#${route}`;
|
|
await send("Page.navigate", { url });
|
|
await waitFor(send, `location.href === ${JSON.stringify(url)}`, `navigation failed: ${route}`);
|
|
await waitFor(send, `Boolean(document.querySelector(${JSON.stringify(selector)}))`, `missing ${selector}: ${route}`);
|
|
};
|
|
const click = (send, selector) => valueOf(send, `document.querySelector(${JSON.stringify(selector)}).click()`);
|
|
const setInput = (send, selector, value) => valueOf(send, `(() => { const input = document.querySelector(${JSON.stringify(selector)}); input.value = ${JSON.stringify(value)}; input.dispatchEvent(new Event('input', { bubbles: true })); return input.value; })()`);
|
|
|
|
const run = async () => {
|
|
const { socket, send } = await connect();
|
|
try {
|
|
await send("Page.enable");
|
|
await send("Runtime.enable");
|
|
for (const size of [{ width: 320, height: 568 }, { width: 412, height: 915 }]) {
|
|
await send("Emulation.setDeviceMetricsOverride", { ...size, deviceScaleFactor: 1, mobile: true, screenWidth: size.width, screenHeight: size.height });
|
|
await open(send, "/pages/family/f04-article-list?count=50", ".article-card");
|
|
if ((await valueOf(send, "document.querySelectorAll('.article-card').length")) !== 50) throw new Error(`F04 did not render 50 articles at ${size.width}`);
|
|
if ((await valueOf(send, "document.documentElement.scrollWidth")) > size.width + 1) throw new Error(`F04 horizontal overflow at ${size.width}`);
|
|
await valueOf(send, "document.querySelector('.article-card:last-of-type').scrollIntoView()");
|
|
|
|
await open(send, "/pages/family/f07-album-list?count=30", ".album-card");
|
|
if ((await valueOf(send, "document.querySelectorAll('.album-card').length")) !== 30) throw new Error(`F07 did not render 30 albums at ${size.width}`);
|
|
if ((await valueOf(send, "document.documentElement.scrollWidth")) > size.width + 1) throw new Error(`F07 horizontal overflow at ${size.width}`);
|
|
}
|
|
|
|
await open(send, "/pages/family/f04-article-list", ".article-card");
|
|
await click(send, ".article-card");
|
|
await waitFor(send, "location.hash.includes('/pages/family/f05-article-detail?articleId=101')", "F04 card did not open ID-driven F05");
|
|
|
|
await open(send, "/pages/family/f03-feed-detail?feedId=1", ".feed-comment-form textarea");
|
|
const before = await valueOf(send, "document.querySelectorAll('.feed-comment-card').length");
|
|
await setInput(send, ".feed-comment-form textarea", "愿家人岁岁平安,常聚常新。");
|
|
await click(send, ".feed-comment-form .app-button");
|
|
await waitFor(send, `document.querySelectorAll('.feed-comment-card').length === ${before + 1}`, "F03 comment was not appended");
|
|
|
|
await open(send, "/pages/family/f07-album-list", ".album-list > .app-button");
|
|
await click(send, ".album-list > .app-button");
|
|
await waitFor(send, "Boolean(document.querySelector('.app-dialog-layer'))", "F07 create dialog did not open");
|
|
await setInput(send, ".album-dialog-field input", "清明祭祖影像");
|
|
await click(send, ".app-dialog__actions .app-button:last-child");
|
|
await waitFor(send, "document.querySelector('.album-card .album-card__copy').innerText.includes('清明祭祖影像')", "F07 created album was not added");
|
|
|
|
process.stdout.write("F-BUSINESS-FLOW-RUNTIME-SMOKE PASS\n");
|
|
} finally {
|
|
try { await send("Emulation.clearDeviceMetricsOverride"); } catch (_) {}
|
|
socket.close();
|
|
}
|
|
};
|
|
|
|
run().catch((error) => { process.stderr.write(`${error.stack || error.message}\n`); process.exit(1); });
|