gpt4 book ai didi

javascript - 如何确保 Cypress 请求执行的顺序

转载 作者:行者123 更新时间:2023-12-04 07:18:44 40 4
gpt4 key购买 nike

在我的 Cypress 测试中,我试图调用两个单独的 API 端点。
我可以调用电话,但我需要确保它们以正确的顺序执行。
以下是我的请求的简化版本:

cy.request('POST', apiUrl + 'Session', {username: merchant_username}
).as('postSession')

cy.request('POST', apiUrl + 'TestCase', {username: merchant_username}
).as('postTestCase')
调用按此顺序执行很重要,因为其中一些调用依赖于其他调用的值。
我正在尝试检索 sessionId来自 postSession回复:
cy.request({
method: 'POST',
url: apiUrl + 'session',
}).as('postSession')

cy.get('@postSession').should(response => {
sessionId = response.body.SessionId;
})
然后在 postTestCase的请求体中使用:
cy.request({
method: 'POST',
url: apiUrl + 'TestCase',
body: {
"SessionId": sessionId
}
})
如果我这样做 .then()postSession 之后& 地点 postTestCase在这样的内部,请求工作正常,但如果可能的话,我想避免这样做。
cy.get('@postToken').should(response => {
sessionId = response.body.SessionId;
}).then(() => {
cy.request({
method: 'POST',
url: apiUrl + 'TestCase',
body: {
"SessionId": sessionId
}
})
})
我也试过使用 cy.wait() ,但 sessionId然后在第二个请求中为空白。
cy.wait('@postSession')
cy.wait('@postTestCase')
有什么办法可以保证 postSessionpostTestCase 之前执行不放置 postTestCase.then() 内在 postSession 之后?

最佳答案

不幸的是,在发布此答案时, Cypress GitHub 存储库中有一个 Unresolved 问题,其中包含 await 的提案。 , 根据这个 link .
所以目前只有“嵌套”请求方式是可能的。
例如你的片段:

cy.request({
method: 'POST',
url: apiUrl + 'session',
}).then((response) => {
const sessionId = response.body.SessionId;

cy.request({
method: 'POST',
url: apiUrl + 'TestCase',
body: {
"SessionId": sessionId
},
});
});

关于javascript - 如何确保 Cypress 请求执行的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68635203/

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