typescript - 如果发生错误,请重试登录
问题描述
我有一些测试偶尔会失败,因为我们认为是一些网络中断,所以我改变了我的登录方法,在显示错误后重试登录。我希望它也向控制台输出它必须重试的次数。这是解决这个问题的最好方法吗?
login(email: string, password: string) {
let count = 0
if (!this.errorDisplayed()) {
this.setEmail(email)
this.setPassword(password)
return this.clickSignIn()
} while (this.errorDisplayed() && count < 5) {
browser.refresh()
count ++
}
console.log(`The login had errors this many ${count} times`)
}
我还尝试了以下方法:
login(email: string, password: string) {
this.setEmail(email)
this.setPassword(password)
this.clickSignIn()
let count = 0
this.errorDisplayed().then(result => {
if (!result) {
console.log(`No error was displayed`)
} while (result && count < 5) {
browser.refresh()
count ++
}
})
console.log(`The login had errors this many ${count} times`)
}
解决方案
这是我想出的解决方案:
login(email: string, password: string) {
let count = 0
let maxTries = 5
while(count <= maxTries) {
try {
this.setEmail(email)
this.setPassword(password)
return this.clickSignIn()
} catch (e) {
console.log(`Could not login because of: ${e.message}`)
browser.refresh()
if (++count === maxTries) {
throw new Error('Failed to login after 5 tries')
}
}
}
}
推荐阅读
- python - 具有多个输入的 Keras 顺序模型,Tensorflow 1.9.0
- php - 如何使用更新查询更新 wordpress 中的表值?
- r - 在分析三个数据帧的三个变量的“cor.test”命令中观察到错误
- docker - Go 可执行文件是如何在 Docker 中创建的?
- android - 绘制最短路径以在多个标记之间导航的解决方案
- sql - MyBatis Mapper 使用 DB2 在一种方法中包含多个 SQL
- ada - 即使在程序结束时断言了相同的条件并且为真,程序上的后置条件也不能证明
- android - 我可以在 android webview 上使用 html5 缓存清单吗?
- javascript - 我正在尝试使用 fetch api 显示数据,但它没有显示任何内容
- reactjs - jwt过期控制和重定向登录页面