#!/usr/bin/env node /** * Ask the real page what is actually painted at a place, instead of trusting * the DOM order. * * node tools/web/probe-ui-paint.mjs --port 5186 --url "/?ui=web" \ * [--blend ] [--workspace Rendering] [--selector ''] [--point x,y] [--json] * * For each selector it prints the element's box, which elements are on top of * its centre and of its top strip, and the nearest scroll containers. That is * enough to tell "the component did not render" from "the component rendered * but something absolutely positioned is covering it". * * Written after a capture showed a toolbar box at y 58–83 while the quick * toolbar's buttons were painted there: the toolbar existed and was invisible. */ import path from "node:path"; import {parseArgs, waitForServer, launchBrowser, newPage, openFile, waitScene} from "./course/lib/lesson-recorder.mjs"; // parseArgs only knows the course runner's flags, so tool-specific options are // read here rather than ignored. const argv = process.argv.slice(2); const opt = (name, fallback) => { const index = argv.indexOf(`--${name}`); return index >= 0 && argv[index + 1] ? argv[index + 1] : fallback; }; const args = parseArgs(); const BASE = `http://127.0.0.1:${args.port}`; const URL_PATH = opt("url", "/?ui=web"); const BLEND = opt("blend", null); const WORKSPACE = opt("workspace", ""); const SELECTORS = opt("selector", '[data-testid="render-result-view"]').split(",").map((item) => item.trim()).filter(Boolean); const POINTS = opt("point", "").split(";").map((item) => item.trim()).filter(Boolean); // Selectors clicked in order before measuring, so the page can be driven into // the state under test (open a panel, start a render) without a bespoke script. const CLICKS = opt("click", "").split(";").map((item) => item.trim()).filter(Boolean); await waitForServer(`${BASE}/`); const browser = await launchBrowser(args.gpu ?? "hardware"); const {context} = await newPage(browser, {baseUrl: null}); const page = context.pages()[0] ?? await context.newPage(); await page.goto(`${BASE}${URL_PATH}`, {waitUntil: "domcontentloaded"}); await page.locator("main.blender-app").waitFor({timeout: 180_000}); if (BLEND && BLEND !== "-") { await openFile(page, path.resolve(BLEND)); await waitScene(page, {objects: Number(args.objects ?? 7), timeout: 180_000}); } if (WORKSPACE) { await page.waitForFunction(() => [...document.querySelectorAll(".workspace-tab")].some((tab) => tab.getAttribute("aria-current") !== null), null, {timeout: 90_000}); await page.evaluate((name) => { const tab = [...document.querySelectorAll(".workspace-tab")].find((element) => element.textContent === name); if (tab instanceof HTMLElement) tab.click(); }, WORKSPACE); await page.waitForTimeout(1500); } for (const selector of CLICKS) { const target = page.locator(selector).first(); await target.waitFor({timeout: 120_000}); await target.click({force: true}); await page.waitForTimeout(Number(opt("settle", 1500))); } const result = await page.evaluate(({selectors, points}) => { const describe = (element) => { if (!element) return null; const style = getComputedStyle(element); return {tag: element.tagName, className: typeof element.className === "string" ? element.className : "", id: element.id || undefined, text: (element.textContent ?? "").replace(/\s+/g, " ").trim().slice(0, 40)}; }; const box = (element) => { const rect = element.getBoundingClientRect(); return {x: Math.round(rect.x), y: Math.round(rect.y), width: Math.round(rect.width), height: Math.round(rect.height), top: Math.round(rect.top), bottom: Math.round(rect.bottom)}; }; const scrollChain = (element) => { const chain = []; for (let node = element.parentElement; node && node !== document.documentElement; node = node.parentElement) { const style = getComputedStyle(node); if (style.overflowY === "auto" || style.overflowY === "scroll") chain.push({...describe(node), scrollTop: node.scrollTop, clientHeight: node.clientHeight, scrollHeight: node.scrollHeight}); } return chain; }; const report = {selectors: {}, points: {}}; for (const selector of selectors) { const element = document.querySelector(selector); if (!element) { report.selectors[selector] = {missing: true}; continue; } const rect = box(element); const centre = document.elementsFromPoint(rect.x + rect.width / 2, Math.min(rect.y + rect.height / 2, window.innerHeight - 2)); const strip = document.elementsFromPoint(rect.x + Math.min(rect.width / 2, 300), Math.min(rect.y + 4, window.innerHeight - 2)); report.selectors[selector] = { box: rect, coveredAtCentre: !centre.includes(element), coveredAtTop: !strip.includes(element), onTopAtCentre: centre.slice(0, 4).map(describe), onTopAtTop: strip.slice(0, 4).map(describe), scrollChain: scrollChain(element), }; } for (const point of points) { const [x, y] = point.split(",").map(Number); report.points[point] = document.elementsFromPoint(x, y).slice(0, 5).map(describe); } return report; }, {selectors: SELECTORS, points: POINTS}); if (args.json) console.log(JSON.stringify(result, null, 2)); else { for (const [selector, info] of Object.entries(result.selectors)) { console.log(selector); if (info.missing) { console.log(" missing"); continue; } console.log(` box x${info.box.x} y${info.box.y} ${info.box.width}×${info.box.height}`); console.log(` covered at top: ${info.coveredAtTop}${info.coveredAtTop ? ` by ${info.onTopAtTop.map((item) => `${item.tag}.${item.className.split(" ")[0] ?? ""}`).join(" > ")}` : ""}`); console.log(` covered at centre: ${info.coveredAtCentre}`); for (const scroller of info.scrollChain) console.log(` scroller ${scroller.tag}.${scroller.className.split(" ")[0] ?? ""} scrollTop=${scroller.scrollTop} ${scroller.clientHeight}/${scroller.scrollHeight}`); } for (const [point, stack] of Object.entries(result.points)) console.log(`${point}: ${stack.map((item) => `${item.tag}.${item.className.split(" ")[0] ?? ""}`).join(" > ")}`); } await context.close(); await browser.close();