jeudi 12 novembre 2020

Cypress - Want to login in once, then run two "it" statements and logout, seems to log me out after the first test

I want to setup tests in which before the test it goes off to my api gets the auth token, seeds a user, then logs user in using those credentials.

I only want to seed the user once before tests are ran.

This part works, however if I have multiple tests within my describe() it doesn't run the second it() statement correctly as it think's i'm logged out it seems. Although in the test runner i can't see its' ran the after() function yet to log out ?

Code:

describe('Create User Validation- Required & Passwords', () => {
    before(() => {
        //seed the user on api and return user.
        seedUser({ 'roles': `admin` }).then((user) => {
            cy.login(user.Name, user.Password);
        }, (errorMsg) => {
            throw new Error(errorMsg);
        });
    });

after(() => {
    cy.logout();
});

it('Shows relevant validation messages when user form fails validation', () => {
    navigate(usersRoute); //custom function to navigate links
    cy.get(adminButtons.create).click();
    cy.get(adminButtons.submit).click();

    cy.get(formFields.Name).closest('.form-group').should('have.class', 'has-error');
    cy.get(formFields.emailAddress).closest('.form-group').should('have.class', 'has-error');
    cy.get(formFields.password).closest('.form-group').should('have.class', 'has-error');;
    cy.get(formFields.defaultUnit).closest('.form-group').should('have.class', 'has-error');;

});


it('Validation error message when confirm password does not match password field', () => {
    navigate(usersRoute);
    cy.get(adminButtons.create).click();
    cy.get(formFields.password).type(`password`);
    cy.get(formFields.confirmPassword).clear().type('invalidText');
    cy.get(adminButtons.submit).click();
    cy.get(formFields.confirmPassword).closest('.form-group').should('have.class', 'has-error');

});

});

// Seed function

export const seedUser = async (userOverrides) => {
    return new Cypress.Promise((resolve, reject) => {
        cy.getAuthToken();
        cy.createUser(userOverrides).then((response) => {
            if (response != null) {
                return response;
            }
            else {
                reject("Error Occured seeding the user");
            }
        });
    })

}

Aucun commentaire:

Enregistrer un commentaire