Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!