我无法在 Protractor 和 Jasmine 中使用 IF 和 EXPECT

I am not able to use IF and EXPECT in Protractor and Jasmine

这是一种情况。

if (await browser.wait(ExpectedConditions.presenceOf(this.collapsedContinent), 500)) {
            await this.helper.click(this.expandCountry);
            await this.helper.click(this.chkcountry);
        } else {
            await this.helper.click(this.chkcountry);
        }

我期待我的期望 return 为真或假,然后执行后续操作。看来我这里做错了。

我知道期望 return 是一个承诺,但无论如何我可以期望 return 是对还是错然后从那里开始?

我接受其他方法,只要它检查元素是否存在。我正在使用异步等待,所以我不想要 doThis.then(doThat);

我完全想避免 .then()

您应该使用 isPresent() 方法来实现以下流程,因为现在 else 条件永远不会发生。

如果 DOM 中没有元素,presenceOf 将在 500 毫秒后失败。

解决方案:

    if (await this.collapsedContinent.isPresent()) {
      await this.helper.click(this.expandCountry);
      await this.helper.click(this.chkcountry);
    } else {
      await this.helper.click(this.chkcountry);
    }

或者,如果您确实需要在检查前等待,browser.wait 不会 return true/false,但您可以使用 .then:

来实现
if (await browser.wait(ExpectedConditions.presenceOf(this.collapsedContinent), 500).then(()=> true, err=> false)) {
    await this.helper.click(this.expandCountry);
    await this.helper.click(this.chkcountry);
} else {
    await this.helper.click(this.chkcountry);
}