Skip to content

Instantly share code, notes, and snippets.

@simicd
Last active August 14, 2020 10:59
Show Gist options
  • Save simicd/483eb4e1dc911d6c46c45a65e8b5f49d to your computer and use it in GitHub Desktop.
Save simicd/483eb4e1dc911d6c46c45a65e8b5f49d to your computer and use it in GitHub Desktop.
Postman array iteration
{
"info": {
"_postman_id": "c18ab42d-2677-4ede-b043-99535f4da9f6",
"name": "Dog Image API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Dog API - Loop through breeds",
"event": [
{
"listen": "test",
"script": {
"id": "0fb55e66-c49d-4687-baf9-d30b36aa11ad",
"exec": [
"// Get current index",
"let item = pm.globals.get(\"arrayIndex\");",
"// If larger than number of elements in array minus 1 start with first element again",
"if (Number(item) >= 3-1) {",
" pm.globals.set(\"arrayIndex\", 0);",
"} else {",
" pm.globals.set(\"arrayIndex\", Number(item) + 1);",
"}",
"",
"// Check if response code is 200 = success",
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});"
],
"type": "text/javascript"
}
},
{
"listen": "prerequest",
"script": {
"id": "5d55d2a2-623b-442a-b2a6-615d9904a0b8",
"exec": [
"// List of breeds",
"const dataArray = [\"hound\", \"beagle\", \"germanshepherd\"];",
"",
"// Get current index from environment (eye icon in top right corner)",
"let item = pm.globals.get(\"arrayIndex\");",
"",
"// Store the current value of breed as environment variable",
"pm.globals.set(\"breed\", dataArray[item]);"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "https://dog.ceo/api/breed/{{breed}}/images/random",
"protocol": "https",
"host": [
"dog",
"ceo"
],
"path": [
"api",
"breed",
"{{breed}}",
"images",
"random"
]
}
},
"response": []
},
{
"name": "Dog API - Loop with expected value",
"event": [
{
"listen": "test",
"script": {
"id": "0fb55e66-c49d-4687-baf9-d30b36aa11ad",
"exec": [
"// Get current index",
"let item = pm.globals.get(\"arrayIndex\");",
"// If larger than number of elements in array minus 1 start with first element again",
"if (Number(item) >= 3-1) {",
" pm.globals.set(\"arrayIndex\", 0);",
"} else {",
" pm.globals.set(\"arrayIndex\", Number(item) + 1);",
"}",
"",
"// Check if expected value is in response body",
"let expected = pm.globals.get(\"expected_body_contains\");",
"pm.test(\"Body matches string\", function () {",
" pm.expect(pm.response.text()).to.include(expected);",
"});",
"",
"// Check if response code is 200 = success",
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});"
],
"type": "text/javascript"
}
},
{
"listen": "prerequest",
"script": {
"id": "5d55d2a2-623b-442a-b2a6-615d9904a0b8",
"exec": [
"// List of breeds",
"const dataArray = [\"hound\", \"beagle\", \"germanshepherd\"];",
"",
"// Expected value",
"const expectedArray = [\"hound\", \"beagle\", \"germanshepherd\"];",
"",
"// Get current index from environment (eye icon in top right corner)",
"let item = pm.globals.get(\"arrayIndex\");",
"",
"// Store the current value of breed and expected value contained in response body",
"// as environment variable",
"pm.globals.set(\"breed\", dataArray[item]);",
"pm.globals.set(\"expected_body_contains\", expectedArray[item]);"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "https://dog.ceo/api/breed/{{breed}}/images/random",
"protocol": "https",
"host": [
"dog",
"ceo"
],
"path": [
"api",
"breed",
"{{breed}}",
"images",
"random"
]
}
},
"response": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment