65 lines
1.8 KiB
JavaScript
65 lines
1.8 KiB
JavaScript
// 测试移动端各导航页面
|
|
const wsUrl = process.argv[2] || "ws://127.0.0.1:9222/devtools/page/4";
|
|
|
|
const ws = new WebSocket(wsUrl);
|
|
let id = 0;
|
|
const pending = new Map();
|
|
|
|
function send(method, params = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const msgId = ++id;
|
|
pending.set(msgId, { resolve, reject });
|
|
ws.send(JSON.stringify({ id: msgId, method, params }));
|
|
});
|
|
}
|
|
|
|
ws.onmessage = (event) => {
|
|
const msg = JSON.parse(event.data);
|
|
if (msg.id && pending.has(msg.id)) {
|
|
const { resolve, reject } = pending.get(msg.id);
|
|
pending.delete(msg.id);
|
|
if (msg.error) reject(new Error(JSON.stringify(msg.error)));
|
|
else resolve(msg.result);
|
|
}
|
|
};
|
|
|
|
ws.onerror = (e) => {
|
|
console.error("WS error:", e.message || e);
|
|
process.exit(1);
|
|
};
|
|
|
|
async function evalJS(expr) {
|
|
const res = await send("Runtime.evaluate", { expression: expr, returnByValue: true });
|
|
return res.result.value;
|
|
}
|
|
|
|
async function navigate(path) {
|
|
await send("Page.navigate", { url: "http://192.168.5.7" + path });
|
|
await new Promise(r => setTimeout(r, 3000));
|
|
const state = await evalJS(`JSON.stringify({
|
|
url: location.href,
|
|
title: document.title,
|
|
bodyText: document.body.innerText.slice(0, 200),
|
|
overflowX: document.documentElement.scrollWidth > document.documentElement.clientWidth,
|
|
viewport: window.innerWidth + 'x' + window.innerHeight
|
|
})`);
|
|
console.log(`\n=== ${path} ===`);
|
|
console.log(state);
|
|
}
|
|
|
|
ws.onopen = async () => {
|
|
try {
|
|
await send("Page.enable");
|
|
await navigate("/");
|
|
await navigate("/utility");
|
|
await navigate("/learn");
|
|
await navigate("/article");
|
|
await navigate("/api");
|
|
await navigate("/profile");
|
|
} catch (e) {
|
|
console.error("Error:", e.message);
|
|
} finally {
|
|
ws.close();
|
|
process.exit(0);
|
|
}
|
|
}; |