0

I am trying to intercept an API call twice (with different body) ( with aliasing and using the cypress wait method) WHEN loading a web page. I've noticed that it only calls the second intercept twice. Is there a built-in solution in cypress that we can use to intercept the same api call twice when loading a Webpage?

cy.intercept('POST', '/SomeUrl', {
    statusCode: 200,
    fixture: 'jsonname.json',
    times: 1,
    }).as('alias');
cy.intercept('POST', '/SomeUrl', {
    statusCode: 200,
    fixture: 'jsonname2.json',
    times: 1,
    }).as('alias2');

cy.visit('https://www.url.com')
   .wait('@alias')
   .wait('@alias2')

The Routes output in the Cypress UI shows us this result:

Method URL Stubbed Alias #
POST **/SomeUrl Yes alias -
POST **/SomeUrl Yes alias2 2

2 Answers2

1

You can wait for an array of aliases (https://docs.cypress.io/api/commands/wait#Aliases).

So you could try the following code.

cy.visit('https://www.url.com')
cy.wait(['@alias', '@alias2'])
Nicolas G.
  • 220
  • 4
  • 9
0

It's solved with:

cy.intercept('POST', '/SomeUrl', (req) => {
     if (req.body.Objectstatus === 1) {
     req.reply({
         statusCode: 200,
         fixture: 'alias.json',
     });
}
     if (req.body.Objectstatus  === 1) {
         req.reply({
         statusCode: 200,
         fixture: 'alias2.json',
     });
     }
   }).as('alias');
});