首页 > 解决方案 > 如果发生错误,请重试登录

问题描述

我有一些测试偶尔会失败,因为我们认为是一些网络中断,所以我改变了我的登录方法,在显示错误后重试登录。我希望它也向控制台输出它必须重试的次数。这是解决这个问题的最好方法吗?

    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`)
}

标签: typescriptprotractor

解决方案


这是我想出的解决方案:

    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')
            }
        }
    }
}

推荐阅读