I want to test the login using multiple users in a single test rather than logging in with one then logging out and then moving to a duplicate test and using different users.
This is the JSON i will be using to fetch data
[
{
"id": "standard User",
"username": "standard_user",
"password": "secret_sauce"
},
{
"id": "locked out user",
"username": "locked_out_user",
"isComplete": "secret_sauce"
},
{
"id": "Problem user",
"username": "problem_user",
"password": "secret_sauce"
},
{
"id": "perfromance glitch user",
"username": "performance_glitch_user",
"password": "secret_sauce"
},
{
"id": "Invalid User",
"username": "perform",
"password": "secret12"
}
]
And below is the script
it('Login with multple users', () => {
//Logging in with the first User.
cy.visit('/')
cy.get('[data-test="username"]').type(user[0].username)
cy.get('[data-test="password"]').type(user[0].password)
cy.get('[data-test="login-button"]').click()
cy.url('https://www.saucedemo.com/inventory.html')
// Logging in with the second User.
cy.visit('/')
cy.get('[data-test="username"]').type(user[1].username)
cy.get('[data-test="password"]').type(user[1].password)
cy.get('[data-test="login-button"]').click()
cy.get('[data-test="error"]').should('have.text', 'Epic sadface: Sorry, this user has been locked out.')
});
});
You can do something like this:
it('Login with multiple users', () => {
cy.readFile('cypress/fixtures/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()
cy.url('https://www.saucedemo.com/inventory.html')
cy.get('[data-test="error"]').should(
'have.text',
'Epic sadface: Sorry, this user has been locked out.'
)
})
})
})
Also in your JSON, in the second entry there is no password field instead there is isComplete.