Cypress Intercept: Waiting on a request

Profile picture for user arilio666

Cypress can wait on an intercepted request to give out what we request. This can be passed within .wait() with the intercept alias.

cy.intercept('http://programsbuzz.com').as('url')
//
cy.wait('@url')
  • Once the request to the 'url' alias responds .wait() will resolve.
cy.wait('@url').then((interception) => {
//
})
  • We can use .wait() on an intercepted alias and, using .then(), yield an object and play around with the response.
cy.intercept({
 url: 'http://programsbuzz.com',
 query: { q: 'Topics' },
}).as('search')
//
cy.wait('@search')
  • .wait() can also be used up with route matcher.
    Once the query with the expected term shows up .wait() will resolve.

Example:

       cy.intercept('?endpoint=customerchat&page_id=2123438804574660&suppress_http_code=1').as('searchIntercept')
       cy.visit('http://www.autopract.com/#/home/fashion')
       cy.get("button[aria-label='Close'] ").click()
       cy.get('img[src="assets/images/icon/cart.png"]').click()
     
       cy.wait('@searchIntercept').its('response.statusCode').should('eq',200)
  • Autopract site is used to intercept a specific endpoint.
  • We have used .wait() to assert statusCode.
  • We can see .wait() has resolved after the status code returns 200 as expected.