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

78
Visualizações
How to repeat a request until getting a different response in Cypress

I'm working with chained requests and need to wait until a condition is met so I can test the response. The prior request will always return a response but I need for its status to be "finished" and sometimes it's "in progress". I don't want to use a static wait for this cy.wait() because that either makes the tests flaky or last forever. How can I do? This is my code so far but it gets stuck and never finishes. With static wait (no loop) it works pretty well.

Any ideas?

it('name of the test', function() {
  cy.processFile().then((response) => {
    let i=0;
    while (i<5){
     cy.wait(1000);
     cy.getInfo().then((resp) => {
     if(resp.body.status !== 'finished'){
       i++;
     } else {
         i = 5;
         expect(.....);
       }
     })
   }
  })
})

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The while (i<5) { statement is synchronous and it runs through the five iterations before the test even starts.

Instead, this should work

function waitForStatus (status, count = 0) {

  if (count === 5) {
    throw 'Never finished'
  }

  return cy.getInfo().then(resp => {
    if (resp.body.status !== status) {
      cy.wait(1000)                       // may not need to wait
      waitForStatus(status, ++count)      // run again, up to 5 times
    } else {
      return resp  // in case you want to expect(resp)...
    }
  })
}

cy.processFile().then((response) => {
  waitForStatus('finished').then(resp => {
    expect(resp)...
  })
})

There is a more detailed info here Writing a Cypress Recursive Function

Your scenario

npm i -D cypress-recurse
# or use Yarn
yarn add -D cypress-recurse
import { recurse } from 'cypress-recurse'

it('waits for status', () => {

  cy.processFile().then((response) => {

    recurse(
      () => cy.getInfo(),
      (resp) => resp.body.status === 'finished',
      {
        log: true,
        limit: 5, // max number of iterations
        timeout: 30000, // time limit in ms
        delay: 1000 // delay before next iteration, ms
      },
    )
    .then(resp => {
      expect(resp)...
    })
})

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