·5 min read

Headless Chrome CDP UI test: 3 traps that lie

Driving a three.js editor over the DevTools protocol, we lost hours to our own test driver. Synthetic pointer events, a release with the wrong button, and a const that survives between evaluate calls.

testingjavascriptgamedevai

Contents

We drive our browser-based block editor in headless Chrome over the DevTools protocol (CDP). A script clicks, drags, moves sliders and checks the result against a written checklist for each milestone. It is the closest thing we have to a person sitting at the app.

Three times the checklist failed and the app was fine. Each time the fault was in our driver, and each time it looked like a real bug in the editor. This post lists the three traps, the messages and symptoms that gave them away, and the fix for each. If you automate a canvas app, a gizmo or any drag interaction over CDP, you will probably meet at least one.

Trap 1: synthetic pointer events never grab the gizmo

Our editor uses the three.js transform gizmo to move parts. The first driver sent events from JavaScript inside the page:

canvas.dispatchEvent(new PointerEvent('pointerdown', { clientX, clientY, pointerId: 1 }));

The ball never moved. We sampled several points along the red X arrow, hovering and then pressing on each, and still got no grab. Hovering mattered, because the gizmo only arms a handle after a hover. But hovering was not the problem.

The cause, probably: a dispatched PointerEvent has no real pointer behind it. The gizmo calls setPointerCapture for the pointer id inside its handler, and that call fails when the browser knows of no active pointer with that id. We did not read the library to confirm the exact line. What we do know is what fixed it: sending input through the protocol.

await cdp.send('Input.dispatchMouseEvent',
  { type: 'mouseMoved', x, y });
await cdp.send('Input.dispatchMouseEvent',
  { type: 'mousePressed', x, y, button: 'left', clickCount: 1 });

Input.dispatchMouseEvent produces trusted events, the same kind a mouse makes. After the switch all six steps of that milestone's checklist passed, including the 4-decimal rounding of the moved position.

Trap 2: a release with button "none" never ends the drag

A later milestone added live updates and undo, and the checklist suddenly broke everywhere. Picking failed on every click, even a floor click at the default blend setting that had worked a day earlier. It looked like a regression in the app, and a bad one.

We added temporary logging to the pointer and drag handlers and ran again. The log showed dragging-changed true and then nothing. There was no up entry. The press reached the canvas and the release did not, so the app believed a drag was still in progress and ignored every pick after it.

Our mouse helper sent the release like this:

{ type: 'mouseReleased', x, y, button: 'none' }

Chrome needs button: 'left' on the release before it delivers it. The previous milestone's driver sent left, which is why that one worked. We put left back, removed the logging from the viewport, and the checks passed.

The lesson is about the failure shape. One missing release left state behind, and everything after it looked broken. When "all clicks fail" starts after one particular step in a script, suspect that step's cleanup.

Trap 3: top-level const survives between evaluate calls

The third one took seven experiments. A report came in that a slider did nothing: we moved it, and the mesh did not change. We checked the store, then the draft, then the mesh, with different scenes and different values. Some runs showed the edit, some did not.

The runs that worked all used an IIFE. The runs that failed did not. Here is the driver pattern that failed:

await cdp.send('Runtime.evaluate', {
  expression: `const e = document.querySelector('#size'); e.value = 2; e.dispatchEvent(new Event('input'))`
});

Chrome keeps top-level const and let bindings in the page's global scope between Runtime.evaluate calls. The first call declares e. The second call declares e again and throws:

Identifier 'e' has already been declared

The call throws before any code runs, so the edit silently never happens. Our driver did not look at the exception details, so nothing was reported. The slider "did nothing" because the driver never moved it.

Two fixes, and we use both. Wrap every expression in an IIFE so names stay local:

expression: `(() => { const e = document.querySelector('#size'); e.value = 2;
  e.dispatchEvent(new Event('input')); })()`

And have the driver check exceptionDetails on every Runtime.evaluate response and fail loudly if it is present. The second fix is the one that would have saved us the seven experiments.

Why these were hard to see

All three share a feature: the app produced no error. Trap 1 had no exception, the gizmo just ignored the event. Trap 2 left the app in a state that was consistent with a drag still running. Trap 3 threw, but into a response we did not read.

Three habits came out of it:

  1. Before changing the app, ask whether the driver did what you think. Log what it sent and what the page received. In trap 2 the missing up line settled it in one run.
  2. Use real input for anything with pointer capture, hover arming or drag. Use page JavaScript only for plain form controls.
  3. Treat every protocol response as data to check. An ignored exceptionDetails turns a one-line error into a long hunt.

We also take a screenshot before blaming the app. In the first trap the screenshot showed the ball selected with its gizmo drawn correctly, which told us the app state was fine and the problem was our click.

A small driver checklist

If you are starting a CDP driver for a three.js or canvas app, these are the lines we would put in on day one:

  • A mouse helper that always sends button: 'left' on both press and release, and always sends a mouseMoved first so hover-armed handles are ready.
  • An evaluate helper that wraps the expression in an IIFE and throws when the response has exceptionDetails, with the text of the error.
  • A step log that prints what was sent. When a check fails you can then compare "what we sent" with "what the page logged", which is how the missing up event stood out.
  • A screenshot on every failed check. In our first trap the picture showed a correct app state, which made the driver the suspect.

None of this is clever. It is the same discipline as for any test code: a driver is code, it has bugs, and the bugs are most dangerous when they look like bugs in the thing under test.

More posts

New writing when there is something worth saying. By email or by RSS, whichever you prefer.