Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to have different assertion for a single test running multiple times

While testing to log in using 4 different users, I need to use 4 different assertions and each assertation should only be associated with its particular user.

Example.

  • User 1 I need to verify a certain URL.
  • User 2 should show an error1 message.
  • User 3 should show an error2 message.
  • User 4 should show an error3 message.

In the below test I am getting an error as Cypress is looking for an error1 message for user 1
When the test runs with the first user.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

it('Login with multiple users', () => {
        cy.readFile('cypress/fixtures/userdata.json').then((users) => {
          users.forEach((user) => {
            cy.visit('/')
            cy.get('[data-test="username"]').type(user.username)
            cy.get('[data-test="password"]').type(user.password)
            cy.get('[data-test="login-button"]').click()
            cy.url('https://www.saucedemo.com/inventory.html')
            cy.get('[data-test="error"]').should('have.text', user.error1)
            cy.get('[data-test="error"]').should('have.text', user.error2)
            cy.get('[data-test="error"]').should('have.text', user.error3)
            
          })
        })
      })

>Solution :

In your JSON you can add an extra field errorMessage and url.

[
  {
    "id": "standard User",
    "username": "standard_user",
    "password": "secret_sauce",
    "url": "https://www.saucedemo.com/inventory.html"
  },
  {
    "id": "locked out user",
    "username": "locked_out_user",
    "password": "secret_sauce",
    "errorMessage": "error 2"
  },
  {
    "id": "Problem user",
    "username": "problem_user",
    "password": "secret_sauce",
    "errorMessage": "error 3"
  },
  {
    "id": "perfromance glitch user",
    "username": "performance_glitch_user",
    "password": "secret_sauce",
    "errorMessage": "error 4"
  },
  {
    "id": "Invalid User",
    "username": "perform",
    "password": "secret12",
    "errorMessage": "error 5"
  }
]

You test should be like this:

it('Login with multiple users', () => {
  cy.readFile('users.json').then((users) => {
    users.forEach((user) => {
      cy.visit('/')
      cy.get('[data-test="username"]').type(user.username)
      cy.get('[data-test="password"]').type(user.password)
      cy.get('[data-test="login-button"]').click()
      if (user.username == 'standard_user') {
        cy.url().should('eq', user.url)
      } else {
        cy.get('[data-test="error"]').should('have.text', user.errorMessage)
      }
    })
  })
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading