Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the last intercepted API only

Tags:

cypress

I'm intercepting an API using cy.intercept(), and it appears that this API is being called two times so it gets intercepted two times, with different responses. I want to access the last intercepted one only. how can I do that?

cy.intercept({
    url: `${Cypress.config().apiUrl}/ticket`,
    method: 'GET',
}).as('assignedTicketAPI')

screenshot

like image 540
Haya D Avatar asked Dec 10 '25 18:12

Haya D


1 Answers

There was a recent question here How to match intercept on response, which is about tackling multiple requests on the same intercept.

You can wait twice on the same intercept alias, provided the route matching parameters are the same.

cy.wait('@assignedTicketAPI')
cy.wait('@assignedTicketAPI').then(interception => {...

It's also possible to count requests in the intercept and assign a unique alias to the 2nd one caught - Aliasing individual requests

let intercepted = 0
cy.intercept({
  url: `${Cypress.config().apiUrl}/ticket`,
  method: 'GET',
}, (req) => {
  intercepted = intercepted + 1
  if (intercepted  === 2) {
    req.alias = 'assignedTicketAPI2'
  }
})

cy.wait('@assignedTicketAPI2').then(interception => {...