Home > database >  Cypress - adding retries to page object
Cypress - adding retries to page object

Time:10-13

In order to make future changes easier, we have put the login script in a page object.

//Login.js

export class Login {

username_input=         () => cy.get('#LoginForm_username');
password_input=         () => cy.get('#LoginForm_password');
login_button=           () => cy.contains('Login');
profile=                () => cy.get('.profile-content');

login(email, password){
    this.username_input().type(email);
    this.password_input().type(password);
    this.login_button().click();
    this.profile().should('exist')
        return this;
    }
}

So later we can just reuse it in any spec file

//actual.spec.js

import {Login} from "../../pages/login/Login";

it('logs in', () => {
login.login(Cypress.env('userEmail'), Cypress.env('userPass'))
}

Now our login page has the strange behavior of sometimes not responding. Therefore we added retries.

We have found two ways that work, but both are not ideal:

Solution 1:

Put retries param in the config file

"retries": 2

Why this is not ideal?

This enables retries for every single test, which we don't want. We only want it for the login script.

Solution 2:

Put the retry param in the 'it'

import {Login} from "../../pages/login/Login";

it('logs in', {retries:2} () => {
login.login(Cypress.env('userEmail'), Cypress.env('userPass'))
}

Why this is not ideal?

We have to put the param in every spec file and if we want to change the number of retries or get rid of retries entirely, we need to change it in every single spec file.

Solution 3???

What I am looking for now is a way to put the retry param somewhere in the login functionality in the login.js but I could not find a way to do that.

CodePudding user response:

(Editing the post to include the 2nd workaround...)

The entire idea of cypress is avoiding flaky test. If you have a login that sometimes fails and sometimes doesn't, then you have an unstable environment even before you start.

  1. If all your tests depend on the login then it might not be a bad idea to include the retry at suite level (in the describe) but if only some of your tests rely on the login then you could group them all in only one suite and then add the retry at suite level.

Something like..


describe('Your suite name', {
  retries: {
    runMode: 2,
    openMode: 2,
  }
}, () => {
 
//number of retries will be applied per test
  it('logs in', () => {
    login.login(Cypress.env('userEmail'), Cypress.env('userPass'))
  })
  
  it('other test', () => {
})

})
  1. Do the login through API and that one make it part of the tests that rely on the login (so it doesn't have to go through the UI and you won't get the issue). When testing the login itself then only there, do the retry for that test.

CodePudding user response:

When you write:

Now our login page has the strange behavior of sometimes not responding.

You can rest assured, that your tests are not the problem.

  • Related