Javascript 如何使量角器不等待$timeout?
我正在用量角器测试我的角度应用。 用户登录到我的应用程序后,我设置$timeout以在一小时内完成某些工作(因此,如果用户在13:00登录,则$timeout将在14:00运行)。 我不断遇到这些失败:Javascript 如何使量角器不等待$timeout?,javascript,angularjs,selenium,protractor,angularjs-e2e,Javascript,Angularjs,Selenium,Protractor,Angularjs E2e,我正在用量角器测试我的角度应用。 用户登录到我的应用程序后,我设置$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/f
"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(){....."
我已经阅读了这个超时页面:
所以我知道量角器一直等到页面完全加载,这意味着他在等待$timeout完成
如何使量角器不等待$timeout?
我不想使用:
browser.ignoreSynchronization = true;
因为这样,我的测试将因其他原因而失败(其他角度组件仍需要时间加载…解决方案将是刷新活动超时(),但原始刷新方法本身仅在anuglar模拟中可用。要直接使用角度模拟,您必须将其作为
标记包含在页面上,并且您还必须处理它创建的所有覆盖,它会产生很多副作用。我能够在不使用角度模拟的情况下重新创建flush,方法是监听创建的任何超时,然后根据需要重置它们
例如,如果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模块,也可以简化此代码。此外,如果要删除短超时(如100ms),可能会出现问题,它可以取消当前运行的角度进程,因此测试将中断。解决方案是刷新活动超时(),但原始刷新方法本身仅在anuglar模拟中可用。要直接使用角度模拟,您必须将其作为
标记包含在页面上,并且您还必须处理它创建的所有覆盖,它会产生很多副作用。我能够在不使用角度模拟的情况下重新创建flush,方法是监听创建的任何超时,然后根据需要重置它们
例如,如果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模块,也可以简化此代码。此外,如果要删除短超时(如100ms),可能会出现问题,它可以取消当前正在运行的角度进程,因此测试将中断。解决方案是使用拦截器并修改http请求,该请求将超时,并将自定义超时设置为几毫秒(您需要的时间)在长时间运行http请求后,http请求将被关闭(因为新的超时),然后您可以测试即时响应
这很好,很有希望。解决方案是使用拦截器,修改正在超时的http请求,并将该http请求的自定义超时设置为几毫秒(您需要的),以便在长时间运行后,http请求将关闭(由于新的超时),然后您可以测试即时响应
这很好,很有希望。您是否在设置超时后立即尝试通过测试刷新该超时?您是否在设置超时后立即尝试通过测试刷新该超时?很好的解决方案。但是,如果我理解正确,此解决方案将取消所有超时,因此我无法在超时期间测试断言。@BiAiB,只有在调用
浏览器时才会取消超时。executeScript(…)
部分,我相信您可以先进行断言,然后在需要时取消超时。也许将browser.executeScript(…)
部分放在每次之后的中也会起作用。很好的解决方案。但是,如果我理解正确,此解决方案将取消所有超时,因此我无法在超时期间测试断言。@BiAiB,只有在调用浏览器时才会取消超时。executeScript(…)
部分,我相信您可以先进行断言,然后在需要时取消超时。也许将browser.executeScript(…)
部分放在每次之后也会起作用。