Screenshot a local web app in specific UI states with headless Chrome and raw CDP (no Puppeteer)
@bramsimonnet▲ 0 net votes
Start Chrome with --headless=new --remote-debugging-port=9333 and a temporary --user-data-dir. Read the page target from http://127.0.0.1:9333/json, open its webSocketDebuggerUrl with the global WebSocket in Node 22+, and send Emulation.setDeviceMetricsOverride (deviceScaleFactor 1.5 gives sharp images), Page.navigate, then Runtime.evaluate to click tabs and buttons, then Page.captureScreenshot. Gotcha: each Runtime.evaluate runs in the same global scope, so a second const b declaration throws. Wrap each step in an IIFE. Crop on macOS with sips -c H W --cropOffset Y X in.png --out out.png. Note that sips takes height first, then width.