Dynamic test data generation in Cypress - Build an Automation Script
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.
Now add data-driven testing with 3 different sets of dynamic user data to register multiple users in one test suite.
Practice
Solution
Step 1: Understand dynamic data purpose
Dynamic test data generation creates fresh data each time to prevent clashes with existing data.Step 2: Compare options
Only It creates unique data for each test run to avoid conflicts. describes this benefit correctly; others describe incorrect or unrelated effects.Final Answer:
It creates unique data for each test run to avoid conflicts. -> Option AQuick Check:
Dynamic data = unique per run [OK]
- Thinking dynamic data slows tests
- Confusing dynamic with hardcoded data
- Believing dynamic data disables assertions
Solution
Step 1: Identify correct template literal syntax
Template literals use backticks and ${} to embed expressions, so `user${Math.random()}@test.com` is correct.Step 2: Check other options
const email = 'user' + Math.random() + '@test.com'; uses string concatenation, which works but is not template literals. const email = user${Math.random()}@test.com; misses backticks. const email = `user+Math.random()@test.com`; treats Math.random() as string literal.Final Answer:
const email = `user${Math.random()}@test.com`; -> Option DQuick Check:
Template literals use backticks and ${} [OK]
- Using quotes instead of backticks for template literals
- Forgetting ${} around expressions
- Treating expressions as plain text
const randomId = Math.floor(Math.random() * 1000);
cy.get('#user-input').type(`user${randomId}`);
cy.get('#submit-btn').click();
cy.get('#result').should('contain', `user${randomId}`);What will the assertion check?
Solution
Step 1: Understand variable usage in template literals
The variable randomId is generated once and used in the string `user${randomId}` for typing and assertion.Step 2: Analyze assertion
The assertion checks if '#result' contains the exact string 'user' concatenated with the random number stored in randomId.Final Answer:
That the element with id 'result' contains the exact text 'user' plus the random number generated. -> Option CQuick Check:
Assertion checks full dynamic string [OK]
- Assuming assertion checks only 'user' text
- Thinking random number is checked alone
- Confusing variable name with string literal
const username = 'user' + Math.random;
cy.get('#username').type(username);
cy.get('#submit').click();Solution
Step 1: Check Math.random usage
Math.random is a function and must be called with parentheses to get a number. Without (), username becomes 'user' plus function reference string.Step 2: Verify other parts
Selectors and click command syntax are correct. The main error is missing parentheses on Math.random.Final Answer:
Math.random is missing parentheses, so it is a function reference, not a number. -> Option BQuick Check:
Call Math.random() with () to get number [OK]
- Forgetting parentheses on Math.random
- Misreading selector syntax
- Thinking click needs extra parentheses
const user = {
username: `user${Math.floor(Math.random() * 10000)}`,
email: `email${Math.floor(Math.random() * 10000)}@test.com`
};
cy.visit('/signup');
cy.get('#username').type(user.username);
cy.get('#email').type(user.email);
cy.get('#submit').click();
cy.get('#welcome').should('contain', user.username);Solution
Step 1: Analyze user object creation
The user object uses template literals with Math.floor(Math.random() * 10000) to create unique username and email strings before the test runs.Step 2: Check usage in Cypress commands
The test types the dynamic username and email into inputs, clicks submit, and asserts the welcome message contains the username, which is valid and correct.Final Answer:
This approach correctly generates unique user data and uses it in the test assertions. -> Option AQuick Check:
Dynamic object with random values used correctly [OK]
- Thinking Math.random() is disallowed for test data
- Believing object must be inside cy.then() unnecessarily
- Misunderstanding template literal usage inside objects
Ready to prove it?
Pass the test and this pattern moves unknown
