这是我的功能,我想在条件满足时中断厕所,但出现错误:
SyntaxError:非法的break语句
我使用带有javascript的量角器。
async CompareTableData(byElement, param) {
try {
await this.WaitToBeClickable(element(By.cssContainingText(byElement.value, param)));
await element.all(byElement).then(async function(item) {
for (var i = 0; i < item.length; i++) {
item[i].getText().then(async function(text) {
var trimTxt = text.trim();
if (await trimTxt == param && byElement.using == "css selector") {
console.log(`Param FOUND! ${param}\n`);
await expect(element(By.cssContainingText(byElement.value, param)).isPresent()).toBe(true);
break;
} else {
return;
}
});
}
});
} catch (err) {
console.log("Table comparison FAILED, element not present!");
return console.log(err);
}
};
答案 0 :(得分:0)
正如其他人指出的那样,中断不是在循环中,而是在.then
中的匿名函数中。除此之外,主要问题是您没有很好地履行诺言。引入Async/await
是为了简化承诺,因为它不需要您使用.then
语句,因此您绝对不应该以这种方式一起使用它们。
另外expect
条语句是同步的,因此不需要等待,但是使用Protractor时,期望范围内的动作(几乎总是)是异步的,因此该语句应读为expect(await element(By.cssContainingText(byElement.value, param)).isPresent()).toBe(true);
您可以这样重写代码:
async function CompareTableData(byElement, param) {
try {
await this.WaitToBeClickable(element(By.cssContainingText(byElement.value, param)));
const item = await element.all(byElement)
for (var i = 0; i < item.length; i++) {
const text = await item[i].getText();
var trimTxt = text.trim();
if (trimTxt == param && byElement.using == "css selector") {
console.log(`Param FOUND! ${param}\n`);
expect(await element(By.cssContainingText(byElement.value, param)).isPresent()).toBe(true);
break;
} else {
return;
}
}
} catch (err) {
console.log("Table comparison FAILED, element not present!");
return console.log(err);
}
};