DEV Community

Cover image for Why faking real browser events doesn't work
Mariam Reba Alexander
Mariam Reba Alexander

Posted on

Why faking real browser events doesn't work

Some time ago, I tried to programmatically trigger a typeahead dropdown with JavaScript for a task I was working on, and I hit a complete brick wall.

I wrote the code to dispatch the input events and saw the value appear in the input box, but nothing else happened. The UI just sat there. It didn't trigger the input event that actually updates the state, and it definitely didn't trigger the dropdown.

It turns out I was fighting a core security feature: the isTrusted read-only property.

The Reality Check:

In the Event interface, isTrusted is the browser’s way of knowing if an event is "real."

  • isTrusted: true – Generated by the User Agent (real human clicks or physical key presses).
  • isTrusted: false – Dispatched via EventTarget.dispatchEvent().

Because I couldn't manually set isTrusted to true, the dropdown's security logic ignored my "fake" events.

The Workaround: Working with the Framework, not the DOM

I eventually solved this by leaning into Vue's reactive system instead of trying to force the browser to believe a lie. Since I couldn't "fake" the event, I updated the data model directly. Then I updated the typeahead option programmatically. It wasn't about the event anymore; it was about the state.

How professional tools handle this:

When you need to scale this for testing, you have to step outside the standard sandbox:
Automation tools like Playwright and Cypress make use of the Chrome DevTools Protocol (CDP) to inject events into the browser's hardware pipeline, making isTrusted true

The Lesson:

If you can’t fake user authority, stop trying to force the DOM. Use a tool like Playwright or Cypress for automation, or work directly with your framework’s native bindings and lifecycle to get the job done.

Top comments (0)