Home > Net >  Testing the Network request with cypress
Testing the Network request with cypress

Time:05-25

I need help! I wanna 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'. BUT I can not reach to results.body to assert 'mailAlertSearchTerm=Handwerk & Produktion'. I tried to show you in the picture as well.

Thank you in advance for any help.

enter image description here

CodePudding user response:

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))
  • Related