1
votes

i have cypress request call returning Json array {ids:[one, two, three]} How can i parse out one of the array values from the body of response and pass it to the next test?

1

1 Answers

1
votes

Considering the given information I guess what you want is Asserting Network Calls from Cypress Tests.

There is a good example in the cypress-example-recipes

I guess you could do something like

describe('Test', () => {
  let ids;

  it('gets expected response', () => {
    cy.server()
    cy.route('GET', '/url').as('response')
    cy.visit("url")
   
    // log the request object
    cy.get('@response').then(console.log) 
  
    // confirm the request status
    cy.get('@response').should('have.property', 'status', 200)

    // confirm the request's response
    cy.get('@response').its('response').then((res) => {
      expect(res.body).to.deep.equal({
        "ids": ["one", "two", "three"]
      })
     
      // store the ids in a variable
      ids = JSON.parse(res.body)?.ids
    })
  })
})