// Chrome DevTools input, accessibility and capture helpers for the Pipelines // P1 fixture probe. import { writeFile } from "node:fs/promises"; import { join } from "node:path"; import { sleep } from "./pipelines_probe_cdp_lib.mjs"; const ENABLE_SEMANTICS = `(() => { const visit = (root) => { const placeholder = root.querySelector?.('flt-semantics-placeholder'); if (placeholder) { placeholder.click(); return true; } for (const element of root.querySelectorAll?.('*') ?? []) { if (element.shadowRoot && visit(element.shadowRoot)) return true; } return false; }; return visit(document); })()`; export async function enableSemantics(connection) { await connection.evaluate(ENABLE_SEMANTICS); await sleep(1000); } function axLabel(node) { return [node?.name?.value, node?.value?.value] .filter((value) => typeof value === "string" && value !== "") .join("\n"); } export async function axNodes(connection) { const { nodes } = await connection.send("Accessibility.getFullAXTree", { depth: -1, }); return nodes; } export async function waitFor(probe, timeout) { const deadline = Date.now() + timeout; while (Date.now() < deadline) { const value = await probe(); if (value) return value; await sleep(120); } return null; } export async function assertName(connection, text, label, log) { const nodes = await waitFor(async () => { const current = await axNodes(connection); return current.some((node) => axLabel(node).includes(text)) ? current : false; }, 12000); if (!nodes) throw new Error(`Missing ${label}: ${text}`); log.checks.push(label); } export async function capture(connection, directory, log, name) { const shot = await connection.send("Page.captureScreenshot", { format: "png", captureBeyondViewport: false, }); const file = join(directory, `${name}.png`); await writeFile(file, Buffer.from(shot.data, "base64")); log.captures.push(file); } export async function click(connection, text) { const nodes = await axNodes(connection); const interactive = new Set([ "button", "link", "tab", "radio", "checkbox", "menuitem", "option", "switch", "toggleButton", "listitem", ]); const byId = new Map(nodes.map((node) => [node.nodeId, node])); const clickable = (node) => { let current = node; while (current) { if (interactive.has(current.role?.value) && current.backendDOMNodeId) { return current; } current = byId.get(current.parentId); } return null; }; const matches = nodes.filter((node) => { const name = axLabel(node); return ( name.includes(text) && (node.role?.value === "button" || node.role?.value === "StaticText" || interactive.has(node.role?.value)) ); }); const target = matches.map(clickable).find(Boolean); if (!target) throw new Error(`No clickable semantics node for ${text}`); const { model } = await connection.send("DOM.getBoxModel", { backendNodeId: target.backendDOMNodeId, }); const [x1, y1, , , x3, y3] = model.content; await clickAt(connection, (x1 + x3) / 2, (y1 + y3) / 2); } export async function clickAt(connection, x, y) { await connection.send("Input.dispatchMouseEvent", { type: "mousePressed", x, y, button: "left", clickCount: 1, }); await connection.send("Input.dispatchMouseEvent", { type: "mouseReleased", x, y, button: "left", clickCount: 1, }); await sleep(500); } export async function dragSelect(connection, x1, y1, x2, y2) { await connection.send("Input.dispatchMouseEvent", { type: "mousePressed", x: x1, y: y1, button: "left", clickCount: 1, }); for (let step = 1; step <= 8; step++) { await connection.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: x1 + ((x2 - x1) * step) / 8, y: y1 + ((y2 - y1) * step) / 8, button: "left", buttons: 1, }); await sleep(30); } await connection.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: x2, y: y2, button: "left", clickCount: 1, }); await sleep(400); }