我正在拦截请求URL:https://api-stg.geneplanet.com/api/nipt-exporters/tasks/407ff05afdec42caa17a660d2c855117/status 请求的 URL 的响应是
{
"name": "ExportN2DStatisticsOrchestrator",
"instanceId": "407ff05afdec42caa17a660d2c855117",
"runtimeStatus": "Completed",
"input": {
"BlobUri": "https://niptexportersstgsa.blob.core.windows.net/nipt-exports/N2D statistics
export_11.04.2022 13.38.49.xlsx?sv=2018-03-
28&sr=c&sig=V0pXmIQUccUdkm0WtsZ3ENjfr%2FtYiCvYDztgZ6JWaYk%3D&se=2022-04-
11T12%3A38%3A49Z&sp=rc",
"From": "2022-03-11T00:00:00+00:00",
"To": "2022-04-11T23:59:59+00:00"
},
"customStatus": {
"message": "done"
},
"output": null,
"createdTime": "2022-04-11T11:38:49Z",
"lastUpdatedTime": "2022-04-11T11:38:50Z"
}
我想对runtimeStatus 进行断言等于Completed。
以下代码不起作用:
cy.intercept('https://api-stg.geneplanet.com/api/nipt-exporters/tasks/*/status').as('exp')
cy.get('.col-sm-12').should('be.visible').and('contain','Export').click()
cy.get('.ng-star-inserted > .p-4 > .mb-2').should('be.visible').and('contain','N2D Statistics export')
cy.get('.ng-star-inserted > .p-4 > .mb-2').should('be.visible').and('contain',' Preparing a document. Please wait.')
cy.wait('@exp').its('response.runtimeStatus').should('eq', 'Completed')
我还在最后一行尝试了 should('include') 和 should('contain') 。 我做错了什么?
cy.wait('@exp')
产生一个拦截对象,请参阅使用产生的对象
// interception object
{
request: {...},
response: {
headers: {...},
body: {
"name": "ExportN2DStatisticsOrchestrator",
"instanceId": "407ff05afdec42caa17a660d2c855117",
"runtimeStatus": "Completed",
...
}
所以这应该有效
cy.wait('@exp').its('response.body.runtimeStatus').should('eq', 'Completed')
或
cy.wait('@exp').its('response.body').should('have.property', '.runtimeStatus', 'Completed')
拦截http调用并返回自定义响应
需要解决被拦截的请求 使用生成的对象
赛普拉斯版本:“^11.0.1”
cy.intercept(
{
method: 'GET',
url: <API_ENDPOINT>
},
{
statusCode: 200,
statusMessage: 'Success',
body: <BODY_RESPONSE_TO_SEND_BACK>
}
).as('interceptedRequest');
cy.wait('@interceptedRequest').should(({ request, response }) => {
cy.log("Response", response.body);
// Perform Assertion based on response body
})
正如亚历克斯对您的问题的评论所说,请尝试下面的代码以检查从等待调用返回的“拦截”对象内的内容:
cy.wait('@exp').then((intercept)=>{
console.log(intercept); //will log a cy object containing the response
console.log(intercept.response.runtimeStatus); //will log what you need
expect(intercept.response.runtimeStatus).to.be.eq('Completed'); //should work
})