2

I am a beginner with cypress. I've been looking for a way to intercept API calls to at least one of multiple URLs. Let's say a button is clicked and something like this code is executed to check if a list of requests were called :

cy.get('@request1').should('have.been.called').log(`Request was made to 'REQUEST1_URL'`) 
OR 
cy.get('@request2').should('have.been.called').log(`Request was made to ''REQUEST2_URL'`)

I want to check if a request was sent to one url or the other, or both.

Has anyone encountered this problem before ? Any contribution is appreciated. Thanks.

Fody
  • 23,754
  • 3
  • 20
  • 37
ElVincitore
  • 355
  • 2
  • 12
  • Can you explain why after the button is clicked there are two possible API calls? Generally speaking you use wildcard characters in the URL to match. Also `cy.get()` is wrong - you need `cy.wait()`. – SuchAnIgnorantThingToDo-UKR Aug 27 '22 at 00:49
  • @SuchAnIgnorantThingToDo-UKR You can use `cy.get()` with a request when you pair it with the `cy.spy()` like this : `cy.intercept( Cypress.env('REQUEST1_URL') , cy.spy().as('request1') ) ` – ElVincitore Aug 27 '22 at 08:24
  • Also there are two possible api calls, depending on previous user actions, the callback can choose to send a request to URL1 or URL2 depending on the state of the app. I want to intercept either one of these requests. – ElVincitore Aug 27 '22 at 08:30

1 Answers1

0

The URL you use in the intercept should be general enough to catch both calls.

For example if the calls have /api/ in common, this catches both

cy.intercept('**/api/*')   // note wildcards in the URL
  .as('apiRequest')

cy.visit('/')
cy.wait('@apiRequest')

If you have more paths in the url than you need to catch, for example /api/dogs/ /api/cats/ and /api/pigs/, then use a function to weed out the ones you want

cy.intercept('**/api/*', (req) => {
  if (req.url.includes('dogs') || req.url.includes('cats') {   // no pigs
    req.alias = 'dogsOrCats'                                   // set alias
  }
})

cy.visit('/')
cy.wait('@dogsOrCats')

Catching 0, 1, or 2 URLs

This is a bit tricky, if the number of calls isn't known then you have to know within what time frame they would be made.

To catch requests which you are fired fairly quickly by the app

let count = 0;
cy.intercept('**/api/*', (req) => {
  count = count +1;
})

cy.visit('/')
cy.wait(3000)   // wait to see if calls are fired
cy.then(() => {
  cy.wrap(count).should('be.gt', 0)  // 0 calls fails, 1 or 2 passes
})
Fody
  • 23,754
  • 3
  • 20
  • 37
  • Sadly, that wouldn't work in my case because the URLs used in the intercept are completely different – ElVincitore Aug 27 '22 at 08:33
  • Actually there's no such thing as completely different - if you just use `*` it will match everything. Then you just need do the cats and dogs filter on the URL to find the ones you want, and apply the alias dynamically. – Fody Aug 27 '22 at 08:52
  • That's a pretty good idea, I'll give that a shot – ElVincitore Aug 27 '22 at 15:35