Reputation: 341
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
Upvotes: 0
Views: 485
Reputation: 18634
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
)
}
})
})
})
Upvotes: 2