javascript - 如何让 Protractor 不等待 $timeout?

标签 javascript angularjs selenium protractor angularjs-e2e

我正在使用 Protractor 测试我的 Angular 应用程序。 用户登录到我的应用程序后,我设置 $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 所以我知道 Protractor 会等到页面完全加载,这意味着他正在等待 $timeout 完成...

如何让 Protractor 不等待 $timeout? 我不想使用:

browser.ignoreSynchronization = true;

因为那时我的测试会因为其他原因而失败(其他 Angular 组件仍然需要时间来加载...)

最佳答案

解决方案是刷新事件超时(as @MBielski mentioned it in comments),但原始刷新方法本身仅在 anuglar-mocks 中可用。要直接使用 angular-mocks,你必须将它作为 <script> 包含在页面上。标签,而且您还必须处理它创建的所有覆盖,它会产生很多副作用。通过监听创建的任何超时,然后按需重置它们,我能够在不使用 Angular 模拟的情况下重新创建刷新。

例如,如果您的 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 进程,因此测试会中断。

关于javascript - 如何让 Protractor 不等待 $timeout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33340693/

相关文章:

java - 为什么这个网站不能定位元素?

javascript - Jquery 高度返回错误值

javascript - react svg getTotalLength

javascript - 如何限制浏览器缓存中来自 firestore 的离线数据

python - WebElement 在调用 driver.refresh() 后作为 NoneType 返回

python - 在输入字段中输入数据不适用于 Selenium

c# - 如何在少数页面上禁用缓存,以避免重复提交表单

angularjs - 在前端过滤MongoDB数据

javascript - 如何使用 json 对象显示 angularjs ng-repeat

javascript - window.open 在 http post angular 中不起作用