我正在尝试为一个相当复杂的菜单编写一些单元测试,我想模拟用户与该菜单的交互。
我发现可以使用querySelector("#myElement").click()
来模拟点击,但我希望能够在mouseDown
的特定位置触发更复杂的事件,例如document
, mouseMove
,touchStart
和touchEnd
等
答案 0 :(得分:2)
我找到了一种方法。它仍然不完整或完美,但它提供了我需要的东西。
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));
}
得到了这个想法