我正在使用 Protractor 测试我的角度应用程序。
用户登录到我的应用程序后,我设置了 $timeout 以在一小时内完成某些工作(因此,如果用户在 13:00 登录,$timeout 将在 14:00 运行)。
我不断遇到这些失败:
"Timed out waiting for Protractor to synchronize with the page after 20 seconds. Please see https://github.com/angular/protractor/blob/master/docs/faq.md. The following tasks were pending: - $timeout: function onTimeoutDone(){....."
我读过这个超时页面:https://github.com/angular/protractor/blob/master/docs/timeouts.md所以我知道量角器会等到页面完全加载,这意味着他正在等待 $timeout 完成......
我怎样才能让量角器不等待$超时?
我不想使用:
browser.ignoreSynchronization = true;
因为这样我的测试将因其他原因而失败(其他角度组件仍然需要时间加载......)
解决方案是刷新活动超时(正如 @MBielski 在评论中提到的),但原始的刷新方法本身仅在角度模拟中可用。要直接使用 Angular-Mocks,您必须将其作为<script>
标签,而且您还必须处理它创建的所有覆盖,它会产生很多副作用。我能够在不使用角度模拟的情况下重新创建刷新,方法是监听创建的任何超时,然后根据需要重置它们。
例如,如果您的 Angular 应用程序超时:
$timeout(function () {
alert('Hello World');
}, 10000); // say hello in 10 sec
测试将如下所示:
it('should reset timeouts', function () {
browser.addMockModule('e2eFlushTimeouts', function () {
angular
.module('e2eFlushTimeouts', [])
.run(function ($browser) {
// store all created timeouts
var timeouts = [];
// listen to all timeouts created by overriding
// a method responsible for that
var originalDefer = $browser.defer;
$browser.defer = function (fn, delay) {
// originally it returns timeout id
var timeoutId = originalDefer.apply($browser, arguments);
// store it to be able to remove it later
timeouts.push({ id: timeoutId, delay: delay });
// preserve original behavior
return timeoutId;
};
// compatibility with original method
$browser.defer.cancel = originalDefer.cancel;
// create a global method to flush timeouts greater than @delay
// call it using browser.executeScript()
window.e2eFlushTimeouts = function (delay) {
timeouts.forEach(function (timeout) {
if (timeout.delay >= delay) {
$browser.defer.cancel(timeout.id);
}
});
};
});
});
browser.get('example.com');
// do test stuff
browser.executeScript(function () {
// flush everything that has a delay more that 6 sec
window.e2eFlushTimeouts(6000);
});
expect(something).toBe(true);
});
这有点实验性,我不确定它是否适合你的情况。这段代码也可以通过移动来简化browser.addMockModule
到一个单独的node.js 模块。如果您想删除短超时(例如 100 毫秒),也可能会出现问题,它可以取消当前正在运行的 Angular 进程,因此测试将中断。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)