TiTi
TiTi

Reputation: 27

Testing the Network request with cypress

I want to test the network request and assert the requested data with Cypress.

cy.intercept('POST', '*service=jobsSearch*').as('ajaxRequest')

cy.get(selectors.dataComponent('jobs')).find('button').click()

cy.wait('@ajaxRequest')

cy.get('@ajaxRequest').its('request.body').should('contain', 'jobApprenticeshipType=stelle')

cy.get('@ajaxRequest').its('results.body').should('contain', 'mailAlertSearchTerm=Handwerk & Produktion')

I can reach to 'request.body' and assert the 'jobApprenticeshipType=stelle'. However, I can not reach to results.body to assert 'mailAlertSearchTerm=Handwerk & Produktion'. Here is a screenshot of the result:

enter image description here

Upvotes: 0

Views: 96

Answers (1)

agoff
agoff

Reputation: 7152

The response has a key of response and not results. My guess is that your response looks something like...

{
  "results": {
    ...
    "mailAlertSearchTerm": "foo"
    ...
  }
}

In which case, you'd want to reference it by something like this:

cy.get('@ajaxRequest')
  .its('response.body')
  .should('contain', 'mailAlertSearchTerm=Handwerk & Produktion')
// or, more specifically using the JSON properties
cy.get('@ajaxRequest')
  .its('response.body.results.mailAlertSearchTerm')
  .should('contains', 'Handwerk & Produktion');
  })

You can see this if you just get the intercept and log the entire thing.

cy.wait('@ajaxRequest')
  .get('@ajaxRequest')
  .then((data) => cy.log(data))

Upvotes: 0

Related Questions