【发布时间】: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