We use cy.wait() to pause the test for a set time or until something finishes. This helps make sure the app is ready before the next step.
cy.wait() for explicit waiting in Cypress
cy.wait(timeInMilliseconds) cy.wait(alias)
You can wait for a fixed time by giving milliseconds, like cy.wait(2000) waits 2 seconds.
You can also wait for a network request by using an alias, like cy.wait('@getUsers').
cy.wait(3000)cy.intercept('GET', '/users').as('getUsers') cy.wait('@getUsers')
This test opens a page, waits 2 seconds, then clicks a link and checks the URL changed.
describe('Example of cy.wait()', () => { it('waits for 2 seconds before clicking', () => { cy.visit('https://example.cypress.io') cy.wait(2000) // wait 2 seconds cy.get('a[href="/commands/actions"]').click() cy.url().should('include', '/commands/actions') }) })
Use fixed waits (cy.wait(2000)) sparingly because they slow tests.
Prefer waiting for network calls or elements to appear for faster, reliable tests.
Always give meaningful aliases when waiting for requests to keep tests clear.
cy.wait() pauses tests for a set time or until a request finishes.
Use it to avoid running steps too early and causing errors.
Prefer waiting for events or requests over fixed time waits for better tests.
Practice
cy.wait() in Cypress tests?Solution
Step 1: Understand the function of
cy.wait()cy.wait()is used to pause the test execution either for a fixed time or until a network request completes.Step 2: Compare with other options
Skipping steps, restarting tests, or retrying assertions are not functions ofcy.wait().Final Answer:
To pause the test for a specific time or until a request finishes -> Option AQuick Check:
Pause test = cy.wait() [OK]
- Thinking cy.wait() skips test steps
- Confusing cy.wait() with test retries
- Assuming cy.wait() restarts tests
Solution
Step 1: Check the correct argument type for
The argument must be a number representing milliseconds, so 3000 means 3 seconds.cy.wait()Step 2: Validate each option
cy.wait(3000) uses a number 3000 correctly. cy.wait('3000') uses a string which is invalid. cy.wait(3) uses 3 which means 3 milliseconds, not 3 seconds. cy.wait(3s) uses an invalid unit '3s'.Final Answer:
cy.wait(3000) -> Option DQuick Check:
Milliseconds number = cy.wait(3000) [OK]
- Passing time as string instead of number
- Using seconds instead of milliseconds
- Adding units like 's' inside wait argument
cy.intercept('GET', '/api/data').as('getData')
cy.visit('/dashboard')
cy.wait('@getData')
cy.get('#result').should('be.visible')What will
cy.wait('@getData') do here?Solution
Step 1: Understand alias usage with
The alias '@getData' refers to the intercepted GET request to '/api/data'. Usingcy.wait()cy.wait('@getData')waits for that request to complete.Step 2: Check other options
It does not wait a fixed time (not 1 second), nor does it continue immediately. The alias is correctly defined, so no syntax error.Final Answer:
Pause test until the GET request to '/api/data' finishes -> Option AQuick Check:
Wait for request alias = cy.wait('@getData') [OK]
- Confusing alias wait with fixed time wait
- Assuming immediate continuation
- Mistaking alias syntax causing errors
cy.wait(5000)
cy.get('.submit-btn').click()But the test fails because the button is not clickable after 5 seconds. What is the likely problem?
Solution
Step 1: Analyze the use of fixed wait
Waiting a fixed 5 seconds does not guarantee the button is ready or clickable. The button might appear later or be disabled.Step 2: Consider better waiting strategy
Better to wait for the button to exist or be enabled using assertions or wait for network requests instead of fixed time.Final Answer:
Using fixed wait instead of waiting for the button to be ready -> Option CQuick Check:
Fixed wait ≠ reliable readiness [OK]
- Assuming fixed wait always works
- Blaming selector syntax without checking
- Thinking cy.wait() must come after cy.get()
cy.wait()?Solution
Step 1: Understand how to wait for multiple aliases
Cypress requires chainingcy.wait()calls sequentially for different aliases to ensure both requests complete.Step 2: Evaluate each option
cy.wait('@apiCall1'); cy.wait('@apiCall2') correctly chains the waits using semicolon separation, which queues them in Cypress. cy.wait('@apiCall1', '@apiCall2') passes multiple arguments incorrectly. cy.wait('@apiCall1') && cy.wait('@apiCall2') uses logical AND which does not chain Cypress commands properly. cy.wait(['@apiCall1', '@apiCall2']) uses an unsupported array argument.Final Answer:
cy.wait('@apiCall1'); cy.wait('@apiCall2') -> Option BQuick Check:
Chained waits = cy.wait('@apiCall1'); cy.wait('@apiCall2') [OK]
- Passing multiple aliases as separate arguments
- Using && instead of proper chaining
- Using unsupported array of aliases
Ready to prove it?
Pass the test and this pattern moves unknown
