我正在尝试找到一种方法来创建自定义等待某些条件的发生。如下所示:
static waitForActivityToHappen(activityName: string, timeout: number) {
const startTime = PageHelper.getCurrentTime(); // Get current time in milisecond
while ((PageHelper.getCurrentTime() - startTime) < timeout) {
browser.sleep(PageHelper.timeout.xxs); // Poll every 1 sec
<Do some action here>
element.all(By.xpath(xpath)).count().then(function (count) {
if (count > 0) {
<break the loop here>
}
});
}
}
但这不起作用。请告诉我如何实现这一目标。
最佳答案
我会尝试使用 browser.wait 函数,它接受第一个参数作为谓词函数,因此您可以编写任何条件,只要您的谓词函数返回 true/false 或 Promise 将解析为 true/,它就会起作用错误。
static waitForActivityToHappen(activityName:string, timeout: number) {
let waitForActivityToHappenPredicate = function () {
return element.all(By.xpath(xpath)).count().then(function () {
if (count > 0) {
return true
} else {
return false
}
}, function (err) {return false})
}
browser.wait(waitForActivityToHappenPredicate, timeout, 'Some timeout message here')
}
关于javascript - 如何根据 Promise 值包装循环和 Promise 和 Break 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48052885/