Writing test in Postman using for loop, forEach, and find method in JS

Ceren Çakır
4 min readFeb 1, 2024

--

  • parsing JSON response, testing using for loop and forEach and find method in JS, writing assertions, using set and get in collectionVariables, following postman console..

Here, we aim to test (check/assert) if the last created order id is in the response or not. I’ve previously created a few orders using createOrder endpoint, and we already have the last created order id. Below JSON response body belongs to getAllOrders. It shows all the orders that we’ve created before. So let’s begin!

[
{
"id": "PZNMQs0CxtP0ygLGb6Mxl",
"items": [
{
"id": 841803488,
"productId": 4643,
"quantity": 1
},
{
"id": 682717067,
"productId": 8554,
"quantity": 1
}
],
"customerName": "Joe Doen",
"created": "2024-01-23T15:19:18.139Z",
"comment": ""
},
{
"id": "BLH912IMGdxhFslzVSgrh",
"items": [
{
"id": 818449830,
"productId": 8554,
"quantity": 1
}
],
"customerName": "Bernard Daugherty",
"created": "2024-01-23T15:33:33.881Z",
"comment": ""
},
{
"id": "BnMgMJAWFW7FZk5t64Hox",
"items": [
{
"id": 821011003,
"productId": 8554,
"quantity": 1
}
],
"customerName": "Ceren Doenz",
"created": "2024-01-23T16:07:33.280Z",
"comment": "empty string'di biz comment ekledik"
},
{
"id": "JBXYMKtsCbhHZZ2olPU2d",
"items": [
{
"id": 442879563,
"productId": 4646,
"quantity": 1
}
],
"customerName": "Ceren Doe",
"created": "2024-01-23T21:54:36.066Z",
"comment": "empty string'di biz comment ekledik"
},
{
"id": "d9aN8D-792X3499EIqtiu",
"items": [
{
"id": 21935557,
"productId": 4643,
"quantity": 1
}
],
"customerName": "Tyler Wehner",
"created": "2024-01-30T16:56:17.177Z",
"comment": ""
},
{
"id": "qPoEl5687ygEK7sMdeDmx",
"items": [
{
"id": 135051737,
"productId": 4643,
"quantity": 1
}
],
"customerName": "Pete Gaylord",
"created": "2024-01-30T17:01:35.390Z",
"comment": ""
},
{
"id": "IU1t69TqeP2w8wGTmzsmk",
"items": [
{
"id": 164764264,
"productId": 4643,
"quantity": 6
}
],
"customerName": "Wilbur D'Amore",
"created": "2024-01-30T17:03:06.811Z",
"comment": ""
},
{
"id": "7XTw2TAGGPCCiUDKL8fmA",
"items": [
{
"id": 552813587,
"productId": 4643,
"quantity": 6
}
],
"customerName": "Harold Gleichner",
"created": "2024-01-30T17:12:44.941Z",
"comment": ""
},
{
"id": "ZAV5NeSMI83xjGAD_P45Q",
"items": [
{
"id": 509286857,
"productId": 4643,
"quantity": 6
}
],
"customerName": "Ceren Cakir",
"created": "2024-01-30T17:15:43.466Z",
"comment": "I updated comment part"
},
{
"id": "iLYCi8kZAZZnnbj6nn7Ue",
"items": [
{
"id": 851634198,
"productId": 4643,
"quantity": 6
}
],
"customerName": "Ceren Cakir",
"created": "2024-01-30T17:28:56.249Z",
"comment": "I updated comment part"
},
{
"id": "HaTtuuIWViMqpU6i_2_9j",
"items": [
{
"id": 633623602,
"productId": 4643,
"quantity": 1
}
],
"customerName": "Mr. Ronnie McKenzie",
"created": "2024-01-31T11:25:29.178Z",
"comment": ""
}
]

I’ve set orderId variable like below while creating a new order.

pm.collectionVariables.set('orderId', orderId);

You can see the orderId in the collection’s variables part. It’s the last created order id. So, let’s look if this orderId exists in the above response or not.

Well, the test part includes 2 tests, and the first one is of course related to checking response status. Before starting the second test, parse the response first, and get the last orderId that we set before. In this way, we can check if the orderId is on the list or not. Moreover, if the orderId matches one of them from the list, we can check the customer name who got the order.. etc

For better understanding, I wrote everything step by step and clearly. You can check the for loop by following the response and the console (picture below) All printed in the console, for example,

what is the response[0].id ? what is the response[1].id ?

does it (response[3].id) match with last created orderId ?

if it doesn’t match, what is written in console ? if it matches, what is written in console?

You can also use the forEach instead of using for loop. It gives the same results.

Follow them in the Console one by one!

You can also use the find method. Check the console below.

Thanks in advance! If you have any questions, just ask! :)

We often think that great achievements require great actions. However, it is surprising the difference a small improvement can make over time. If you improve by 1 percent every day for 1 year, you will be 37 times better by the end of the year. So, keep working!

--

--