Home > Mobile >  Cypress REST calls - how to address field in JSON response body?
Cypress REST calls - how to address field in JSON response body?

Time:09-17

I want to verify the JSON response from REST endpoints, so I cannot use the “should” method because it targets the DOM of a web page (which I don’t have – only REST API available). My JSON response looks like this:

[
  {
    "id": 111,
    "createdAt": "2021-09-14T16:19:29.803",
    "datasetId": "cypress-datasetId2",
  },
  {
    "id": 112,
    "createdAt": "2021-09-14T16:19:29.874",
    "datasetId": "cypress-datasetId",
  },
  {
    "id": 113,
    "createdAt": "2021-09-14T16:30:37.101",
    "datasetId": "cypress-datasetId3",
  },
]

I don’t know in advance how many datasets the response will contain. I don’t know the counter of “id” as well. I need to find out if a certain datasetId is contained in the response. I test for:

   it('Validate the datasetId of any entry - expect', () => {
       cy.request({
    method: 'GET',
    url: 'http://localhost:8040/data-sets',
       }).then((response) => {
        expect(response.body).to.deep.equal({datasetId: 'cypress-datasetId3'})
       })
   });

The AssertionError comes up:

expected [ Array(6) ] to deeply equal { datasetId: 'cypress-datasetId3' }

at the code position of: ”.equal”. So it does not tell what the result looks like, only what it expected and that it went wrong. How can I check for the presence of a certain field in any of the datasets I get as response? Or what allowed me to identify the whole dataset, e.g.

  {
    "id": 113,
    "createdAt": "2021-09-14T16:30:37.101",
    "datasetId": "cypress-datasetId3",
  },

And return that as a result?

One of the many alternatives that I tried was:

   it('Validate the datasetId of any entry - should', () => {
       cy.request({
    method: 'GET',
    url: 'http://localhost:8040/data-sets',
       }).its('body').should('deep.contain', {
           datasetId: 'cypress-datasetId3'
       })
   });

Which results in

Timed out retrying after 4000ms: expected [ Array(6) ] to deep include { datasetId: 'cypress-datasetId3' }

The database is empty, so there should be no delay for seeking data sets.

What does work is to address a specific dataset in the array but typically I cannot know this in advance:

   it('Validate the datasetId of the first entry - include', () => {
       cy.get('@data-sets')
        .its('body').its('0').its('datasetId').should('include', 'cypress-datasetId');
   });

How can I make my approach more generic and independent from the result order?

CodePudding user response:

The response is an array, so you could .map() to the datasetId

cy.request({
  ...
}).then((response) => {
  const datasetIds = response.map(item => item.datasetId)
  expect(datasetIds).to.include('cypress-datasetId3')
})

To get that whole object, use .find()

cy.request({
  ...
}).then((response) => {
  const found = response.find(item => item.datasetId === 'cypress-datasetId3')
  cy.wrap(found).as('dataset)
})

cy.get('@dataset')
  .its('datasetId')
  .should('eq', 'cypress-datasetId3')

CodePudding user response:

If you want to assert one entire section of the array you have to use deep.include. Something like this:

it("Validate the datasetId of any entry - should", () => {
  cy.request({
    method: "GET",
    url: "http://localhost:8040/data-sets",
  }).then((response) => {
    expect(response.body).to.deep.include({
      id: 113,
      createdAt: "2021-09-14T16:30:37.101",
      datasetId: "cypress-datasetId3",
    })
  })
})

Or if you just want to assert that the datasetId values. For that first you have to extract the values from the json into an array and then assert it, something like this:

it("Validate the datasetId of any entry - should", () => {
  cy.request({
    method: "GET",
    url: "http://localhost:8040/data-sets",
  }).then((response) => {
    var datasetIdArray = [] //this will have all the datasetid's
    for (var index in response.body) {
      datasetIdArray.push(response.body[index].datasetId)
    }
    expect(datasetIdArray).to.include("cypress-datasetId")
  })
})
  • Related