Pregunta

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.

¿Fue útil?

Solución

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()
    ..style.height = "50px"
    ..style.width = "50px"
    ..style.border = "1px solid black";
  document.body.children.add(menuSprite);

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

  // Create the events.
  // TODO: Events have more parameters or be more specific.
  // See:
  //  * https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-dom-html.MouseEvent
  //  * https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-dom-html.TouchEvent
  //
  // 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), () =>
      menuSprite.dispatchEvent(myMouseDown));
  new Future.delayed(new Duration(milliseconds: 1000), () =>
      menuSprite.dispatchEvent(myMouseUp));
  new Future.delayed(new Duration(milliseconds: 1500), () =>
      menuSprite.dispatchEvent(myTouchStart));
  new Future.delayed(new Duration(milliseconds: 2000), () =>
      menuSprite.dispatchEvent(myTouchEnd));
}

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top