Programmatically simulate user interaction for Unit Testing in Google Dart


I am trying to write some Unit Testing for a rather complex menu and I’d like to simulate the user interaction with that menu.

I found that it is possible to simulate a click with querySelector("#myElement").click() but I would like to be able to trigger more complex events, like mouseDown in a particular place of the document, mouseMove, touchStart and touchEnd, etc.


I found a way to do that. It is still not complete or perfect, but it provides a bit what I needed.

import 'dart:html';
import 'dart:async';

void main() {

  // Create the target element.
  DivElement menuSprite = new DivElement() = "50px" = "50px" = "1px solid black";

  // Asign event listeners.
    ..onMouseDown.listen((e) => = "red")
    ..onMouseUp.listen((e) => = "blue")
    ..onTouchStart.listen((e) => = "orange")
    ..onTouchEnd.listen((e) => = "brown");

  // Create the events.
  // TODO: Events have more parameters or be more specific.
  // See:
  //  *
  //  *
  // CAUTION: event type is case sensitive ('mouseDown' desn't work).
  Event myMouseDown = new Event('mousedown');
  Event myMouseUp = new Event('mouseup');
  Event myTouchStart = new Event('touchstart');
  Event myTouchEnd = new Event('touchend');
  // TODO: mouseMove and touchMove events.

  // We can fire the events using "Element.dispatchEvent()".
  new Future.delayed(new Duration(milliseconds: 500), () =>
  new Future.delayed(new Duration(milliseconds: 1000), () =>
  new Future.delayed(new Duration(milliseconds: 1500), () =>
  new Future.delayed(new Duration(milliseconds: 2000), () =>

I’ve got the idea from: Async and User Input Testing in Dart

Answered By – Daniel

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *