0

I have a database table with approx 4,000 records (currently). A response to an API call (POST, JSON) gives me data of this table for a maximum of 1,000 records per API call. A parameter ‘PageNo’ defines which of the 4,000 records are selected (e.g. PageNo = 1 gives me record 1-1000). The header data of the response includes a ‘PageCount’, in my example 4. I am able to retrieve that ‘PageCount’ and the test below loops through the PageNo (result in Postman Console = 1 2 3 4).

How I can call the same request repeatedly in a loop and use the values of the PageNo (i) as a parameter for that request like so:

{{baseUrl}}/v1/Units/Search?PageNo={{i}}

In my example I would expect the request to run 4 times with PageNo2 = 1, 2, 3, 4.

I am aware that I can use a CSV file and loop through the request in Collection Runner but PageCount changes (i.e. the number of records in the table change) and I need to run this loop frequently so creating a new CSV file for each loop is not really an option.

Postman Test:

pm.environment.set('Headers2', JSON.stringify(pm.response.headers));
var Headers2 = JSON.stringify(pm.response.headers);

pm.environment.set('PageCount2', JSON.parse(Headers2)[10].value);
var i;
for (i = 1; [i] <= pm.environment.get('PageCount2'); i++) {
    console.log(i);
    postman.setNextRequest('custom fields | json Copy');

}

Postman Request:

{
  "Location":"{{TestingLocation}}",
  "Fields":[
    "StockNo",
    "BrandDesc"
    ],
  "Filters": {
      "StatusCode":"{{TestingUnitSearchStatusCode}}"
  },
  "PageSize":1000,
  "PageNo" : "{{i}}"
}
karel
  • 5,489
  • 46
  • 45
  • 50
Markus K
  • 21
  • 2
  • 6

2 Answers2

1

With postman.setNextRequest() you can set the calling request as the same request. But you need an exit strategy, otherwise that request would be called infinite times. This only works with the collection runner.

  1. On your first(!) request, store the amount of pages and create a counter.
  2. Increment the counter. If it is smaller than the amount of pages, set the current request as the next request.
  3. Else, do not set a next request. The collection runner will continue with its normal flow. Optionally remove the pages and counter variables.

You can let the request detect that it is its first iteration by not having initialized the amount of pages and counter variables.

AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
  • Thanks very much Anant. I am able to loop through the pages and get my data in this format: [{“StockNo”:“0001”,“BrandDesc”:“Brand1”}, [{“StockNo”:“0002”,“BrandDes”:"Brand2”}] These are two records out of 4,000 and they are all returned in one line. Is there a way to add line breaks to the API response? I also have the option to choose txt, xmll or csv format which would remove the [{]} characters but again no line breaks. Ideally I would like to get the response in the following format: StockNo, BrandDesc 0001, Brand1 0002, , Brand2 Thank you for your help. – Markus K Dec 05 '18 at 01:28
1

Performed a POC for your use-case using postman-echo API.

  1. Pre-req script --> Takes care of the initial request to the endpoint to retrieve the PageSize and set it as an env var. Also initializes the iterationCount to 1 (as env var)
  2. Test Script --> Checks for the current iteration number and performs tests.

Here's a working postman collection. If you're familiar with newman, you can just run:

newman run <collection-name>

You can also import this collection in Postman app and use collection runner.

{
    "info": {
        "_postman_id": "1d7669a6-a1a1-4d01-a162-f8675e01d1c7",
        "name": "Loop Req Collection",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "Run_Req",
            "event": [
                {
                    "listen": "test",
                    "script": {
                        "id": "dac5e805-548b-4bdc-a26e-f56500414208",
                        "exec": [
                            "var iterationCount = pm.environment.get(\"iterationCount\");",
                            "if (pm.environment.get(\"iterationCount\") <= pm.environment.get(\"pageSize\")) {",
                            "    console.log(\"Iteration Number: \" + iterationCount);",
                            "",
                            "    pm.test(\"Status code is 200\", function() {",
                            "        pm.response.to.have.status(200);",
                            "    });",
                            "",
                            "    pm.test(\"Check value of pageNo in body is equal to iterationCount\", function() {",
                            "        var jsonData = pm.response.json();",
                            "        return jsonData.json.PageNo == iterationCount;",
                            "",
                            "    });",
                            "    iterationCount++;",
                            "    pm.environment.set(\"iterationCount\", iterationCount);",
                            "    postman.setNextRequest('Run_Req');",
                            "}",
                            "if (pm.environment.get(\"iterationCount\") > pm.environment.get(\"pageSize\")) {",
                            "    console.log(\"Cleanup\");",
                            "    pm.environment.unset(\"pageSize\");",
                            "    pm.environment.set(\"iterationCount\", 1);",
                            "    postman.setNextRequest('');",
                            "    pm.test(\"Cleanup Success\", function() {",
                            "        if (pm.environment.get(\"pageSize\") == null && pm.environment.get(\"iterationCount\") == null) {",
                            "            return true;",
                            "        }",
                            "    });",
                            "}"
                        ],
                        "type": "text/javascript"
                    }
                },
                {
                    "listen": "prerequest",
                    "script": {
                        "id": "3d43c6c7-4a9b-46cf-bd86-c1823af5a68e",
                        "exec": [
                            "if (pm.environment.get(\"pageSize\") == null) {",
                            "    pm.sendRequest(\"https://postman-echo.com/response-headers?pageSize=4\", function(err, response) {",
                            "        var pageSize = response.headers.get('pageSize');",
                            "        var iterationCount = 1;",
                            "        console.log(\"pre-req:pageSize= \" + pageSize);",
                            "        console.log(\"pre-req:iterationCount= \" + iterationCount);",
                            "        pm.environment.set(\"pageSize\", pageSize);",
                            "        pm.environment.set(\"iterationCount\", iterationCount);",
                            "    });",
                            "",
                            "}"
                        ],
                        "type": "text/javascript"
                    }
                }
            ],
            "request": {
                "method": "POST",
                "header": [
                    {
                        "key": "Content-Type",
                        "name": "Content-Type",
                        "value": "application/json",
                        "type": "text"
                    }
                ],
                "body": {
                    "mode": "raw",
                    "raw": "{\n  \"Location\":\"{{TestingLocation}}\",\n  \"Fields\":[\n    \"StockNo\",\n    \"BrandDesc\"\n    ],\n  \"Filters\": {\n      \"StatusCode\":\"{{TestingUnitSearchStatusCode}}\"\n  },\n  \"PageSize\":1000,\n  \"PageNo\" : \"{{iterationCount}}\"\n}"
                },
                "url": {
                    "raw": "https://postman-echo.com/post",
                    "protocol": "https",
                    "host": [
                        "postman-echo",
                        "com"
                    ],
                    "path": [
                        "post"
                    ]
                }
            },
            "response": []
        }
    ]
}
Anant Naugai
  • 538
  • 4
  • 14