在柏树自定义comand中等待响应值更改
直到从“等待”变为“运行”中的响应变化,对轮询响应的最佳解决方案是什么。因此,递归功能不是解决方案,因为我不想发送超过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
HTTP 仅是单个响应,但原始 POST 可以为您提供状态 202 和要轮询的标头。
HTTP is only single response, but the original POST can give you a status of 202 and a header to poll.
您应该使用截距/等待组合:
无论您提出单个请求还是提出多个请求,上面都应该工作,因为CY.Wait命令将拦截“ CheckStatus”拦截功能中定义的任何呼叫。
You should use an intercept/wait combination:
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.
您可以根据您的情况使用
cypress-recurse
。You can use
cypress-recurse
for your situation.您无法轮询
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.
您可以使用自定义超时应用
应该('quare','running')
断言。应该断言将在预期值的预期值<代码>的指定时间重新尝试。目前,超时为6000ms(6秒),但您可以将其更改为适合您的情况的任何工作。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 valueRUNNING
. Currently the timeout is 6000ms(6 seconds) but you can change it to whatever works for your case.