4

我有几个地方使用 $timeout 或 $interval 在 UI 中发生延迟。这是一个简化的示例:

控制器代码

$timeout(function() {
    $scope.showElement = true;
}, 10000);

HTML

<div id="myElement" ng-show="showElement"></div>

我希望能够创建一个端到端量角器测试,以测试 #myElement 在等待 10 秒后是否显示。我发现这样做的唯一方法是调用 browser.sleep(10000),这会导致我的测试实际延迟 10 秒。这行得通,但是这些暂停加起来会显着增加我的测试持续时间。想象一下,您想测试一个模态是否在 30 分钟不活动后弹出。

有没有办法模拟特定时间的流逝,类似于茉莉花测试中的 $timeout.flush() ?

4

2 回答 2

3

您可以装饰$timeout$interval覆盖提供给它们的延迟:

较低的等待时间.js

exports.module = function() {
    angular.module('lowerWaitTimeDecorator', [])
    .config(function($provide) {
        $provide.decorator('$timeout', function($delegate) {
            return function() {
                // The second argument is the delay in ms
                arguments[1] = arguments[1] / 10;
                return $delegate.apply(this, arguments);
            };
        });
    })
};

用法

beforeAll(function() {
    var lowerWaitTime = require('lower-wait-time');
    browser.addMockModule('lowerWaitTimeDecorator', lowerWaitTime.module);
});

afterAll(function() {
    browser.removeMockModule('lowerWaitTimeDecorator');
});

it('My-sped-up-test', function() {

});
于 2015-06-29T07:49:53.600 回答
1

您可以使用async.whilst来执行此操作。这个想法是继续寻找元素,直到达到超时。如果在超时到达之前找到元素或者如果在超时内未找到元素,则测试失败,否则测试通过。我没有测试过这个,但你明白了。例如,

var driver = browser.driver,
 wd = browser.wd,
 async = require('async'),
 start = Date.now(),
 found = false,
 diff;
 async.whilst(
      function() {
          var diff = Date.now() - start;
          return diff <= 10000 && !found;
      },
      function(callback) {
         driver.findElement(wd.By.id('myElement')).then(function() {
              found = true;
              callback();
           },function(err) {
            found = false;
            callback();
         });
      },
      function (err) {
          var isTesrPassed = !err && found && diff>=10000;
          assertTrue(isTestPassed, 'element visibility test failed');
      }
 );
于 2015-06-28T21:30:32.950 回答