错误处理-如果每个挂钩前的a中存在错误,如何停止脚本?

问题描述

我正在使用testcafe进行E2E自动化,我想添加一些更漂亮的错误(而不是在DOM中找不到x元素)。在每次测试之前,我都会对应用程序进行干净的登录,但是这可能会失败(因为在成功完成主构建部署之后,我在DEV环境中运行了测试),例如,开发人员破坏了构建,登录所必需的服务已关闭登录本身中断,依此类推。

我希望我的脚本立即失败,而不是尝试运行所有测试并因相同的原因失败。我在堆栈溢出中读到,我应该使用return,并且脚本应该以return停止,这就是为什么我在第二个catch块中添加它(重试登录,然后失败)的原因,但是似乎没有这样做。我在做什么错了?

我应该在tryloglog.login中使用try / catch并返回一个返回值,还是可以在每个钩子之前实现我想实现的目标?

现在,如果由于某种原因登录失败,脚本仍将尝试执行所有测试。如果每个挂钩之前失败,我希望跳过测试。我该如何实现?

fixture `Activity History Tests`
.page `https://mypage.com`
.beforeEach(async (t) => {
    try {
        await loginPage.login(username,password) 
    }
    catch(e) { 
        try {
            console.log('Login Failed,retrying. Failure reason: ')
            await loginPage.login(username,password)
        }
        catch(e) {
            throw new Error('Couldn\'t login')
            return

        }
    }}
)


test('Test #1: do something,async (t) => {
    await t.setTestSpeed(0.8)
    try { //the test goes here }

loginPage.login()的内容为:

  async login(username: string,password: string) {

await t
  .setTestSpeed(0.5)
  .maximizeWindow()
  .click(this.loginButton)
  await t
  .typeText(this.emailField,username,{paste:true})
  .click(this.submit)
  await this.passwordField.visible
  await t
  .typeText(this.passwordField,password)
  .click(this.signInButton)
 // await t
  //.click(this.staySignedIn)
  const breakPanelText = await this.breakPanel
  await t
  .expect(breakPanelText.innerText).notContains("Hello Agent",{timeout: 10000})
};

解决方法

目前,没有公共API可以终止使用Test API的测试执行。我已经在TestCafe GitHub存储库中为您的用例创建了一个suggestion