0
0
Cypresstesting~15 mins

Parallel execution in Cypress - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality with parallel test execution
Preconditions (3)
Step 1: Open the login page at '/login'
Step 2: Enter 'user1@example.com' in the email input field
Step 3: Enter 'Password1!' in the password input field
Step 4: Click the 'Login' button
Step 5: Verify the URL changes to '/dashboard'
Step 6: Repeat the above steps for 'user2@example.com' with password 'Password2!' and 'user3@example.com' with password 'Password3!' in parallel
✅ Expected Result: All three login tests run in parallel and each user is successfully logged in, verified by the URL '/dashboard'
Automation Requirements - Cypress
Assertions Needed:
Verify URL is '/dashboard' after login
Verify login button is clickable
Verify email and password fields accept input
Best Practices:
Use Cypress commands consistently
Use data-driven testing for multiple users
Configure Cypress to run tests in parallel using CI or Cypress Dashboard
Avoid hardcoded waits; use Cypress built-in retries and assertions
Use semantic selectors (data-cy attributes) for locating elements
Automated Solution
Cypress
/// <reference types="cypress" />

describe('Parallel Login Tests', () => {
  const users = [
    { email: 'user1@example.com', password: 'Password1!' },
    { email: 'user2@example.com', password: 'Password2!' },
    { email: 'user3@example.com', password: 'Password3!' }
  ];

  users.forEach(({ email, password }) => {
    it(`logs in successfully with ${email}`, () => {
      cy.visit('/login');
      cy.get('[data-cy=email-input]').should('be.visible').type(email);
      cy.get('[data-cy=password-input]').should('be.visible').type(password);
      cy.get('[data-cy=login-button]').should('be.enabled').click();
      cy.url().should('include', '/dashboard');
    });
  });
});

This test suite defines three user credentials in an array. For each user, it runs a separate test case that visits the login page, enters the email and password, clicks the login button, and verifies the URL changes to '/dashboard'.

Selectors use data-cy attributes for stability and clarity. Assertions check visibility and enablement before interacting with elements to avoid flaky tests.

Running these tests in parallel is handled by Cypress when configured with the Cypress Dashboard or CI parallelization, so the code is written to support independent test cases that can run concurrently.

Common Mistakes - 3 Pitfalls
Using hardcoded waits like cy.wait(5000) before assertions
{'mistake': "Using CSS selectors that are brittle like '.btn-primary' or element indexes", 'why_bad': 'These selectors can break easily if UI changes, causing tests to fail unnecessarily.', 'correct_approach': 'Use semantic selectors such as data attributes (e.g., data-cy) that are stable and meant for testing.'}
Writing all login tests inside a single it() block
Bonus Challenge

Now add data-driven testing with 3 different user credentials using Cypress fixtures

Show Hint