Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
Cypress intercept - execute same api multiple times with different payload in same test case

I want to execute the multiple times same API with different payloads, but it's not working.

it('call same api with different payload',()=>{
  cy.intercept('PATCH', `**/user/1`,{name:'thava'} ).as("action1")
  cy.intercept('PATCH', `**/user/1`,{name:'prakash'} ).as("action2")

  cy.visit('../app/intercept-identical.html');

  cy.get("#update-action1").click();
  cy.wait(['@action1']);

  cy.get("#update-action2").click();
  cy.wait(['@action2']); // not working
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When requests are identical, only one intercept routeMatcher will catch all the calls.

To vary the response, one way is to respond with a function

it('responds to identical requests with different responses', () => {

  let requestNo = 0
  const responses = [ { name: 'thava' }, { name:'prakash' } ]

  cy.intercept('PATCH', '**/user/1', (req) => { 
    req.reply(responses[requestNo++])
  }).as('action');

  cy.visit('../app/intercept-identical.html')

  cy.get("#update-action").click()
  cy.wait('@action')
    .its('response.body.name')
    .should('eq', 'thava')                          // passes

  cy.get("#update-action").click()
  cy.wait('@action')
  .its('response.body.name')
  .should('eq', 'prakash')                          // passes
})

Tested with

<body>
  <button id="update-action" onclick="clickhandler()"></button>
  <script>
    function clickhandler() {
      fetch('https://jsonplaceholder.typicode.com/user/1', {
        method: 'PATCH'
      })
      .then(res => res.json())
    }
  </script>
</body>

Note

req.reply(res => { res.send(...) } will not stub the request, it will only modify the response from the live server. If the server targeted is not able to accept 'PATCH', you will get an error.


Overriding intercepts

If you update to the latest version of Cypress, you can simply over-write the intercept.

The last-added intercept will be the one to catch the request.

it('overrides intercepts', () => {

  cy.visit('../app/intercept-identical.html')

  cy.intercept('PATCH', `**/user/1`, { name:'thava' } ).as("action1")
  cy.get("#update-action").click()
  cy.wait('@action1')
    .its('response.body.name')
    .should('eq', 'thava')                          // passes

  cy.intercept('PATCH', `**/user/1`, { name:'prakash' } ).as("action2")
  cy.get("#update-action").click()
  cy.wait('@action2')
    .its('response.body.name')
    .should('eq', 'prakash')                        // passes
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda