0
0
Cypresstesting~15 mins

Dynamic test data generation in Cypress - Build an Automation Script

Choose your learning style9 modes available
Automate user registration with dynamic test data
Preconditions (2)
Step 1: Open the registration page at '/register'
Step 2: Generate a random valid email address
Step 3: Generate a random valid username
Step 4: Enter the generated email in the email input field with id 'email'
Step 5: Enter the generated username in the username input field with id 'username'
Step 6: Enter 'Password123!' in the password input field with id 'password'
Step 7: Enter 'Password123!' in the confirm password input field with id 'confirmPassword'
Step 8: Click the submit button with id 'submitBtn'
Step 9: Wait for the success message with class 'success-message' to appear
✅ Expected Result: The success message 'Registration successful' is displayed confirming the user was registered with the dynamic data
Automation Requirements - Cypress
Assertions Needed:
Verify the success message text is exactly 'Registration successful'
Verify the URL changes to '/welcome' after registration
Best Practices:
Use Cypress commands consistently (cy.visit, cy.get, cy.type, cy.click)
Generate dynamic test data inside the test to avoid hardcoded values
Use explicit assertions with .should() for validation
Use semantic and stable selectors (id, class) for locating elements
Avoid using fixed waits; rely on Cypress automatic waits
Automated Solution
Cypress
describe('User Registration with Dynamic Data', () => {
  function generateRandomEmail() {
    const randomPart = Math.random().toString(36).substring(2, 10);
    return `user_${randomPart}@testmail.com`;
  }

  function generateRandomUsername() {
    const randomPart = Math.random().toString(36).substring(2, 8);
    return `user_${randomPart}`;
  }

  it('should register a new user with dynamic data', () => {
    const email = generateRandomEmail();
    const username = generateRandomUsername();
    const password = 'Password123!';

    cy.visit('/register');

    cy.get('#email').type(email);
    cy.get('#username').type(username);
    cy.get('#password').type(password);
    cy.get('#confirmPassword').type(password);

    cy.get('#submitBtn').click();

    cy.get('.success-message')
      .should('be.visible')
      .and('have.text', 'Registration successful');

    cy.url().should('include', '/welcome');
  });
});

This Cypress test automates the user registration process using dynamic test data.

First, two helper functions generate a random email and username to avoid hardcoded values and ensure uniqueness.

The test visits the registration page, fills in the form fields with the generated data and a fixed password, then submits the form.

Assertions check that the success message appears with the exact text and that the URL changes to '/welcome', confirming successful registration.

Selectors use stable IDs and classes for reliability. Cypress commands handle waiting automatically, so no fixed delays are used.

Common Mistakes - 3 Pitfalls
Hardcoding email and username instead of generating dynamically
Using cy.wait() with fixed time instead of relying on automatic waits
Using brittle selectors like XPath or overly complex CSS selectors
Bonus Challenge

Now add data-driven testing with 3 different sets of dynamic user data to register multiple users in one test suite.

Show Hint