samedi 19 septembre 2020

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

I am using Testcafe for E2E automation and I want to add some prettier errors (instead of x element not found in DOM). Before each test, I do a clean login to the application, but that might fail (since I run the tests in DEV environment after a successful master build deployment), such as, devs broke the build, a service essential for login is down, the login itself broke and so on.

I want my script to fail right away instead of trying to run all tests and fail with the same reason. I read on stack overflow that I should use a return and that my script should stop with return, that's why I added it in the second catch block (retry login, then fail), but it doesn't seem to do that. What am I doing wrong?

Should I use a try/catch with a return in the loginpage.login or I can achieve what I want to achieve in the before each hook?

Right now, if login fails for whatever reason, the script still attempts to execute all tests. I want the tests to be skipped if the before each hook failed. How can I achieve this?

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 }

The contents of loginPage.login() are:

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

Aucun commentaire:

Enregistrer un commentaire