Retreive API response data with cy.intercept

12.1k views Asked by At

I'm intercepting request URL: https://api-stg.geneplanet.com/api/nipt-exporters/tasks/407ff05afdec42caa17a660d2c855117/status Response of requested URL is

{
  "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"
}

I want to make the assertion on runtimeStatus equals Completed.

Following code is not working:

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')

I've also tried should('include') and should('contain') in the last line. What am I doing wrong?

3

There are 3 answers

4
Fody On

The cy.wait('@exp') yields an interception object, see Using the yielded object

// interception object
{
  request: {...},
  response: {
    headers: {...},
    body: {
      "name": "ExportN2DStatisticsOrchestrator",
      "instanceId": "407ff05afdec42caa17a660d2c855117",
      "runtimeStatus": "Completed",
      ...
}

so this should work

cy.wait('@exp').its('response.body.runtimeStatus').should('eq', 'Completed')

or

cy.wait('@exp').its('response.body').should('have.property', '.runtimeStatus', 'Completed')
1
Fseee On

As said by Alex on comment to your question, try below code in order to check what is inside the "intercept" object returned from the wait call:

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
})
0
khizer On

Intercepting http call and returning customised response

Intercepted request needs to be resolved using the yielded object

Cypress version: "^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 
    })