我似乎在与来自cy.request的响应正文交互时遇到问题。我有两个 API 调用,它们都单独工作。我想从第一次调用的响应数组中发送一个 id,以供第二次调用使用。
Cypress.Commands.add('POST_and_PATCH_Order',() => {
cy.fixture('orderEntry.json').then((fileBody)=>{
cy.request({
method:'POST',
url:'/api/OrderEntry',
json: true,
body: fileBody
})
})
.then(response => {
return new Promise(resolve => {
const target = response.body;
resolve(target)
})
})
})
然后测试
it("An order may be set to delivered", ()=>{
cy.POST_and_PATCH_Order().then(order =>{
const OrderId_val = order['OrderId'];
cy.request({
method:'PATCH',
url:'/api/OrderManager/StatusDelivered',
json: true,
body: {
"OrderId": OrderId_val,
"ActualPickupDate": "2020-06-08T15:34:20.035Z",
"ActualDeliveryDate": "2020-06-08T15:34:20.035Z",
"DeliveryReceiptName": "string"
}
})
})
})
如上所述发送OrderId_val会得到一个 404 响应,请求正文未提供 OrderId 的值。以 OrderId 发送订单会导致整个响应标头作为 OrderId(Body: {"OrderId":[{"OrderId":3588901,"OrderType":"Order","Uuid":"1591821576913"}],"ActualPickupDate":"2020-06-08T15:34:20.035Z","ActualDeliveryDate":"2020-06-08T15:34:20.035Z","DeliveryReceiptName":"string"})
发送
我尝试在命令中与 response.body 进一步交互.js 和测试中,但遇到了 NullReferenceException 结果。如有任何帮助,请从 POST 响应访问 OrderId 值,以便在后续的 PATCH 调用中使用。
您的fileBody
具有array
格式的数据:[{"OrderId":3588901,"OrderType":"Order","Uuid":"1591821576913"}]
您应该将其阅读为order[0].OrderId
.