柏树拦截存根并检索响应



你好,我对在柏树上使用intercept相对陌生。单击按钮发送请求。截取而非存根(//1(使我可以在cy.log($resp.response(中检索响应中的日期值,但我也需要存根响应(//2(这无法在cy.log中返回日期值($resp.response(如何检索响应并保持存根?

cy.intercept({method: 'POST', url: '**/myURL'}).as('successfulAction')  //1      
cy.intercept({method: 'POST', url: '**/myURL'},{stubbed data}).as('successfulAction') //2
cy.get('button').click()
cy.wait('@successfulAction').then(($resp) => {
cy.log($resp.response)
})

在第一次截取时,添加中间件标志。

这允许捕获真正的请求,但将请求传递给应用存根数据的第二次拦截。

cy.intercept({
method: 'POST', 
url: '**/myURL',
middleware: true
}).as('successfulAction')     
cy.intercept({method: 'POST', url: '**/myURL'}, {stubbed data}) // no alias needed
cy.wait('@successfulAction')
.then(($resp) => {
cy.log($resp.response)
})

您也可以使用单个拦截

cy.intercept({
method: 'POST', 
url: '**/myURL',
middleware: true
}).(req => {
req.continue(resp => {
cy.log($resp.response)
res = stubbedData
})
})

.作为("成功操作"(

谢谢你的回答,我确实给了我一个想法,下面的方法很有效。

cy.intercept({
method: 'POST', 
url: `**/${parentAlarmsAssetsData[0].AssetNumber}`,
middleware: true
},(req) => {
req.continue((res) => {
res.send({ //add stubbed data
statusCode: 200,
body: {
"status":true,
"responseMessage":null
}
})
})
}).as('successfulAction')

cy.get(manualCheckButton).click()
cy.wait('@successfulAction').then(($resp) => {
acknowledgedDateTime = $resp.response.headers.date //set global var
})

最新更新