在柏树自定义comand中等待响应值更改

发布于 2025-01-18 19:17:05 字数 446 浏览 3 评论 0原文

直到从“等待”变为“运行”中的响应变化,对轮询响应的最佳解决方案是什么。因此,递归功能不是解决方案,因为我不想发送超过1个请求。

Cypress.Commands.add('createService', () => {
  const query = '';
  cy.request({
   method: 'POST',
      url: 'http://localhost:3000/create',
      body: {
        query
      }
  }).then((response){
     //before RUNNING status is WAITING
    expect(response.body.serviceStatus).to.eql('RUNNING');
  });
});

What is the best solution for polling response until value in response change from 'WAITING' to 'RUNNING'. So recursive function is not solution, because I don't want to send more than 1 request.

Cypress.Commands.add('createService', () => {
  const query = '';
  cy.request({
   method: 'POST',
      url: 'http://localhost:3000/create',
      body: {
        query
      }
  }).then((response){
     //before RUNNING status is WAITING
    expect(response.body.serviceStatus).to.eql('RUNNING');
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦年海沫深 2025-01-25 19:17:06

HTTP 仅是单个响应,但原始 POST 可以为您提供状态 202 和要轮询的标头。

const poll = (url, attempts = 0) => {
  if (attempts > 20) throw 'Too many attempts'

  cy.request(url).then(response => {
    if (response.body.serviceStatus !== "RUNNING") {
      cy.wait(1000)  // throttle if desired
      poll(url, ++attempts)
  })
}

cy.request({
  method: 'POST',
  url: 'http://localhost:3000/create',
  body: {
    query
  }
}).then((response){

  if (response.status === 202) {
    poll(response.headers.location)
  } else {
    expect(response.body.serviceStatus).to.eq('RUNNING')
  }
})

HTTP is only single response, but the original POST can give you a status of 202 and a header to poll.

const poll = (url, attempts = 0) => {
  if (attempts > 20) throw 'Too many attempts'

  cy.request(url).then(response => {
    if (response.body.serviceStatus !== "RUNNING") {
      cy.wait(1000)  // throttle if desired
      poll(url, ++attempts)
  })
}

cy.request({
  method: 'POST',
  url: 'http://localhost:3000/create',
  body: {
    query
  }
}).then((response){

  if (response.status === 202) {
    poll(response.headers.location)
  } else {
    expect(response.body.serviceStatus).to.eq('RUNNING')
  }
})
当梦初醒 2025-01-25 19:17:06

您应该使用截距/等待组合:

cy.intercept({
    method: 'POST',
    hostname: 'http://localhost:3000/create'
}).as('checkStatus');


cy.wait('@checkStatus').then((interception) => {
    expect(interception.response.body.serviceStatus).to.be.eq('RUNNING');
})

无论您提出单个请求还是提出多个请求,上面都应该工作,因为CY.Wait命令将拦截“ CheckStatus”拦截功能中定义的任何呼叫。

You should use an intercept/wait combination:

cy.intercept({
    method: 'POST',
    hostname: 'http://localhost:3000/create'
}).as('checkStatus');


cy.wait('@checkStatus').then((interception) => {
    expect(interception.response.body.serviceStatus).to.be.eq('RUNNING');
})

Above should work whether you make a single request or you make multiple requests, since the cy.wait command will intercept any call defined in the 'checkStatus' intercept function.

私藏温柔 2025-01-25 19:17:05

您可以根据您的情况使用 cypress-recurse

import { recurse } from 'cypress-recurse'

recurse (
  cy.request({
     method: 'POST',
     url: 'http://localhost:3000/create',
     body: { query }
  }), // actions you want to iterate
  response.body.serviceStatus == 'RUNNING', // until this condition is satisfied  
  { // options to pass along
      log: true,
      limit: 50, // max number of iterations
      timeout: 30000, // time limit in ms
      delay: 300, // delay before next iteration, ms
  }
)

You can use cypress-recurse for your situation.

import { recurse } from 'cypress-recurse'

recurse (
  cy.request({
     method: 'POST',
     url: 'http://localhost:3000/create',
     body: { query }
  }), // actions you want to iterate
  response.body.serviceStatus == 'RUNNING', // until this condition is satisfied  
  { // options to pass along
      log: true,
      limit: 50, // max number of iterations
      timeout: 30000, // time limit in ms
      delay: 300, // delay before next iteration, ms
  }
)
允世 2025-01-25 19:17:05

无法轮询cy.request(),因为它只会回复单个响应。

如果该响应是“WAITING”,则测试将不会收到另一个响应。您必须重新发送请求。

You cannot poll a cy.request() because it only ever replies with a single response.

If that response is 'WAITING', the test will not receive another response. You would have to resend the request.

月亮邮递员 2025-01-25 19:17:05

您可以使用自定义超时应用应该('quare','running')断言。应该断言将在预期值的预期值<代码>的指定时间重新尝试。目前,超时为6000ms(6秒),但您可以将其更改为适合您的情况的任何工作。

Cypress.Commands.add('createService', () => {
  const query = ''
  cy.request({
    method: 'POST',
    url: 'http://localhost:3000/create',
    body: {
      query,
    },
  }).as('req')

  cy.wait('@req')
    .its('response.body.serviceStatus', {timeout: 6000})
    .should('equal', 'RUNNING')
})

You can apply a should('equal', 'RUNNING') assertion with a custom timeout. The should assertion will re-try for the specified amount of time for the expected value RUNNING. Currently the timeout is 6000ms(6 seconds) but you can change it to whatever works for your case.

Cypress.Commands.add('createService', () => {
  const query = ''
  cy.request({
    method: 'POST',
    url: 'http://localhost:3000/create',
    body: {
      query,
    },
  }).as('req')

  cy.wait('@req')
    .its('response.body.serviceStatus', {timeout: 6000})
    .should('equal', 'RUNNING')
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文