Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
Cypress: extract value from JSON body to a variable

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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'))
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda