#!/usr/bin/env node // Capture one screenshot per workbench from the running Web FreeCAD shell. // // Reuses tools/fcweb/capture.mjs for browser launch and screenshots. The // inventory decides what exists and what status each workbench has; this tool // only drives the shell and records what it observed. // // node tools/fcweb/ui-capture.mjs --all // node tools/fcweb/ui-capture.mjs --only Part,Fem --headed import fs from "node:fs"; import path from "node:path"; import { createHash } from "node:crypto"; import { fileURLToPath } from "node:url"; import { launchBrowser, openPage, screenshot } from "./capture.mjs"; const here = path.dirname(fileURLToPath(import.meta.url)); const args = process.argv.slice(2); const argValue = (name, fallback) => { const index = args.indexOf(name); return index >= 0 && args[index + 1] ? args[index + 1] : fallback; }; const url = argValue("--url", "http://127.0.0.1:5182/"); const inventoryPath = path.resolve(argValue("--inventory", path.join(here, "out", "ui-inventory.json"))); const outDir = path.resolve(argValue("--out", path.join(here, "out", "ui-shots"))); const reportPath = path.resolve(argValue("--report", path.join(here, "out", "ui-capture-report.json"))); const only = argValue("--only", ""); const headed = args.includes("--headed"); const timeoutMs = Number(argValue("--timeout", "120000")); const [width, height] = argValue("--viewport", "1600x1000").split("x").map(Number); if (!fs.existsSync(inventoryPath)) { console.error(`[ui-capture] missing inventory: ${inventoryPath}`); process.exit(2); } const inventory = JSON.parse(fs.readFileSync(inventoryPath, "utf8")); const wanted = only ? new Set(only.split(",").map(item => item.trim()).filter(Boolean)) : null; const capturable = inventory.chapters.filter(chapter => (chapter.status === "renders" || chapter.status === "fallback") && (!wanted || wanted.has(chapter.id))); const skipped = inventory.chapters .filter(chapter => !capturable.includes(chapter)) .map(chapter => ({ id: chapter.id, status: chapter.status, reason: chapter.reason ?? null })); fs.mkdirSync(outDir, { recursive: true }); const browser = await launchBrowser({ headed }); const results = []; let baselineSha = null; try { for (const chapter of capturable) { const started = Date.now(); const id = chapter.id; const { page, errors, failedRequests } = await openPage(browser, url, { viewport: { width, height } }); const record = { id, status: "failed", ms: 0, file: null, bytes: 0, sha256: null, toolbarCrop: null, iframe: false, domToolbarButtons: 0, pageErrors: [], failedRequests: [], warnings: [], }; try { await page.waitForSelector('select[aria-label="Workbench"]', { state: "attached", timeout: 60000 }); // The Start overlay covers the shell on a cold load; Escape closes it. await page.keyboard.press("Escape").catch(() => {}); await page.waitForTimeout(400); const select = page.locator('select[aria-label="Workbench"]'); if (await select.isDisabled().catch(() => false)) { // A task panel disables the switcher; close whatever is open. await page.keyboard.press("Escape").catch(() => {}); await page.waitForTimeout(400); } await select.selectOption(id, { timeout: 30000 }); await page.waitForFunction( expected => document.querySelector('select[aria-label="Workbench"]')?.value === expected, id, { timeout: 15000 }, ); // Wait for whatever this workbench actually shows. const toolbars = `[aria-label="${id} toolbars"]`; const frame = `iframe[title="${id} workbench"]`; const deadline = Date.now() + timeoutMs; let sawSomething = false; while (Date.now() < deadline) { if (await page.locator(toolbars).count() > 0) { sawSomething = true; break; } if (await page.locator(frame).count() > 0) { record.iframe = true; sawSomething = true; break; } // Part is special-cased (no parity toolbars); fall back to its dock. if (await page.locator('[data-toolbar-name="Solids"]').count() > 0) { sawSomething = true; break; } await page.waitForTimeout(500); } if (!sawSomething) record.warnings.push("no toolbar dock or iframe appeared; screenshot taken anyway"); await page.waitForTimeout(1800); const file = path.join(outDir, `${id}.png`); await screenshot(page, { file }); const bytes = fs.readFileSync(file); record.bytes = bytes.length; record.sha256 = createHash("sha256").update(bytes).digest("hex"); record.file = file; record.domToolbarButtons = await page.evaluate(() => document.querySelectorAll('[aria-label$=" toolbars"] div[role="toolbar"] button, [data-toolbar-name] button').length ).catch(() => 0); if (await page.locator(toolbars).count() > 0) { const crop = path.join(outDir, `${id}-toolbars.png`); try { await screenshot(page, { file: crop, locator: toolbars }); record.toolbarCrop = crop; } catch { record.warnings.push("toolbar crop failed"); } } record.pageErrors = errors.slice(0, 5); record.failedRequests = failedRequests.slice(0, 5); if (record.bytes < 20000) record.warnings.push(`screenshot is only ${record.bytes} bytes`); if (baselineSha === null) baselineSha = record.sha256; else if (id !== "Architecture" && record.sha256 === baselineSha) { record.warnings.push("screenshot is byte-identical to the first capture (possible blank page)"); } record.status = record.pageErrors.length === 0 ? "ok" : "page-errors"; } catch (error) { record.status = "failed"; record.warnings.push(String(error?.message ?? error).slice(0, 300)); record.pageErrors = errors.slice(0, 5); } finally { record.ms = Date.now() - started; results.push(record); console.log(`${record.status === "ok" ? "OK " : "FAIL"} ${id.padEnd(20)} ${String(record.ms).padStart(7)}ms ${record.bytes}B ${record.warnings[0] ?? ""}`); await page.close().catch(() => {}); } } } finally { await browser.close(); } const summary = { schema: "freecad-web/ui-capture/v1", url, viewport: { width, height }, captured: results.length, ok: results.filter(item => item.status === "ok").length, failed: results.filter(item => item.status !== "ok").length, skipped, results, }; fs.mkdirSync(path.dirname(reportPath), { recursive: true }); fs.writeFileSync(reportPath, `${JSON.stringify(summary, null, 2)}\n`); console.log(JSON.stringify({ ok: summary.failed === 0, captured: summary.captured, failed: summary.failed, report: reportPath })); process.exit(summary.failed === 0 ? 0 : 1);