【问题标题】:promise chaining vs promise.allPromise 链式 vs Promise.all
【发布时间】:2023-03-12 17:05:01
【问题描述】:

我有一个任务是使用量角器输入通知日期 我需要在进入之前清除内容,所以我想出了这个代码

 this.Then(/^I should enter "Notification Date"$/, () => {
    const d = new Date();
    return orderCheckOutPage.pageElements.recipientNotificationDateMonth.clear().then(() => {
        return orderCheckOutPage.pageElements.recipientNotificationDateMonth.sendKeys(d.getMonth() + 1).then(() => {
            return orderCheckOutPage.pageElements.recipientNotificationDateDay.clear().then(() => {
                return orderCheckOutPage.pageElements.recipientNotificationDateDay.sendKeys(d.getDate()).then(() => {
                    return orderCheckOutPage.pageElements.recipientNotificationDateYear.clear().then(() => {
                        return orderCheckOutPage.pageElements.recipientNotificationDateYear.sendKeys(d.getFullYear())
                    })
                })
            })
        })
    })
});

我的朋友告诉我上面的代码可以重构为

const promises = []; promises.push(orderCheckOutPage.pageElements.recipientNotificationDateMonth.clear()); promises.push(orderCheckOutPage.pageElements.recipientNotificationDateMonth.sendKeys(d.getMonth() + 1)); promises.push(orderCheckOutPage.pageElements.recipientNotificationDateDay.clear()); promises.push(orderCheckOutPage.pageElements.recipientNotificationDateDay.sendKeys(d.getDate())); promises.push(orderCheckOutPage.pageElements.recipientNotificationDateYear.clear()); promises.push(orderCheckOutPage.pageElements.recipientNotificationDateYear.sendKeys(d.getFullYear())); return promise.all(promises);

听说promise.all 会开始一一解决promise

首先它会转到第一个语句并尝试解决如果它[在上述情况下是明确的月份]如果是异步的它将跳转到第二个语句并尝试执行语句[将密钥发送到月份]

这里清零和进货都是并行的

任务是根据承诺得到解决的时间来执行的

如果是这种情况,sendkeys 是否有机会在清除之前被执行

如果我错了请纠正我......!!!!!

【问题讨论】:

  • Protractor 有自己的控制流程,可以堆叠和解析每个命令。因此无需链接命令或解析它们。

标签: selenium protractor cucumber angular-promise


【解决方案1】:

Protractor 有自己的 Promise 管理机制,叫做控制流,简单理解控制流,你可以认为它是一个队列。

当 nodejs 逐行执行你的 Protractor 脚本时,如果该行中的表达式返回一个 promise,控制流会将 promise 添加到队列中。

所有行执行完成后,你会得到一个promise队列,此时你的测试还没有完成,因为控制流会让你的测试等待队列中的所有promise被执行。现在控制流将从队列中弹出一个promise并执行并等待它完成,然后是下一个promise。

所以有了这样的机制,你的脚本可以按照你写的顺序执行 在文件中。实际上控制流所做的事情比我这里说的要复杂。

在你的情况下你不需要使用嵌套的then链,你的代码就像回调金字塔,不代表promise的优势(promise是解决回调金字塔)。您的代码可以简单如下:

const d = new Date();
//input month
orderCheckOutPage.pageElements.recipientNotificationDateMonth.clear();
orderCheckOutPage.pageElements.recipientNotificationDateMonth.sendKeys(d.getMonth() + 1);
//input day
orderCheckOutPage.pageElements.recipientNotificationDateDay.clear();
orderCheckOutPage.pageElements.recipientNotificationDateDay.sendKeys(d.getDate());
//input year
orderCheckOutPage.pageElements.recipientNotificationDateYear.clear();
orderCheckOutPage.pageElements.recipientNotificationDateYear.sendKeys(d.getFullYear());

对于您的情况,无需使用 promise.all(),因为您的代码的所有交互都不会从页面中获得一些价值。我将举一个例子来帮助您了解在哪种情况下使用promise.all()更好:

假设我有一个页面,它显示价格和金额。我需要按价格*金额计算费用。

使用嵌套然后链:

var fee = ele_price.getText().then(function(price){

    return ele_amount.getText().then(function(amount){
        return price * amount;
    });
});

fee.then(function(fee){
    console.log(fee);
});

使用 promise.all():

var fee = promise.all([
  ele_price.getText(),
  ele_amount.getText()
])
.then(function(datas){
    var price = datas[0];
    var amount = datas[1];
    return price * amount;
});

fee.then(function(fee){
    console.log(fee);
});

所以使用 promise.all(),一个 then() 就足够了。这使您的代码比嵌套的 then 链更具可读性。

希望您现在明白为什么在您的情况下不需要使用 promise.all()。

【讨论】:

  • 也许要补充一点:甚至不需要为element.clear() 和element.sendKeys() 重复同一行。只需将其一个接一个地链接为element.clear().sendKeys('value')。也可以。
  • 感谢大家的反馈和澄清
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
  • 2018-02-06
  • 2020-04-28
  • 1970-01-01
相关资源
最近更新 更多