1

I am using cypress.io to test an API(Created using Node.js). I want to extract value from JSON response of the API and save it to a variable.

I have tried solutions mentioned in the below links it did not work: Cypress - get value from json response body

Below is the code i am using to test the API:

/// <reference types="cypress" />
describe("Testing Provider Catalog API", () => {
    it("Provider Catalog API GET Request", () => {
        cy.request('GET', 'v1/providers')
            .then((response) => {
                expect(response).to.have.property('status', 200)
                expect(response.body).to.not.be.null
                // expect(response.body.providers).to.have.length(1)
            })
        cy.screenshot()
    })
    it("Provider Catalog API POST Request", () => {
        const provider = {
            "type": "loadboard",
            "name": "123loadboard"
        };
        cy.request('POST', 'v1/providers', provider)
        cy.screenshot()
    })
    it("Provider Catalog API PUT Request", () => {
        const provider = {
            "type": "loadboard",
            "name": "123loadboard"
        };
        cy.request('PUT', 'v1/providers/61a54a66a2b734859481931c', provider)
        cy.screenshot()
    })
    it("Provider Catalog API DELETE Request", () => {
        cy.request('DELETE', 'v1/providers/61a68e7ca6011e605029191b')
        cy.screenshot()
    })
})

Below is the code that i am using

       var providerId
        cy.wait('@newObject').then((response) => {
            expect(response.status).to.eq(201)
            expect(response.responseBody).to.have.property('_id')
            const body = (response.responseBody)
            providerId = body['_id']
        })
        cy.get(someInput).type(newId)

Sample output of the API:

{
"_id":"61a54ba1a2b7348594819323",
"type":"loadboard",
"name":"123loadboard",
"__v":0
}

I want to store the value of the _id in a variable and use it later. I have been trying to for the last couple of days and nothing seems to work. Can anyone please help me. Thanks in advance.

JustAG33K
  • 1,403
  • 3
  • 13
  • 28

1 Answers1

1

The recommended way to use variables with cypress is with aliases. See docs here.

In your code, you can wrap() your _id and save it as an alias, then call your alias somewhere else in your code:

cy.wait('@newObject').then((response) => {
    expect(response.status).to.eq(201)
    expect(response.responseBody).to.have.property('_id')
    cy.wrap(response.responseBody._id).as('newId')
})
// then later in your code use:
cy.get('@newId').then((newId) => {
    cy.get(someInput).type(newId)
})

You could also type() your _id inside your wait():

cy.wait('@newObject').then((response) => {
    expect(response.status).to.eq(201)
    expect(response.responseBody).to.have.property('_id')
    cy.get(someInput).type(response.responseBody._id)
})

Or you can use cypress global environmment object Cypress.env(). See docs here.

cy.wait('@newObject').then((response) => {
    expect(response.status).to.eq(201)
    expect(response.responseBody).to.have.property('_id')
    Cypress.env('newId', response.responseBody._id)
})
// then later in your code use:
cy.get(someInput).type(Cypress.env('newId'))
PeaceAndQuiet
  • 1,692
  • 8
  • 13