0
0
Cypresstesting~3 mins

Why cy.wait() for explicit waiting in Cypress? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your tests could wait just the right time, never too early or too late?

The Scenario

Imagine testing a website where you have to wait for a button to appear before clicking it. You keep refreshing the page and clicking randomly, hoping the button is ready.

The Problem

This manual waiting is slow and frustrating. You might click too early and cause errors, or waste time waiting too long. It's hard to know exactly when the page is ready.

The Solution

Using cy.wait() lets you pause the test for a set time or until a condition is met. This means your test waits just the right amount, making it faster and more reliable.

Before vs After
Before
cy.get('button').click(); // might fail if button not ready
After
cy.wait(1000); cy.get('button').click(); // waits 1 second before clicking
What It Enables

It enables tests to run smoothly by waiting exactly when needed, avoiding random failures and saving time.

Real Life Example

When testing a login form, cy.wait() can pause the test until the server responds, ensuring the next step only runs when the page is ready.

Key Takeaways

Manual waiting is slow and error-prone.

cy.wait() pauses tests explicitly for better timing.

This makes tests more reliable and faster.