Sometimes your test needs to pause until a web request finishes. This helps make sure the page is ready before checking things.
Waiting for requests (cy.wait with alias) in Cypress
cy.intercept('GET', '/api/data').as('getData') cy.visit('/page') cy.wait('@getData')
You first create an alias for the request using cy.intercept() and .as().
Then you use cy.wait() with the alias name prefixed by @ to pause until the request finishes.
cy.intercept('POST', '/login').as('loginRequest') cy.get('button[type=submit]').click() cy.wait('@loginRequest')
cy.intercept('GET', '/users').as('getUsers') cy.visit('/users') cy.wait('@getUsers')
cy.intercept('GET', '/products/*').as('getProduct') cy.get('.product-link').first().click() cy.wait('@getProduct')
This test waits for the GET request to '/api/items' to finish before checking that the page shows some items.
describe('Wait for API request example', () => { it('waits for data to load before checking', () => { cy.intercept('GET', '/api/items').as('getItems') cy.visit('/items') cy.wait('@getItems') cy.get('.item').should('have.length.greaterThan', 0) }) })
Always create the alias before the action that triggers the request.
You can wait for multiple requests by passing an array of aliases to cy.wait().
If the request fails or times out, the test will fail, helping catch issues early.
Use cy.intercept() with .as() to name requests.
Use cy.wait() with the alias to pause until the request finishes.
This makes tests more reliable by syncing with network activity.
Practice
cy.wait() with an alias in Cypress tests?Solution
Step 1: Understand
Thecy.wait()with aliascy.wait()command pauses the test until the network request with the given alias completes.Step 2: Purpose of waiting for requests
This ensures the test runs only after the needed data is loaded, making tests reliable.Final Answer:
To pause the test until a specific network request finishes -> Option BQuick Check:
Waiting for request = pause test until done [OK]
- Thinking cy.wait skips steps
- Confusing cy.wait with page reload
- Using cy.wait without alias
Solution
Step 1: Identify correct alias method
In Cypress,cy.intercept()is used to spy or stub requests, and.as()assigns an alias.Step 2: Check syntax correctness
cy.intercept('/api/data').as('getData') usescy.intercept('/api/data').as('getData'), which is the correct syntax.Final Answer:
cy.intercept('/api/data').as('getData') -> Option DQuick Check:
Alias uses .as() after intercept [OK]
- Using .alias() instead of .as()
- Trying to alias cy.wait instead of cy.intercept
- Using deprecated cy.route instead of cy.intercept
cy.intercept('GET', '/users').as('getUsers')
cy.visit('/users')
cy.wait('@getUsers').then(({ response }) => {
console.log(response.status)
})Solution
Step 1: Understand intercept and wait behavior
Thecy.intercept()spies on the GET request to '/users' and aliases it as 'getUsers'.Step 2: Analyze the wait and response
cy.wait('@getUsers')waits for the request to finish, then logs theresponse.status. A successful GET usually returns 200.Final Answer:
200 -> Option AQuick Check:
Wait logs response status 200 [OK]
- Confusing request duration with status code
- Expecting wait to return undefined
- Assuming 404 if request is slow
cy.intercept('POST', '/login').as('loginRequest')
cy.get('button#submit').click()
cy.wait('loginRequest')Solution
Step 1: Check cy.wait usage
Thecy.wait()command requires the alias to be prefixed with '@' to identify the request alias.Step 2: Identify the error
The code usescy.wait('loginRequest')missing the '@', which causes Cypress to not recognize the alias.Final Answer:
Missing @ symbol in cy.wait alias -> Option AQuick Check:
Alias in cy.wait needs '@' prefix [OK]
- Omitting '@' before alias in cy.wait
- Using wrong HTTP method in intercept
- Thinking quotes affect alias recognition
Solution
Step 1: Setup intercept before action
You must callcy.intercept()and alias before triggering the request by clicking the button.Step 2: Wait for request before assertions
Usecy.wait('@submitRequest')after clicking to pause until the POST completes, then check page content.Final Answer:
Intercept first, click, wait, then assert success message -> Option CQuick Check:
Intercept before action, wait after click [OK]
- Waiting before intercepting
- Asserting before request finishes
- Interception after click triggers request
Ready to prove it?
Pass the test and this pattern moves unknown
