Skip to content
Advertisement

How can I wait for a request to load in Cypress?

I’m writing automate test on Cypress and I want to wait for a page to load. There is a request named “Availability” that if it passes with statusCode:200 then the page loads.

I’ve tried this, but it didn’t work:

cy.intercept('POST' , '*/api/Availability/*').as('availability') 

cy.wait('@availability').its('response.statusCode').should('eq' , 200)

How can I do this?

Advertisement

Answer

Where is your page visit?

If it’s here, it won’t work

cy.visit(...)
cy.intercept('POST' , '*/api/Availability/*').as('availability') 
cy.wait('@availability').its('response.statusCode').should('eq', 200)

You need to set up the intercept before the app sends the api request.

cy.intercept('POST' , '*/api/Availability/*').as('availability') 
cy.visit(...)
cy.wait('@availability').its('response.statusCode').should('eq', 200)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement