我怎样才能让量角器等不到$超时? [英] How can I make Protractor NOT wait for $timeout?

查看:294
本文介绍了我怎样才能让量角器等不到$超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试我用量角器角应用。
一旦用户登录到我的应用程序,我设置了$超时做一些工作在一小时内(所以如果用户登录的13:00,在$超时将14:00运行)。
我不断收到这些故障:

I'm testing my angular application with Protractor. Once the user is logged in to my app, I set a $timeout to do some job in one hour (so if the user was logged-in in 13:00, the $timeout will run at 14:00). I keep getting these failures:

"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(){....."

我读过这个超时页:<一href=\"https://github.com/angular/protractor/blob/master/docs/timeouts.md\">https://github.com/angular/protractor/blob/master/docs/timeouts.md
我是这么理解量角器等待,直到页面完全加载,这意味着他在等待超时$完成...

I've read this timeouts page: https://github.com/angular/protractor/blob/master/docs/timeouts.md so I understand Protractor waits till the page is fully loaded which means he's waiting for the $timeout to complete...

我怎样才能让量角器不能等待超时$?
我不想使用:

How can I make Protractor NOT wait for that $timeout? I don't want to use:

browser.ignoreSynchronization = true;

由于然后我的测试中会因其他原因失效(其它角度部件仍然需要时间来加载...)

Because then my tests will fail for other reasons (other angular components still needs the time to load...)

推荐答案

该解决方案将刷新活跃超时(<一个href=\"http://stackoverflow.com/questions/33340693/how-can-i-make-protractor-not-wait-for-timeout#comment54491368_33340693\">as @MBielski在评论提到),但原来的冲洗方法本身仅在anuglar,模拟考试可用。直接用角嘲笑你将不得不包括它的页面上&LT;脚本&GT; 标签,你也必须对付它创建所有覆盖,它会产生很多副作用。我可以不使用角嘲笑通过聆听了在创建任何超时,然后CH375复位并在需要重新创建刷新。

The solution will be to flush active timeouts (as @MBielski mentioned it in comments), but original flush method itself is available only in anuglar-mocks. To use angular-mocks directly you will have to include it on the page as a <script> tag and also you'll have to deal with all overrides it creates, it produces a lot of side effects. I was able to re-create flush without using angular-mocks by listening to any timeouts that get created and then reseting them on demand.

例如,如果你有一个超时在你的角度应用:

For example, if you have a timeout in your Angular app:

$timeout(function () {
    alert('Hello World');
}, 10000); // say hello in 10 sec

该测试将是这样的:

The test will look like:

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);
});

这有点实验性的,我不知道它是否会为你的案件工作。这code,也可以通过移动 browser.addMockModule 到单独的Node.js模块简化。也有可能是问题,如果你想删除的短超时(如100毫秒),它可以取消当前正在运行的进程角,因此测试将中断。

It's kinda experimental, I am not sure if it will work for your case. This code can also be simplified by moving browser.addMockModule to a separate node.js module. Also there may be problems if you'd want to remove short timeouts (like 100ms), it can cancel currently running Angular processes, therefore the test will break.

这篇关于我怎样才能让量角器等不到$超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆