feat(web): panel scroll contract, layout probe and functional suite
- lock the three-column height chain on workbench/PartDesign pages so each side panel scrolls independently and the centre stage keeps its size - dock module task panels above the layout with their own scrollbar instead of letting them push the stage out of the viewport - add tools/layout-probe.mjs (page scroll, aside bounds, pane scrolling, stage stability) and CLI mode for tools/static-host.mjs with relative --root support - add the P0-P4 browser functional suite, manual scenarios and capture tooling; document it under docs/current/web-functional-suite Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
186
web/tools/manual/lib/actions.mjs
Normal file
186
web/tools/manual/lib/actions.mjs
Normal file
@@ -0,0 +1,186 @@
|
||||
// Scenario action interpreter shared by capture.mjs (screenshot capture) and the
|
||||
// headed functional suite (assertions). Behaviour is identical to the switch it
|
||||
// was extracted from; everything environment-specific arrives through options.
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { listOpfsFiles, readOpfsFile, seedOpfsFromBytes } from "../../carrier-course/lib/opfs.mjs";
|
||||
|
||||
const sha256 = file => createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
||||
|
||||
export function createActionRunner(page, {
|
||||
origin = "",
|
||||
shotDir,
|
||||
shotPrefix = "",
|
||||
fixturePath = null,
|
||||
record = () => {},
|
||||
shotIndex = [],
|
||||
} = {}) {
|
||||
const click = async action => {
|
||||
let locator;
|
||||
if (action.role) locator = page.getByRole(action.role, { name: action.name, exact: action.exact });
|
||||
else {
|
||||
locator = page.locator(action.selector);
|
||||
if (action.hasText) locator = locator.filter({ hasText: action.hasText });
|
||||
if (action.target) locator = locator.locator(action.target);
|
||||
}
|
||||
if (action.nth !== undefined) locator = locator.nth(action.nth);
|
||||
else if (!action.all) locator = locator.first();
|
||||
if (action.optional && (await locator.count()) === 0) return;
|
||||
if (action.all) {
|
||||
const count = await locator.count();
|
||||
for (let index = 0; index < count; index++) await locator.nth(index).click();
|
||||
} else await locator.click({ timeout: action.timeout ?? 30000 });
|
||||
};
|
||||
const shot = async action => {
|
||||
if (!shotDir) throw new Error("shot action needs a shotDir");
|
||||
const file = path.join(shotDir, `${action.name}.png`);
|
||||
fs.mkdirSync(shotDir, { recursive: true });
|
||||
if (action.selector) {
|
||||
const target = page.locator(action.selector).first();
|
||||
await target.waitFor({ state: "visible", timeout: 30000 });
|
||||
try {
|
||||
await target.screenshot({ path: file, timeout: 20000 });
|
||||
} catch {
|
||||
// Element screenshots scroll the element into view and can time out on
|
||||
// containers taller than the viewport; evidence capture must not turn
|
||||
// a healthy page state into a failure, so fall back to the viewport.
|
||||
await page.screenshot({ path: file });
|
||||
record({ action: "shot", name: action.name, fallback: "viewport" });
|
||||
}
|
||||
} else {
|
||||
await page.screenshot({ path: file, fullPage: Boolean(action.fullPage) });
|
||||
}
|
||||
const stat = fs.statSync(file);
|
||||
if (stat.size < 1024) throw new Error(`screenshot too small: ${file}`);
|
||||
shotIndex.push({ id: `${shotPrefix}${action.name}`, file,
|
||||
bytes: stat.size, sha256: sha256(file), fullPage: Boolean(action.fullPage), selector: action.selector ?? null });
|
||||
record({ action: "shot", name: action.name, bytes: stat.size });
|
||||
return file;
|
||||
};
|
||||
const save = async action => {
|
||||
await page.evaluate(() => { const details = document.querySelector(".file-tools"); if (details) details.open = true; });
|
||||
await page.waitForTimeout(400);
|
||||
const before = await listOpfsFiles(page);
|
||||
await page.locator('button:has-text("保存 FCStd 到 OPFS")').first().click();
|
||||
let after = before; let status = "";
|
||||
for (let attempt = 0; attempt < 40; attempt++) {
|
||||
await page.waitForTimeout(750);
|
||||
after = await listOpfsFiles(page);
|
||||
status = await page.locator("p.status").first().textContent().catch(() => "");
|
||||
if (after.some(name => !before.includes(name) && /\.FCStd$/i.test(name)) || /Saved\s+\S+\.FCStd\s+to OPFS/i.test(status ?? "")) break;
|
||||
}
|
||||
const match = /Saved\s+(\S+\.FCStd)\s+to OPFS/i.exec(status ?? "");
|
||||
const name = match?.[1] ?? after.find(item => !before.includes(item) && /\.FCStd$/i.test(item));
|
||||
if (!name) throw new Error(`save produced no FCStd; status=${status}`);
|
||||
const bytes = (await readOpfsFile(page, name)).bytes;
|
||||
if (fixturePath) {
|
||||
fs.mkdirSync(path.dirname(fixturePath), { recursive: true });
|
||||
fs.writeFileSync(fixturePath, Buffer.from(bytes));
|
||||
}
|
||||
record({ action: "save", name, bytes: bytes.length, status });
|
||||
return { name, bytes: Buffer.from(bytes) };
|
||||
};
|
||||
|
||||
const run = async action => {
|
||||
switch (action.type) {
|
||||
case "goto":
|
||||
await page.goto(`${origin}${action.url}`, { waitUntil: "domcontentloaded", timeout: 120000 });
|
||||
record({ action: "goto", url: action.url });
|
||||
break;
|
||||
case "waitFor":
|
||||
if (action.selector) await page.waitForSelector(action.selector, { timeout: action.timeout ?? 120000 });
|
||||
if (action.text) await page.getByText(action.text, { exact: action.exact ?? false }).first()
|
||||
.waitFor({ state: "visible", timeout: action.timeout ?? 120000 });
|
||||
record({ action: "waitFor", selector: action.selector ?? null, text: action.text ?? null });
|
||||
break;
|
||||
case "click": await click(action); record({ action: "click", selector: action.selector ?? action.name }); break;
|
||||
case "fill":
|
||||
await page.locator(action.selector).first().fill(String(action.value));
|
||||
record({ action: "fill", selector: action.selector, value: action.value });
|
||||
break;
|
||||
case "select":
|
||||
await page.locator(action.selector).first().selectOption(action.label ? { label: action.label } : action.value);
|
||||
record({ action: "select", selector: action.selector, label: action.label ?? action.value });
|
||||
break;
|
||||
case "selectIndex":
|
||||
await page.locator(action.selector).first().selectOption({ index: action.index });
|
||||
record({ action: "selectIndex", selector: action.selector, index: action.index });
|
||||
break;
|
||||
case "press": await page.keyboard.press(action.key); record({ action: "press", key: action.key }); break;
|
||||
case "clickCanvas": {
|
||||
const target = page.locator(action.selector).first();
|
||||
await target.scrollIntoViewIfNeeded();
|
||||
await page.waitForTimeout(200);
|
||||
const box = await target.boundingBox();
|
||||
if (!box) throw new Error(`canvas missing: ${action.selector}`);
|
||||
await page.mouse.click(box.x + box.width * action.fx, box.y + box.height * action.fy);
|
||||
await page.waitForTimeout(action.wait ?? 250);
|
||||
record({ action: "clickCanvas", selector: action.selector, fx: action.fx, fy: action.fy });
|
||||
break;
|
||||
}
|
||||
case "assertAnyText": {
|
||||
const texts = await page.locator(action.selector).allTextContents();
|
||||
if (!texts.some(text => String(text).includes(action.contains)))
|
||||
throw new Error(`assertAnyText failed: ${action.contains} in ${JSON.stringify(texts.slice(0, 5))}`);
|
||||
record({ action: "assertAnyText", selector: action.selector, contains: action.contains });
|
||||
break;
|
||||
}
|
||||
case "drag": {
|
||||
const box = await page.locator(action.selector).first().boundingBox();
|
||||
if (!box) throw new Error(`drag target missing: ${action.selector}`);
|
||||
const x = box.x + box.width / 2; const y = box.y + box.height / 2;
|
||||
await page.mouse.move(x, y); await page.mouse.down();
|
||||
await page.mouse.move(x + (action.dx ?? 0), y + (action.dy ?? 0), { steps: 18 });
|
||||
await page.mouse.up(); await page.waitForTimeout(400);
|
||||
record({ action: "drag", selector: action.selector, dx: action.dx, dy: action.dy });
|
||||
break;
|
||||
}
|
||||
case "eval":
|
||||
await page.evaluate(action.script);
|
||||
record({ action: "eval" });
|
||||
break;
|
||||
case "seed": {
|
||||
const file = path.resolve(action.file);
|
||||
if (!fs.existsSync(file)) throw new Error(`seed fixture missing: ${file}`);
|
||||
await seedOpfsFromBytes(page, action.name ?? path.basename(file), fs.readFileSync(file));
|
||||
record({ action: "seed", file, name: action.name ?? path.basename(file) });
|
||||
break;
|
||||
}
|
||||
case "shot": await shot(action); break;
|
||||
case "save": await save(action); break;
|
||||
case "assertText": {
|
||||
const text = await page.locator(action.selector).first().textContent();
|
||||
if (!String(text ?? "").includes(action.contains)) throw new Error(`assertText failed: ${action.selector} ${text}`);
|
||||
record({ action: "assertText", selector: action.selector, contains: action.contains });
|
||||
break;
|
||||
}
|
||||
case "assertVisible":
|
||||
await page.locator(action.selector).first().waitFor({ state: "visible", timeout: 30000 });
|
||||
record({ action: "assertVisible", selector: action.selector });
|
||||
break;
|
||||
case "waitOptions": {
|
||||
const locator = page.locator(action.selector).first();
|
||||
let count = 0;
|
||||
for (let attempt = 0; attempt < 80; attempt++) {
|
||||
count = await locator.locator("option").count();
|
||||
if (count >= (action.min ?? 1)) break;
|
||||
await page.waitForTimeout(500);
|
||||
}
|
||||
if (count < (action.min ?? 1)) throw new Error(`waitOptions failed: ${action.selector} ${count}`);
|
||||
record({ action: "waitOptions", selector: action.selector, count });
|
||||
break;
|
||||
}
|
||||
case "assertValue": {
|
||||
const value = await page.locator(action.selector).first().inputValue();
|
||||
if (String(value) !== String(action.value)) throw new Error(`assertValue failed: ${action.selector} ${value} != ${action.value}`);
|
||||
record({ action: "assertValue", selector: action.selector, value });
|
||||
break;
|
||||
}
|
||||
case "wait": await page.waitForTimeout(action.ms ?? 500); break;
|
||||
default: throw new Error(`unknown action ${action.type}`);
|
||||
}
|
||||
};
|
||||
|
||||
return { click, shot, save, run, shotIndex };
|
||||
}
|
||||
Reference in New Issue
Block a user