如何在 cypress 中延迟重试 cy.request 和重试限制

use*_*513 3 cypress

我正在尝试通过重试实现类似于赛普拉斯请求的目标- 尽管该帖子的评论表明该解决方案不再有效/从未有效?!

无论如何,我需要向弹性服务器发送请求,如果没有返回记录,请稍等一下,然后重试(每隔几(10)秒重试一次,最多可以重试2分钟)...即类似:

cy.request({auth,method,body,url...}).then(reqresult => {
  if (reqresult.body.hits.hits.length){
    // results are in elastic, return them
  }
  else {
    cy.wait(10000)
    // repeat the request, if retry attempts not exceeded, or return the empty response if exceeded
  }
})
Run Code Online (Sandbox Code Playgroud)

但我尝试过的所有操作(包括类似于上面的帖子)要么产生了“混合异步与同步”错误,要么只是循环而没有重新发送请求,或者最坏的情况是导致我的 Cypress (10.3.0) 窗口彻底挂了!

有人可以提供任何帮助吗?

Fod*_*ody 5

您在寻找请求轮询吗?

function req (retries = 0) {

  if (retries > 10) throw new Error('Request failed');   // for limit

  cy.request(...)
    .then((resp) => {
      // if we got what we wanted

      if (resp.status === 200 && resp.body.ok === true)
        // break out of the recursive loop
        return

      // else recurse
      cy.wait(10000)            // for delay
        .then(() => req())      // queue the next call
    })
}

req()   // initial call
Run Code Online (Sandbox Code Playgroud)