gpt4 book ai didi

javascript - 等待 Cypress 中的响应主体发生变化

转载 作者:行者123 更新时间:2023-12-02 19:20:21 26 4
gpt4 key购买 nike

我正在尝试自动化一种情况,我需要等待,直到在响应正文中看到值成功。我尝试过的:

cy.server()
cy.route({
method: 'GET',
url: 'https://endpoint.com/api/readsomething'
auth: {
'username': username
'password': password
},
response: {}
}).as('checkStatus');

cy.wait('@checkStatus').then((ele) => {
expect(ele).should('have.property', 'response.body.latest_run.status', 'Success')
})
})

我收到错误:cy.wait() 超时等待第一个路由请求 5000 毫秒:checkStatus。没有发生任何请求。

如果您能让我知道我做错了什么,那就太好了。

最佳答案

我通过查看请求 cypress 文档找到了解决方案 - https://docs.cypress.io/api/commands/request.html#Request-Polling

由于我想要调用的 API 端点与我正在处理的应用程序不同,因此我使用了 cy.request()而不是cy.server() cy.route()

    function getStatus() {
cy.request({
method: 'GET',
url: Cypress.config('endpoint'),
auth: {
'username': Cypress.config('username'),
'password': Cypress.config('password')
}
}).then((response) => {
if (response.body.status == "Success")
return
else
getStatus()
})
}

//Trigger the above function
cy.then(getStatus)

关于javascript - 等待 Cypress 中的响应主体发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63140735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com