1

In Network Calls we have a request url https://xyz.com.testqa.com/site/v3/userPref/
There is a different tab for payload where i want to validate action id in below json

c: 5232
d: {noticeId: 32029, companyId: 5232, actionId: 11, regulationId: 2, regulationType: 1, 
consentGiven: 1,…}
actionId: 11

I am able to assert status code for above url using below code:

cy.intercept('POST','privacycollector.test.com/site/v3/userPref/',(req) => { }).as('getSettings') 
cy.wait('@getSettings').then(({ response }) => { expect(response.statusCode).to.eq(204) }) 
cy.wait('@getSettings').then(({ request }) => { const requestBody = request.body cy.log(requestBody) })

but i am not able to validate json in request payload.
These calls are coming in network tab

Nopal Sad
  • 516
  • 1
  • 7
Manish B
  • 389
  • 1
  • 4
  • 19
  • It would help if you share some code. You may not be drilling down to the exact part you are looking. – jjhelguero May 30 '22 at 19:55
  • cy.intercept('POST','https://privacycollector.test.com/site/v3/userPref/',(req) => { }).as('getSettings') cy.wait('@getSettings').then(({ response }) => { expect(response.statusCode).to.eq(204) }) cy.wait('@getSettings').then(({ request }) => { const requestBody = request.body cy.log(requestBody) }) – Manish B May 31 '22 at 08:18

2 Answers2

1

First thing, the order of commands is not correct. You should set up the cy.intercept() which listens for the networks calls before triggering those calls.

Second thing, if there is only one POST you can only cy.wait('@getSettings') once. You must do all checks inside first callback.

// first set up cy.intercept listener
cy.intercept('POST','privacycollector.test.com/site/v3/userPref/', (req) => {})
  .as('getSettings') 

// now trigger the network calls
cy.visit(...)

// wait for catch
cy.wait('@getSettings').then((interception) => { 

  expect(interception.response.statusCode).to.eq(204)

  const requestBody = interception.request.body 
  console.log(interception.request.body) 

  expect(interception.request.body.c).to.eq('5232')

  expect(interception.request.body.d.actionId).to.eq('11')

})

or deconstruct both request and response in the paramter

// wait for catch
cy.wait('@getSettings').then(({request, response}) => { 
  ...
Nopal Sad
  • 516
  • 1
  • 7
  • Your answer is partially correct. But response code is checked against response but the json is in request body. How can we assert both request and response – Manish B May 31 '22 at 11:01
  • Ok, yes I was going to add another change for that and forgot to. – Nopal Sad May 31 '22 at 11:49
  • why do we first intercept the request and then visit the website. Kindly guide me with technical details – Manish B May 31 '22 at 17:13
  • It's a matter of timing. The intercept does not read past information from the network tab, it listens to network calls as they happen, so you must set up the intercept before the calls are triggered. – Nopal Sad May 31 '22 at 23:21
0

You should check for specific property:

 cy.wait('@getSettings').then((response) => { 
   expect(response.d).to.have.property('actionId',11);
 })
Fseee
  • 2,476
  • 9
  • 40
  • 63