我们什么时候应该将 .then 与 Protractor Promise 一起使用? [英] When should we use .then with Protractor Promise?

查看:76
本文介绍了我们什么时候应该将 .then 与 Protractor Promise 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Protractor 有很多不稳定性,而且我确定有些我不明白.有时我在继续之前点击按钮时需要使用 .then() ,有时它没有任何影响,我不应该使用 .then() 或测试失败.

I've got many instability with Protractor, and I'm sure there is something I don't understand. Sometimes I need use the .then() when clicking on a button before continuing, sometimes it don't have any impact and I should not use .then() or the test failed.

我想知道在 Protractor 中进行测试时应该什么时候使用 .then() 回调?示例:

I wonder when should I use the .then() callback when testing in Protractor ? Example :

createAccountForm = $('#form-create-account');
submitButton = createAccountForm.$('button[type=submit]');

browser.wait(EC.elementToBeClickable(submitButton), 5000);
submitButton.click(); // .then(function(){   <-- uncomment in the .then form

// find the confirmation message
var message = $('.alert-success');
browser.wait(EC.visibilityOf(message), 5000);
log.debug('After visibilityOf');

expect(message.isPresent()).to.be.eventually.true;
// }); --> uncomment when in .then form

当我使用这种形式的测试(没有 .then())时,我在浏览器上看到 点击按钮没有完成,测试继续以下期望然后停止.

When I use this form of test (without .then()) I see on browser that the click on the button is not done, the test continue with the following expect and then stop.

如果我使用 .then() 表单,按钮点击完成,测试继续没有错误.

If I use the .then() form, the click on the button is done, and the test continue without error.

在其他测试中,我不需要在单击按钮时使用 then() 回调.

On other test, I don't need to use the then() callback when clicking on button.

那么,我什么时候应该使用 .then() 什么时候不应该使用?

So , when should I use the .then() and when not ?

让-马克

推荐答案

这个问题的答案可以在这个帖子中找到:http://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/

The answer of this question can be found in this post : http://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/

即:

  1. 量角器将控制流中的所有驱动程序命令排入队列,
  2. 当您需要驱动程序命令的结果时,您应该使用 .then,
  3. 当您不需要驱动程序的结果时,您可以避免 .then 但所有以下指令必须在 ControlFlow 中排队,否则它们将在队列中的命令之前运行导致不可预测结果.所以,如果你想运行一个非驱动程序测试命令,你应该将它添加到 .then 回调或将测试包装到一个 Promise 并将测试排入 ControlFlow 中.请参阅下面的示例.

这里是我的测试没有 .then 的例子:

Here is an example of my test working without .then :

log.debug('test0');

// enqueue the click
submitButton.click(); 
var message = $('.alert-success'); 

// enqueue the wait for message to be visible  
browser.wait(EC.visibilityOf(message), 5000);  

log.debug('test1');

// enqueue a test
expect(message.isPresent()).to.be.eventually.true;
log.debug('test2');

// a function returning a promise that does an async test (check in MongoDB Collection)
var testAccount = function () {           
    var deferred = protractor.promise.defer();

    // Verify that an account has been created
    accountColl.find({}).toArray(function (err, accs) {
        log.debug('test5');
        expect(err).to.not.exist;
        log.debug('test6');
        expect(accs.length).to.equal(1);
        return deferred.fulfill();
    });
    return deferred.promise;
};

log.debug('test3');

// Enqueue the testAccount function
browser.controlFlow().execute(testAccount);  
log.debug('test4');

输出现在是我们所期望的:

Output is now what we expect :

test0

test1

test2

test3

test4

test5

test6

这篇关于我们什么时候应该将 .then 与 Protractor Promise 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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