0

As we know we can set default timeout in cypress and if element not present cypress will wait for that specific time before returning error element not found.

Then why we use cy.wait(). Is there any specific example where we have to use cy.wait()?

Muneeb Akhtar
  • 87
  • 3
  • 11

2 Answers2

3

You should prefer a timeout over a wait, because a timeout will return earlier if the condition is satisfied.

// timeout - up to maximum time
cy.get(selector, {timeout:10_000})
  .should('be.visible')

// wait - fixed time
cy.wait(10_000)
cy.get(selector)
  .should('be.visible')

What is cy.wait(time) good for - if you have a flaky test, you can add a fixed wait temporarily to pinpoint the place where you need additional timeouts.

Thelonious
  • 146
  • 10
1

You're correct, you should use the default command timeout to wait for elements.

The primary use case of cy.wait() is for waiting on intercepted network calls using cy.intercept().

Generally you’d see something like the following, so the test execution won't proceed until a specific network response comes back.

cy.intercept('**/api/my-api').as('api');

// do something to trigger network call

cy.wait('@api').should(/* assert network response here */);

There is another use case for waiting an arbitrary amount of time, although this is an anti-pattern and generally shouldn't be used.

cy.wait(5000); // wait 5 seconds

Source: https://docs.cypress.io/api/commands/wait

DJSDev
  • 812
  • 9
  • 18