This Cypress test visits a registration page, fills in a unique email generated dynamically, enters a password, submits the form, and checks for a success message.
Cypress
describe('User Registration with Dynamic Data', () => {
it('should register a user with a unique email', () => {
const randomValue = Math.random().toString(36).substring(2, 10);
const email = `user_${randomValue}@example.com`;
const password = 'Test@1234';
cy.visit('https://example.com/register');
cy.get('input[name="email"]').type(email);
cy.get('input[name="password"]').type(password);
cy.get('button[type="submit"]').click();
cy.contains('Registration successful').should('be.visible');
});
});
OutputSuccess
Important Notes
Always ensure generated data matches the format expected by the application.
Use dynamic data to avoid test flakiness caused by duplicate entries.
Keep generated data simple and readable for easier debugging.
Summary
Dynamic test data helps tests run independently without conflicts.
Use JavaScript functions like Math.random() and Date.now() to create unique values.
Apply dynamic data in form inputs, API calls, and anywhere unique data is needed.
Practice
(1/5)
1. What is the main benefit of using dynamic test data generation in Cypress tests?
easy
A. It creates unique data for each test run to avoid conflicts.
B. It makes tests run slower by adding delays.
C. It hardcodes values to ensure consistency.
D. It disables assertions to speed up tests.
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 A
Quick Check:
Dynamic data = unique per run [OK]
Hint: Dynamic data means fresh unique values each test run [OK]
Common Mistakes:
Thinking dynamic data slows tests
Confusing dynamic with hardcoded data
Believing dynamic data disables assertions
2. Which of the following is the correct way to generate a random email in Cypress using JavaScript template literals?
easy
A. const email = user${Math.random()}@test.com;
B. const email = 'user' + Math.random() + '@test.com';
C. const email = `user+Math.random()@test.com`;
D. const email = `user${Math.random()}@test.com`;
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 D
Quick Check:
Template literals use backticks and ${} [OK]
Hint: Template literals use backticks and ${expression} [OK]
Common Mistakes:
Using quotes instead of backticks for template literals
A. The username variable should be declared inside the test only.
B. Math.random is missing parentheses, so it is a function reference, not a number.
C. The selector '#username' is invalid and should be '.username'.
D. The click command is missing parentheses.
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 B
Quick Check:
Call Math.random() with () to get number [OK]
Hint: Always call Math.random() with parentheses [OK]
Common Mistakes:
Forgetting parentheses on Math.random
Misreading selector syntax
Thinking click needs extra parentheses
5. You want to generate a dynamic user object with unique email and username for each Cypress test run. Which approach correctly creates this object and uses it in a test?
A. This approach correctly generates unique user data and uses it in the test assertions.
B. This approach fails because Math.random() should not be used for test data.
C. This approach is incorrect because user object must be inside cy.then() callback.
D. This approach is wrong because template literals cannot be used inside objects.
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 A
Quick Check:
Dynamic object with random values used correctly [OK]
Hint: Create dynamic objects before test, then use in commands [OK]
Common Mistakes:
Thinking Math.random() is disallowed for test data
Believing object must be inside cy.then() unnecessarily