希望在第二个请求中使用一个请求的结果,用于使用Cypress进行API测试



我想使用cypress测试API端点,并希望对AUTH令牌进行mock,因为它来自另一个API

cy.intercept({
method:'get',
url:'/first endpoint'
},response).as('mocktoken');
cy.request({
method:'get',
url:'/firstendpoint'}).then(response=>{
cy.request({
method:'get',
url:'/secondendpoint'}).then(response=>{ // assertion statement});

这是不工作什么可以做来实现这个场景

首先,我想要验证令牌从第一,我试图模拟然后使用这个令牌,我将从第二个端点得到响应

在这种情况下不要使用cy.intercpet(),它是用于拦截网页请求而不是测试请求。

这是最简单的方法

cy.request('/firstendpoint')
.then(response1 => {
cy.request({
method:'get',
url:'/secondendpoint',
body: {
token: response1.token    // check where the token is in the response
}
})
.then(response2 => { 
...
})
})

没有嘲弄,你在用真实的东西!

最新更新