Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!