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')

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 => {...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With