Protractor . ispresent()停止循环



我试图循环单击下拉选择,直到一个元素存在。即使控制台记录"找到的数据"。循环没有停止

while (i < 23) {
//select dropdown selection
let selectDeviceid = lists.get(i);
deviceLogsPage.selectDevice();
//click on the first dropdown
selectDeviceid.click();
//click apply button
deviceLogsPage.clickApply();
let bool = deviceContent.isPresent().then(function (isDisplayed) {
if (isDisplayed) {
console.log("found data");
return true;
//found and stop the loop
} else {
console.log("no data");
return false;
}
});
console.log(bool);
if (bool === true) {
break;
}
i++;
}

因为你正在使用异步代码和承诺…bool将是一个承诺…没有真假,只有承诺

你可以使用async/await

并不一定要在Async - IIFE中——如果这段代码已经在一个函数中,只需将该函数设置为async

(async() => {
while (i < 23) {
let selectDeviceid = lists.get(i);
deviceLogsPage.selectDevice();
selectDeviceid.click();
deviceLogsPage.clickApply();
let bool = await deviceContent.isPresent();
if (bool) {
console.log("found data");
break;
} else {
console.log("no data");
}
i++;
}
)();

更简单的方法是将bool作为while条件的一部分

let bool;
while (i < 23 && !bool) {
let selectDeviceid = lists.get(i);
deviceLogsPage.selectDevice();
selectDeviceid.click();
deviceLogsPage.clickApply();
bool = await deviceContent.isPresent();
i++;
}

最新更新