错误处理 - 如果每个挂钩之前的 a 中出现错误,我该如何停止脚本?

Error Handling - How do I stop the script if there's an error in a before each hook?

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

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

我应该在 loginpage.login 中使用 try/catch 和 return 还是我可以在每个挂钩之前实现我想要实现的目标?

现在,如果由于某种原因登录失败,脚本仍会尝试执行所有测试。如果每个挂钩失败之前,我希望跳过测试。我怎样才能做到这一点?

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

目前,没有 public API 使用测试 API 中止测试执行。我在 TestCafe GitHub 存储库中为您的用例创建了一个 suggestion