Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

76
Visualizações
Get property from JSON response in Cypress

Can someone, help in solve asserting following response:

{"rowId":"899103a2-a9b1-42t3-bc3w-6we638a43fc3","polygons":[{"polygon1":"40eea45f-ffc8-46vb-9ae6-26f5ba5edf4b","polygon2":{"type":"Polygon","geoId":[[[-45.428529,48.321791],[-45.428529,48.321791],[-45.428529,48.321791],[-45.428148,48.321653],[-45.428164,48.32163],[-45.428529,48.321791]]]}}]}

Tried with following code:

Cypress.Commands.add('updateResponse', (request, elementCss) => {
cy.intercept(request, '**/api/rows*').as('update')
cy.xpath(elementCss)
.click()
.wait('@update', { timeout: 20000 })
.then((xhr) => {
  cy.log(JSON.stringify(xhr.response.body))
    .its('response.statusCode')
    .should('eq', 200)
    .its('rowId')
    .should('not.be.empty')
 })
})

where request variable is POST request.

Achieved only to assert that response code is 200. For second assert, rowID is not empty, I got error:

"*Timed out retrying after 10000ms: cy.its() errored because the property: rowId does not exist on your subject.*"

I want ta assert, also, that polygon2 array is not empty and having data inside geoId.

What I am doing wrong? Thank you in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can do something like this:

Cypress.Commands.add("updateResponse", (request, elementCss) => {
  cy.intercept(request, "**/api/rows*").as("update")
  cy.xpath(elementCss)
    .click()
    .wait("@update", { timeout: 20000 })
    .then((xhr) => {
      expect(xhr.status).to.eq(200)
      expect(xhr.body).to.have.property(
        "rowId",
        "899103a2-a9b1-42t3-bc3w-6we638a43fc3"
      )
      expect(xhr.body.rowId).not.to.be.empty
    })
})

Or, in your case instead of just rowId you have to provide response.body.rowId

Cypress.Commands.add("updateResponse", (request, elementCss) => {
  cy.intercept(request, "**/api/rows*").as("update")
  cy.xpath(elementCss)
    .click()
    .wait("@update", { timeout: 20000 })
    .then((response) => {
      cy.log(JSON.stringify(response.body))
        .its("response.statusCode")
        .should("eq", 200)
        .its("response.body.rowId")
        .should("not.be.empty")
    })
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda