以下方法处理事件。
exports.movingTowardsNextInstanceStage = () => {
const nextInstance = manipulations.getDomObject('instance-tab-phases-buttons-next-instance', 'id'); // Next Approval Stage Button
nextInstance.getText().then((text) => {
if (text.includes('WEITERLEITEN AN TL')) {
browser.wait(until.elementToBeClickable(nextInstance), TIMEOUT, 'taking too long to be clickable');
nextInstance.click();
}
}).catch((err) => {
console.error(err);
});
nextInstance.getText().then((text) => {
if (text.includes('WEITERLEITEN AN LSM')) {
browser.wait(until.elementToBeClickable(nextInstance), TIMEOUT, 'taking too long to be clickable');
nextInstance.click();
}
}).catch((err) => {
console.error(err);
});
nextInstance.getText().then((text) => {
if (text.includes('FREIGABE')) {
browser.wait(until.elementToBeClickable(nextInstance), TIMEOUT, 'taking too long to be clickable');
nextInstance.click();
}
}).catch((err) => {
console.error(err);
});
};
背景信息:必须激活一个按钮(nextInstance)三次,才能激活另一个按钮。为了简短起见...按下每个按钮后,会出现一个加载器,一些组件被激活和停用,依此类推,还需要大约2-3秒的时间才能再次单击该按钮。在这三秒钟内,按钮被禁用。
我尝试了其他单击按钮x次的方法。使用Array.forEach,使用setInterval并具有给定的重复次数和延迟时间,设置每个事件之间的隐式等待(单击)(此操作实际有效)。
我想避免使用隐式等待。
我知道我对上下文的定义不是很好,但是查看我的代码和意图,有什么方法可以改进它?
我是否正确理解,'WEITERLEITEN AN LSM'预期会在前一次点击(“ WEITERLEITEN AN TL”之后的点击之后显示)?
[如果是这样,您的代码当前不会在检查第n + 1个文本之前等待第n次单击,因此您需要的是诺言链。 .catch
,就像.then
会返回一个新的Promise,您可以等待。
此外,browser.wait也返回您需要等待的Promise。
所以,类似:
nextInstance.getText().then((text) => {
if (text.includes('WEITERLEITEN AN TL')) {
return browser.wait(until.elementToBeClickable(nextInstance), TIMEOUT, 'taking too long to be clickable')
.then(() => nextInstance.click());
}
}).catch((err) => {
console.error(err);
})
.then(() => nextInstance.getText()).then((text) => {
if (text.includes('WEITERLEITEN AN LSM')) {
return browser.wait(until.elementToBeClickable(nextInstance), TIMEOUT, 'taking too long to be clickable')
.then(() => nextInstance.click());
}
}).catch((err) => {
console.error(err);
});
// ...
但是,如果第n次点击产生的错误会阻止第n + 1次点击,那么您也应该忽略除最后一次.catch
调用之外的所有内容。
您可能会考虑使用异步/等待。它比Promise链接更加直观。