我正在使用Spectron 3.8.0,并试图检查DOM中是否存在元素。我尝试将waitUntil
方法与try/catch
结合使用,但未按预期工作。最近,如果元素存在,我得到了同步app.client.isExisting()
返回true,但是否则它会被卡住并抛出超时异常(mocha)。
代码如下:
@log
protected async isExisting(element: string, name?: string): Promise<boolean> {
await this.app.client.isExisting(element)
.then(data => {
const isExisting = data;
console.log(CONSOLE_COLORS.YELLOW, "IS EXISTING???", isExisting);
return isExisting;
})
.catch(e => {
console.log(CONSOLE_COLORS.RED, "no existing elem")
return false;
});
}
最佳答案
isExisting应该可以正常工作。
您应该正确返回 promise
return app.client.isExisting('#element');
这像 Spark 一样工作
如果给定选择器至少存在一个元素,则返回true。否则返回false
等待元素,请使用以下内容
doesexist(app, element) {
return app.client.waitforExist(element,60 * 1000);
}
只需通过需要检查的应用程序和元素即可。避免等待的更简洁方法
如果元素在60秒之前存在,则返回true;如果在60秒之后不存在错误提示,则返回true
关于javascript - Spectron异步isExisting不会返回false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50183805/