Cypress Intercepted Requests

Profile picture for user arilio666

Using the intercept command, let us try to understand the concept of intercepting requests made by the site.

       cy.intercept('GET', '?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)
  • The intercept command now matches any requests containing the URL '?endpoint=customerchat&page_id=2123438804574660&suppress_http_code=1'.
  • We can target different requests with an intercept that includes POST, GET, etc.
  • For instance, we have used GET here to match the request that only requests with GET should be matched.
cy.intercept({
         'HTTPS': false,
         'method': 'GET',
         'query': {
           'limit': 10
         },
         'path': '?endpoint=customerchat&page_id=2123438804574660&suppress_http_code=1'
       })
        
  • Now it is possible in cypress to get multiple requests with limit using this following intercept method.
  • With intercept, we have to pace the test correctly.
  • Sometimes cypress does not intercept the requests and URLs we provide.
       cy.wait('@searchIntercept').its('response.statusCode').should('eq',200)
  • To wait for the request to be intercepted after visiting, we are using the .wait() command.