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

289
Vistas
How to assert Array element via cypress dynamicly

My question is about the verification of Array elements dynamically. In my project, I have to suggest a price, and every time this suggested price will increase I need such a script that will verify all these elements dynamically. In Screenshot I tried to show what I need to verify. I used below script:

    ` describe('POST method', () => {
    it('suggest a price', () => {
     //Suggest a price you are willing to pay. Available only for auctionType: fixed_price

        cy.request({
            method: 'POST',
            url:"xxxxxx",

            headers:{
                "Authorization":"LOOL",
                "content-type": "application/json"
            },

            body: {
                "id": "d42f516a867590633dd8a82cb2563437",
                "bid": 6900,
                "auctionPlatformId": "xxx",
                "auctionPlatformUserId": 0
            },

            failOnStatusCode: false

            
        })


expect(res.status).to.eq(200)
      expect(res.body).to.have.property("id", "d42f516a867590633dd8a82cb2563437")
      expect(res.body).to.have.property("amount", 6900)
      expect(res.body).to.have.property("auctionPlatformId", "abc")
     expect(res.body).to.have.property("auctionPlatformId", 0)`

but this code is not dynamic and also not verifying

This screen is what I need to make an assertion

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

0

One way is you can use within and define the range.

expect(res.body.amount).to.be.within(6000,7000)

To generate random values for bid you can use Math.random

"bid": Math.floor( Math.random() * 10000 ) + 1  //Generate random numbers between 1 and 10000

So Assuming that in your response all amounts are less than or equal to the bid amount you input, you can do something like this. So You decide the min and max values for your random bid amount and then using the same(min and max values), you can assert that the numbers in response body are within this range.

describe("POST method", () => {
  it("suggest a price", () => {
    //Suggest a price you are willing to pay. Available only for auctionType: fixed_price
    var minval = 1
    var maxval = 10000
    var bidAmount = Math.floor(Math.random() * maxval) + minval
    cy.request({
      method: "POST",
      url: "xxxxxx",

      headers: {
        Authorization: "LOOL",
        "content-type": "application/json",
      },

      body: {
        id: "d42f516a867590633dd8a82cb2563437",
        bid: bidAmount,
        auctionPlatformId: "xxx",
        auctionPlatformUserId: 0,
      },

      failOnStatusCode: false,
    }).then((res) => {
      expect(res.status).to.eq(200)
      //If you want to assert separately
      expect(res.body.suggestedPrices[0].amount).to.be.within(minval, maxval)
      expect(res.body.suggestedPrices[1].amount).to.be.within(minval, maxval)
      expect(res.body.suggestedPrices[2].amount).to.be.within(minval, maxval)

      //If you want to assert all together
      for (var index in res.body.suggestedPrices) {
        expect(res.body.suggestedPrices[index].amount).to.be.within(
          minval,
          maxval
        )
      }
    })
  })
})
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