elements
Start Docs Blog Demos UI Pricing
Elements Manual
  • Getting Started
  • CLI
  • Build
  • Browser
  • Packages
  • TypeScript
  • Elements HTML
  • Assets
  • RPC
  • Router
  • Session
  • Database
  • Migrations
  • LiveTable
  • Channel
  • Jobs
  • Email
  • Payments
  • JSON
  • Async
  • Style
  • UI
  • JSOC
  • Config
  • Tests
  • Logs
  • Deploy
  • Editors
  • Recipes
  • Licenses
  • Issues
  • Changelog
Manual / Browser

Browser

elements man browser

On This Page

  • A Still Of A Route
  • Window Size Is Not Viewport Size
  • Phone Widths
  • Measure Instead Of Reading Pixels
  • Clicking And Reading State Back
  • Read And Write Page State
  • A Template's Own State
  • Related

Look at the route you changed. A green build says the code compiles, not that the page is right, and a screenshot costs a second. Chrome, Edge or any other Chromium build already on the machine can take a still of a route, render it at phone size, click, and read a measurement back out of the page.

The browser is the user's and shows the running app. Where your tool has a browser pane, test the app there as you build, in the pane already showing it: click through it, fill its forms, add a row. The user watching you use the app is how they know the work is real. Headless Chrome is for the checks they do not need to watch. Never open a project file in their browser: its source replaces the page they are watching.

Test every page at phone width (390px), boards and wide tables included, not only at desktop width.

A green build is not a green request: a route that throws is a 500, and the page shows the stack. elements logs shows what the app logged.

Do not install Playwright, Puppeteer or chromedriver, and do not report a layout as unverified because a browser automation library is missing. Look for a Chromium binary first, and only say you could not look if there is none.

A Still Of A Route

The app must be running (elements start, serving http://localhost:4000 (opens in a new tab)).

Terminal
# Windows: chrome.exe or msedge.exe. Linux: google-chrome. "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" \ --headless --disable-gpu --hide-scrollbars \ --window-size=1440,900 --virtual-time-budget=3000 \ --screenshot=/tmp/page.png "http://localhost:4000/your-route"

Then open /tmp/page.png.

Window Size Is Not Viewport Size

--window-size sets the size of the window, not the viewport. The platform enforces a minimum window width, and the window's own chrome takes part of the height. On macOS with Chrome 152 the numbers come out like this:

Flag Viewport Image
--window-size=1440,900 1440x813 1440x900
--window-size=500,800 500x713 500x800
--window-size=390,844 500x757 390x844

The width floor is the platform's minimum window width, and the height loses the window's own chrome. At 390 the page is laid out at 500 and the capture is cropped to 390, so the image is a narrow desktop layout with its right edge cut off. A check made against that image is wrong in two ways: a control positioned past x=390 is cropped out and looks missing, and the phone breakpoints did not apply because the page was laid out at 500.

So use --window-size for desktop sizes only, where the viewport is the width you asked for and only its height is smaller. For a phone, emulate the device.

Phone Widths

Start Chrome once with a debugging port, then drive it over the DevTools Protocol. Emulation.setDeviceMetricsOverride sets the viewport exactly, at the pixel ratio and the mobile flag a phone has.

Pick a port of your own rather than a fixed one, since another project may be driving a Chrome too, and keep the pid the shell gives you:

Terminal
PORT=$((20000 + RANDOM % 10000)) "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" \ --headless --disable-gpu --hide-scrollbars \ --remote-debugging-port=$PORT about:blank & CHROME_PID=$!

Do not pass --user-data-dir. Some builds hang on a cold profile.

Stop it by that pid when the run is done, never by a pattern match, which also kills every other project's Chrome:

Terminal
kill $CHROME_PID

A background Chrome outlives the shell that started it. While one is alive it holds the LaunchServices registration for com.google.Chrome, so opening Chrome from the Dock does nothing at all: no window, exit 0, no error. The browser looks broken and nothing says a headless process is the reason. This applies to the one-shot --screenshot form too, which usually exits on its own but does not when the page never finishes loading.

The client is one file with no dependencies: Node ships a global WebSocket.

shot.mjsPORT=<port> node shot.mjs <url> <out.png> [width] [height]
let [url, out, width = 390, height = 844] = process.argv.slice(2); let port = process.env.PORT; let target = await (await fetch(`http://127.0.0.1:${port}/json/new?about:blank`, { method: "PUT" })).json(); let ws = new WebSocket(target.webSocketDebuggerUrl); let waiting = new Map(); let onLoad; let id = 0; await new Promise((ok) => (ws.onopen = ok)); ws.onmessage = (e) => { let m = JSON.parse(e.data); if (m.id && waiting.has(m.id)) { waiting.get(m.id)(m.result); waiting.delete(m.id); } if (m.method === "Page.loadEventFired") { onLoad?.(); } }; function send(method, params = {}) { return new Promise((ok) => { waiting.set(++id, ok); ws.send(JSON.stringify({ id, method, params })); }); } await send("Page.enable"); await send("Emulation.setDeviceMetricsOverride", { width: +width, height: +height, deviceScaleFactor: 2, mobile: +width < 768, }); let loaded = new Promise((ok) => (onLoad = ok)); await send("Page.navigate", { url }); await loaded; let shot = await send("Page.captureScreenshot", { format: "png" }); let fs = await import("node:fs/promises"); await fs.writeFile(out, Buffer.from(shot.data, "base64")); ws.close();

PORT=$PORT node shot.mjs http://localhost:4000/ /tmp/phone.png 390 844 writes a 780x1688 image: 390x844 at deviceScaleFactor: 2, laid out at 390 the way the phone lays it out. Check 320 as well. It is the narrowest width to support, and a row of buttons that fits at 390 may not fit at 320.

mobile: true honours the page's viewport meta, and Elements puts <meta name="viewport" content="width=device-width, initial-scale=1.0"> in every rendered page, so a route renders at the width you asked for.

An editor's built-in browser, such as the VS Code tab the chat agent shares with you, keeps the pane's fixed width, and the agent's browser tools cannot change it. Check a layout at phone and desktop widths with the headless renders above (--window-size and --screenshot for desktop, shot.mjs for phones), and use the shared tab to click through the app.

Measure Instead Of Reading Pixels

The same connection evaluates JavaScript in the page, so you can read exact layout values instead of estimating them from a screenshot.

shot.mjs
let evaluate = async (expression) => (await send("Runtime.evaluate", { expression, returnByValue: true, awaitPromise: true, })).result.value; // Horizontal overflow: 0 or the page scrolls sideways on a phone. await evaluate(`document.documentElement.scrollWidth - document.documentElement.clientWidth`); // Pages that share a header share its edges. Run this on every route and // compare: one left and width that differs is a header that jumps. await evaluate(`["header > *", "main"].map((sel) => { let r = document.querySelector(sel).getBoundingClientRect(); return [sel, r.left, r.width]; })`); // Did the wordmark stay on the heading's line? await evaluate(`document.querySelector("h1 .mark").getBoundingClientRect().top === document.querySelector("h1").getBoundingClientRect().top`);

An element whose right edge is past the viewport overflows only if no ancestor clips it: check for an overflow: hidden ancestor before reporting it.

Clicking And Reading State Back

Click an element by evaluating click() on it, then read values from the page to check the result of the click.

shot.mjs
await evaluate(`document.querySelector("a.button.is-primary").click()`); await new Promise((ok) => setTimeout(ok, 500)); await evaluate(`location.pathname`); // "/signin"

Input.dispatchMouseEvent and Input.dispatchKeyEvent drive real input events where a synthetic click() is not enough, such as a control that reacts to pointerdown or to a keystroke.

Submit a form by clicking its submit button. An agent's browser pane usually sends Return as a keydown with an empty key and no text, and the browser does not treat that as Enter, so the form never submits and the RPC function is never called. A fully specified Input.dispatchKeyEvent (key: "Enter", code: "Enter", windowsVirtualKeyCode: 13, text: "\r") does submit. Where there is no button to click, call requestSubmit():

shot.mjs
await evaluate(`document.querySelector("form").requestSubmit()`);

Elements forms submit on a real Enter like any other form: the runtime calls preventDefault() and runs your onsubmit handler. A form without an onsubmit handler does nothing, since its data only reaches the server through an @rpc call the handler makes. When a pane's Return does nothing, click the submit button or call requestSubmit() and carry on; there is nothing to report to the user.

Read And Write Page State

The page exposes its own state. $ is the page's attributes object and template is the template, both on window, and both reactive: assign to one and the dom bound to it repaints.

Read it in one call. State is the data the page renders from, a fraction of the size of its markup, so reading it is the quick way to confirm what a click, a save or a live update did. Read the state to check behavior, and take a screenshot to check how the page looks.

shot.mjs
await evaluate(`Object.keys($)`); // the page's attributes by name await evaluate(`JSON.stringify($.edit)`); // one of them await evaluate(`JSON.stringify($)`); // all of them

Write it the same way, and set several fields in one call:

shot.mjs
await evaluate(`$.edit.title = "renamed"`); // the input updates itself await evaluate(`Object.assign($.edit, { title: "renamed", done: true })`);

Set an input's value by assigning through $. Do not set .value, .checked or .group on an input: those are two-way bindings, and a write to the dom property does not update the bound variable. The handler then reads the old value, or an empty one, and acts on it. Assigning through $ changes the input and the bound variable together.

Assigning to $ changes data and does not call a handler. To run a handler, such as the function a Save button calls, click the button, then read $ back.

A Template's Own State

Every template on the page has an attributes object, like the page's $. In the Elements panel each template is between two comment nodes, <!--<Dialog e:id='240'>--> and <!--</Dialog>-->, and each comment node has the template as its .template property. Select either comment and $0.template.attrs is that template's state, to read or to assign.

From a script, find templates by name:

shot.mjs
// Every template with this name, in document order. let templates = `((name) => { let found = []; let walker = document.createTreeWalker(document, NodeFilter.SHOW_COMMENT); while (walker.nextNode()) { let node = walker.currentNode; if (node.template && node.data.split(/[ >]/)[0] === "<" + name) { found.push(node.template); } } return found; })`; await evaluate(`JSON.stringify(${templates}("Dialog")[0].attrs)`); // open, size, label... await evaluate(`${templates}("Dialog")[0].attrs.open = true`); // opens the dialog

Assigning to an attribute that has a two-way binding also sets the page variable bound to it, so opening a Dialog through its own open sets the page variable bound to open.

Where you need real key events rather than a state change, use Input.dispatchKeyEvent.

Each call to /json/new opens another tab in the same browser, so every tab shares one cookie jar and one signed-in user. Live data between two different users needs two Chrome processes, each with its own --remote-debugging-port.

Cookies are per host, not per port, so two local apps on different ports share them: signing in to one can sign you out of the other. Drive a second app in its own Chrome process with its own port, or in its own browser context (Target.createBrowserContext).

Related

  • build: the build loop and what a green build means.
  • tests: the test runner, which checks the app on every build.
Prev BuildNext Packages
Product
Manual Pricing Compare
Company
History Contact
Legal
Privacy Policy Acceptable Use Terms License Agreement
© Elements Dev Corporation 2026