【问题标题】:Programmatically simulate user interaction for Unit Testing in Google Dart以编程方式模拟 Google Dart 中单元测试的用户交互
【发布时间】:2014-04-24 02:53:23
【问题描述】:

我正在尝试为一个相当复杂的菜单编写一些单元测试,并且我想模拟用户与该菜单的交互。

我发现可以使用querySelector("#myElement").click() 模拟点击,但我希望能够触发更复杂的事件,例如mouseDowndocumentmouseMove、@987654325 的特定位置@和touchEnd

【问题讨论】:

    标签: unit-testing dart dart-html


    【解决方案1】:

    我找到了一种方法。它仍然不完整或不完美,但它提供了我需要的一点。

    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));
    }
    

    我的想法来自:Async and User Input Testing in Dart

    【讨论】:

      猜你喜欢
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 2012-02-27
      • 1970-01-01
      相关资源
      最近更新 更多