Bird
0
0

You need to create multiple test users via API before running UI tests in Cypress. Which code snippet correctly ensures all users are created before visiting the UI page?

hard📝 Application Q8 of 15
Cypress - Test Organization and Patterns
You need to create multiple test users via API before running UI tests in Cypress. Which code snippet correctly ensures all users are created before visiting the UI page?
const users = [{name: 'Tom'}, {name: 'Jerry'}];
// Which approach is correct?
ACall <code>cy.visit('/users')</code> immediately after starting requests without waiting
BUse <code>cy.wrap(users).each(user => cy.request('POST', '/api/users', user))</code> then <code>cy.visit('/users')</code> inside <code>then</code>
CUse <code>users.forEach(user => cy.request('POST', '/api/users', user))</code> then <code>cy.visit('/users')</code>
DCreate users manually in UI before tests instead of API
Step-by-Step Solution
Solution:
  1. Step 1: Understand asynchronous behavior

    Cypress commands like cy.request are asynchronous and must be chained to ensure order.
  2. Step 2: Evaluate options

    Use cy.wrap(users).each(user => cy.request('POST', '/api/users', user)) then cy.visit('/users') inside then uses cy.wrap().each() which properly chains requests and waits for completion before visiting the page.
  3. Step 3: Identify incorrect options

    Call cy.visit('/users') immediately after starting requests without waiting visits page immediately without waiting; Use users.forEach(user => cy.request('POST', '/api/users', user)) then cy.visit('/users') uses forEach which does not wait for promises; Create users manually in UI before tests instead of API ignores API-first setup benefits.
  4. Final Answer:

    Use cy.wrap(users).each(user => cy.request('POST', '/api/users', user)) then cy.visit('/users') inside then -> Option B
  5. Quick Check:

    Chain API calls to ensure completion before UI actions [OK]
Quick Trick: Use cy.wrap().each() to chain async API calls [OK]
Common Mistakes:
  • Using forEach which doesn't wait for async calls
  • Visiting page before API calls complete
  • Skipping API setup and using UI only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes