简体   繁体   English

使用量角器的 browser.wait 和条件

[英]browser.wait and conditions with Protractor

With protractor, I'm creating a customer object, and when saving I want to check that the customer object that just got created is actually displayed.使用量角器,我正在创建一个客户对象,在保存时我想检查刚刚创建的客户对象是否实际显示。

In "UI" term, after clicking on the save button there is a little delay where the dialog creation is closed and the customer list updated.在“UI”术语中,单击保存按钮后,对话框创建关闭和客户列表更新有一点延迟。

I have the original customerCount, so I want the browser to wait until there is customerCount + 1 customer card.我有原始的customerCount,所以我希望浏览器等到有customerCount + 1个客户卡。

Here is my code :这是我的代码:

Step :步 :

When('I wait for the customer list to be updated', function() {
    return browser.wait(Utils.countElements('.tb-card-item', customerCount + 1), 5000);
});

countElements :计数元素:

module.exports = {
    countElements(css, i) {
        let e = element.all(by.css(css));
        return e.count().then(function(elementCount) {
            console.log('COUNT : ' + elementCount);
            return elementCount === i;
        })
    }
};

My understanding was that the countElement function would be executed until it returns true, so until the number of customer is the right one.我的理解是 countElement 函数将一直执行,直到它返回 true,所以直到客户数量是正确的。 What happens in reality is that it gets executed once and goes straight to the next instruction, failing my tests.实际发生的情况是它被执行一次并直接进入下一条指令,我的测试失败了。

Thanks in advance提前致谢

browser.wait returns Promise object, according to official documentation.根据官方文档, browser.wait返回 Promise 对象。 Basically, there are two options how you can tell cucumber that this particular step is async.基本上,有两种方法可以告诉黄瓜这个特定步骤是异步的。

First, if with done callback首先,如果有done回调

When('I wait for the customer list to be updated', function(done) {
    browser.wait(Utils.countElements('.tb-card-item', customerCount + 1), 5000)
       .then(() => {
            done();
       })
})

Second, with async keyword二、用async关键字

When('I wait for the customer list to be updated', async function() {
    await browser.wait(Utils.countElements('.tb-card-item', customerCount + 1), 5000);
});

But it is not enough just to return promise, like you did, cucumber has no idea how to deal with it.但是仅仅返回 promise 是不够的,像你一样,Cucumber 不知道如何处理它。 You have to tell cucumber that he can't move on, until this promise resolved.你必须告诉黄瓜他不能继续前进,直到这个承诺得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM