Cypress.io: How to use one Login test for different user types?

Viewed 2327

I'm using Cypress.io to test a third party page.

I want to test a login page and have different users. All Users have access to different content. So I have to check if they get the right page. I want to use 1 Test and pass data to this test.

without variables I have this code:

cy.xpath("path)
.type('manager')
cy.xpath("path")
.type('password')
cy.xpath('path for login button')
.click()

How can I start doing this? can I use fixtures for this? if yes, how? can I write my login with variables into the commands to use it in other tests?

1 Answers

I will suggest iterating by [key, value] pairs, like:

describe('Login', () => {
    const userType = new Map([
        ['userX', password],
        ['userY', password],
        ['userZ', password],
    ]);
        for (const [key, value] of userType.entries()) {
            it(`should be logged as ${key}`, () => {
                cy.visit('/login');
                cy.xpath("path).type(key)
                cy.xpath("path").type(value)
                cy.xpath('path_for_login_button').click()
                /* 

                    assert logic here

                 */

                cy.visit('/logout')
        });
    }
});
Related