模拟量角器中的时间流逝? [英] Simulate the passage of time in protractor?
问题描述
我有一些地方可以使用$ timeout或$ interval延迟在UI中进行操作.这是一个简化的示例:
I have a few spots where things happen in the UI on a delay using $timeout or $interval. Here's a simplified example:
控制器代码:
$timeout(function() {
$scope.showElement = true;
}, 10000);
HTML :
<div id="myElement" ng-show="showElement"></div>
我希望能够创建一个端到端量角器测试,以测试#myElement是否在等待10秒后显示.我发现这样做的唯一方法是调用browser.sleep(10000),这会导致我的测试实际延迟10秒.这可行,但是这些停顿加起来加起来并显着增加了我的测试时间.想象一下您要测试闲置30分钟后是否弹出模态的情况.
I want to be able to create an end-to-end Protractor test that tests whether #myElement gets displayed after a 10 second wait. The only way I have found to do this is to call browser.sleep(10000), which results in an actual 10-second delay in my test. This works, but these pauses add up add up and significantly increase the duration of my tests. Imagine a situation where you wanted to test whether a modal pops up after 30 minutes of inactivity.
是否有一种方法可以模拟特定时间的流逝,类似于茉莉花测试中的$ timeout.flush()?
Is there a way to simulate the passage of a specific amount of time, similar to $timeout.flush() in a jasmine test?
推荐答案
您可以修饰$timeout
和$interval
以覆盖提供给它们的延迟:
You can decorate $timeout
and $interval
to override the delay supplied to them:
lower-wait-time.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() {
});
这篇关于模拟量角器中的时间流逝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!