Bird
Raised Fist0
Cypresstesting~20 mins

Dynamic test data generation in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Dynamic Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Cypress test snippet?

Consider this Cypress test code that generates dynamic user data and asserts the username length.

Cypress
const username = `user_${Math.floor(Math.random() * 1000)}`;

it('checks username length', () => {
  expect(username.length).to.be.greaterThan(5);
});
ATest fails because username length is always less than 5
BTest sometimes passes or fails depending on random number
CTest passes because username length is always greater than 5
DTest throws an error due to syntax mistake
Attempts:
2 left
💡 Hint

Think about the minimum length of the username string generated.

❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies a dynamically generated email format?

You generate an email dynamically in Cypress as const email = `test${Date.now()}@example.com`;. Which assertion correctly checks the email format?

Aexpect(email).to.match(/^[\w.-]+@[\w.-]+\.com$/);
Bexpect(email).to.include('@example.com');
Cexpect(email).to.equal('test@example.com');
Dexpect(email).to.have.length(10);
Attempts:
2 left
💡 Hint

Check which option validates the email pattern properly.

🔧 Debug
advanced
2:00remaining
Why does this dynamic data test fail intermittently?

Look at this Cypress test that fills a form with dynamic data:

const userId = Math.floor(Math.random() * 10);
cy.get('#user-id').type(userId.toString());
cy.get('#submit').click();
cy.get('#result').should('contain', `User ID: ${userId}`);

Sometimes the test fails with a mismatch error. Why?

AThe random userId is generated outside the test, causing stale value issues
BThe selector '#user-id' is incorrect and causes failure
CThe test runs too fast and the page does not update in time
DThe userId is a number but should be a string for typing
Attempts:
2 left
💡 Hint

Consider when the random value is generated and how Cypress commands queue.

❓ framework
advanced
2:00remaining
Which Cypress command correctly generates and uses dynamic test data for multiple tests?

You want to generate a unique username for each test run and reuse it in multiple tests. Which approach is best?

AUse <code>beforeEach(() =&gt; { cy.wrap(`user_${Date.now()}`).as('username'); });</code> and access with <code>this.username</code> in tests
BGenerate username in <code>before()</code> hook and save to a variable outside tests
CGenerate username inside each test separately to avoid sharing
DUse <code>Cypress.env('username')</code> set dynamically before tests
Attempts:
2 left
💡 Hint

Think about scope and sharing data between tests in Cypress.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of dynamic test data generation in automated tests?

Choose the best explanation for why dynamic test data generation is important in automated testing.

AIt guarantees tests will never fail due to data issues
BIt makes tests run faster by avoiding hardcoded values
CIt simplifies test code by removing the need for assertions
DIt prevents tests from using stale or duplicate data, improving test reliability and coverage
Attempts:
2 left
💡 Hint

Think about how dynamic data affects test results and maintenance.

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

  1. Step 1: Understand dynamic data purpose

    Dynamic test data generation creates fresh data each time to prevent clashes with existing data.
  2. 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.
  3. Final Answer:

    It creates unique data for each test run to avoid conflicts. -> Option A
  4. 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

  1. Step 1: Identify correct template literal syntax

    Template literals use backticks and ${} to embed expressions, so `user${Math.random()}@test.com` is correct.
  2. 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.
  3. Final Answer:

    const email = `user${Math.random()}@test.com`; -> Option D
  4. 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
  • Forgetting ${} around expressions
  • Treating expressions as plain text
3. Given the Cypress code snippet:
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?
medium
A. That the element with id 'result' contains a random number only.
B. That the element with id 'result' contains the text 'user' only.
C. That the element with id 'result' contains the exact text 'user' plus the random number generated.
D. That the element with id 'result' contains the string 'randomId'.

Solution

  1. 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.
  2. Step 2: Analyze assertion

    The assertion checks if '#result' contains the exact string 'user' concatenated with the random number stored in randomId.
  3. Final Answer:

    That the element with id 'result' contains the exact text 'user' plus the random number generated. -> Option C
  4. Quick Check:

    Assertion checks full dynamic string [OK]
Hint: Assertions check exact dynamic string used in test [OK]
Common Mistakes:
  • Assuming assertion checks only 'user' text
  • Thinking random number is checked alone
  • Confusing variable name with string literal
4. Identify the error in this Cypress test code that tries to generate a random username:
const username = 'user' + Math.random;
cy.get('#username').type(username);
cy.get('#submit').click();
medium
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

  1. 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.
  2. Step 2: Verify other parts

    Selectors and click command syntax are correct. The main error is missing parentheses on Math.random.
  3. Final Answer:

    Math.random is missing parentheses, so it is a function reference, not a number. -> Option B
  4. 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?
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);
hard
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

  1. 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.
  2. 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.
  3. Final Answer:

    This approach correctly generates unique user data and uses it in the test assertions. -> Option A
  4. 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
  • Misunderstanding template literal usage inside objects
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard