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

285
Visualizações
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 Respostas
Responde à pergunta

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 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