Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

268
Visualizações
Cypress: login Testing

I'm trying to test a successful login by a few types of users. Some of them have a bit different behavior. I am trying to do this without copy-pasting same parts of code because maintaining them one-by-one (each userType in different file or case) was crazy.

I have my own test-sandbox, so the credentials in files will not put security under attack (I hope?)

  1. Is it OK to try to create more or less dynamically generated tests?
  2. Is my code over-complicated and there is just easier way?
  3. Am I missing something and doing some/everything wrong?
  4. Should I divide last else for one more "if" for all good cases with same behavior and else just for something unexpected?
const credentials = [
{userType: 'UserType1', login: 'UserType1Login', password: 'UserType1Password'},
{userType: 'UserType2', login: 'UserType2Login', password: 'UserType2Password'}
//6 user types and credentials for NonExistingUser and blocked
];

describe('Checks login', () => {

beforeEach('Go to Login Modal', () => {
    cy.visit('/');
    cy.get('[data-cy=loginModalOpen]').click();
  });

// dynamically create a single test for each credential obj in the list
  credentials.forEach(credential => {

  it(`Checks Authorization by ${credential.userType} user`, () => {
      cy.get('[data-cy=login]').type(credential.login);
      cy.get('[data-cy=password]').type(credential.password);
      cy.get('[data-cy=loginSubmit]').click();

      if (credential.userType.includes('blocked')) {
        cy.get('[data-cy=passwordError]').should('contain', 'User blocked');
        cy.url().should('not.include', '/orders/published');

      } else if (credential.userType.startsWith('UserType2')) {
        cy.url().should('include', '/stores');

      } else if (credential.userType.includes('nonExisting')) {
        cy.get('[data-cy=passwordError]').should('contain', 'No user with such login');
        cy.url().should('not.include', '/orders/published');

      } else if (credential.userType.includes('UserType1')) {
        cy.url().should('include', 'orders_all/published');

      } else {
//all other cases. 
        cy.url().should('include', '/orders/published');
      }
// i have some more "it" cases for error messages, but i THINK they are ok. 
}):
});
});

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Even though what you have written will work, I personally do not like this approach. For me as a general rule of thumb is that if you are going to add conditional blocks in your test cases, it is worth splitting the test into smaller test cases. Here is how I would have done this:

const credentials = {
  UserType1: {
    login: 'UserType1Login',
    password: 'UserType1Password',
  },
  UserType2: {
    login: 'UserType2Login',
    password: 'UserType2Password'
  }
};

const logInTheUser = (credential) => {
  cy.get('[data-cy=login]').type(credential.login);
  cy.get('[data-cy=password]').type(credential.password);
  cy.get('[data-cy=loginSubmit]').click();
}

describe('Checks login', () => {

  beforeEach('Log in the user', () => {
    cy.visit('/');
    cy.get('[data-cy=loginModalOpen]').click();
  });

  it('prints error and does not redirect if user is blocked', () => {
    logInUser(credentials.blocked);
    cy.get('[data-cy=passwordError]').should('contain', 'User blocked');
    cy.url().should('not.include', '/orders/published');
  });

  it('redirects user of type 2 to stores page', () => {
    logInUser(credentials.UserType2);
    cy.url().should('include', '/stores');
  });

  it('prints error and does not redirect if user does not exist', () => {
    loginUser(credentials.nonExisting);
    cy.get('[data-cy=passwordError]').should('contain', 'No user with such login');
    cy.url().should('not.include', '/orders/published');
  });

  it('redirects user of type 1 to published orders page', () => {
    loginUser(credentials.UserType1);
    cy.url().should('include', 'orders_all/published');
  });

  // ...other tests
});

As you can see, the test descriptions are also more descriptive and helps the reader to understand at first glance which test failed.

You you can also do the same by storing all the assertion information in (e.g url, passwordError etc) in the objects that you were iterating over but at that point, you might as well just create separate test cases, which is easier to read and understand what's going on.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda