Instead of going through the hassle of logging in with one user, then logging out and repeating the process with different users for each test, I want to streamline the process by testing login using multiple users within a single test.
Here is the JSON data that will be used to fetch information:
[
{
"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"
}
]
Below is the script that demonstrates how to log in with multiple users:
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.')
});
});