gpt4 book ai didi

angularjs - 模拟 Protractor 中的时间流逝?

转载 作者:行者123 更新时间:2023-12-01 00:51:30 25 4
gpt4 key购买 nike

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

Controller 代码 :

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

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

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

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

最佳答案

可以装修$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() {

});

关于angularjs - 模拟 Protractor 中的时间流逝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31083521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com