![](/img/trans.png)
[英]how intercept and stub the response of a rpc call in react with cypress
[英]cypress intercept stub and retrieve response
您好,所以在柏樹上使用攔截相對較新。 單擊按鈕發送請求。 攔截而不是存根 (//1) 讓我可以檢索 cy.log($resp.response) 中看到的響應中的日期值,但我也需要存根響應 (//2) 這無法返回日期值在 cy.log($resp.response) 中。 數據值是在 UI 中看到的那樣生成我如何檢索響應並仍然存根?
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
})
})
.as('successfulAction')
感謝您的回答,我絕對給了我一個想法,下面的作品。
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
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.