done()的用途和使用方法(量角器,茉莉花) [英] what done() is for and how to use it ( protractor, jasmine)

查看:110
本文介绍了done()的用途和使用方法(量角器,茉莉花)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

it('should for something', function check(done) {
  browser.sleep(2000);
  $('.csTx').isPresent().then(function(result) {
    if(result) {
      done();
    } else {
      xPage.clickBack();
      check(done);
    }
  })
}, 30000);

有人可以解释done()的工作原理以及它的用途。我用谷歌搜索了它,但找不到任何容易让我理解的信息。我正在使用量角器和茉莉花进行自动化。请考虑以上代码。

Can someone explain how done() works and what this is for. I googled it but cannot find any information that would be easy enough for me to understand. I am automating with protractor and jasmine. please consider the above code.

推荐答案

如果您的测试创建并行TaskQueue,则需要使用 done 在测试的控制流程中(了解有关的更多信息)承诺和控制流程)。

You need to use done if your test creates a parallel TaskQueue in your test's Control Flow (read more about promises and control flow).

例如:

describe('Control Flow', function() {
    function logFromPromise(text) {
        var deferred = protractor.promise.defer();
        deferred.then(function() {
            console.log(text);
        });
        deferred.fulfill();
        return deferred;
    }

    it('multiple control flows', function() {
        setTimeout(function() {
            logFromPromise('1');
        });
        logFromPromise('0');
    });
}

调用 setTime 创建并行任务队列在控件中:

Calling setTime creates a parallel Task Queue in the control:

ControlFlow
| TaskQueue
| | Task<Run fit("multiple control flows") in control flow>
| | | TaskQueue 
| | | | Task <logFromPromise('0');>
| TaskQueue
| | Task <setTimeout>

量角器认为测试打印 0 后完成。在此示例中,测试完成后可能会打印 1 。要使量角器等待任务< setTimeout> ,您需要调用done函数:

Protractor thinks the test is "done" after 0 is printed. In this example, 1 will probably be printed after the test is completed. To make protractor wait for Task <setTimeout>, you need to call the done function:

    it('multiple control flows', function(done) {
        setTimeout(function() {
            logFromPromise('1').then(function() {
                done();
            });
        });
        logFromPromise('0');
    });

如果可以,请在测试完成时让量角器处理。并行TaskQueues可能会导致测试中出现意外的竞争条件。

If you can, let protractor handle when the test is "done". Having parallel TaskQueues can lead to unexpected race conditions in your test.

这篇关于done()的用途和使用方法(量角器,茉莉花)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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