Waiting Strategies: cy.wait() vs Automatic Retries
You'll learn when to let Cypress handle waiting automatically versus when explicit waits are necessary.
Cypress has a built-in superpower: it automatically retries commands until they succeed (up to a default timeout of 4 seconds). This means most of the time, you don't need to add manual waits—Cypress will keep checking if an element exists, is visible, or meets your assertion criteria.
Automatic Retries: The Default Behavior
When you write cy.get('.submit-button').click(), Cypress doesn't just check once and fail. It continuously queries the DOM, waiting for the button to appear and become clickable. This automatic retry mechanism handles:
- Elements that appear after asynchronous operations
- DOM updates from React state changes
- Elements that need time to become visible or enabled
Think of it like a patient friend who keeps knocking on a door until someone answers, rather than giving up after one try.
When to Use cy.wait()
Explicit waits with cy.wait() are necessary in specific scenarios:
Network Timing
When you've stubbed a network request with cy.intercept() and aliased it:
cy.intercept('GET', '/api/users', { fixture: 'users.json' }).as('getUsers');
cy.visit('/dashboard');
cy.wait('@getUsers'); // Ensures the request completed
cy.get('.user-list').should('be.visible');
Animation Timing
When testing animations or transitions where the element is present but still animating:
cy.get('.modal').should('be.visible');
cy.wait(300); // Let the slide-in animation complete
cy.get('.modal-content').click(); // Now safe to interact
Key Takeaway: Trust Cypress's automatic retries for DOM queries and assertions—they handle most async scenarios. Use cy.wait() only for explicit network request synchronization or waiting out animations that could interfere with interactions.