The first part of your request is reasonably straight forward, but you can’t write directly to disk with Postman, so that part will be more difficult. I would recommend setting up a database with API access to send the results to.
The first part of your request can be achieved using the collection runner and setNextRequest to loop through an array of test data (your ID’s).
Setup three collection variables to start with. You need a blank array called “IDs”, a blank object to store your results, and finally a variable to store the count (which will be used to custom tailor the results).
My example is running against Postman echo, and is just sending the ID as part of the URL.
https://postman-echo.com/get?test={{id}}
The pre-request script gets retrieves the IDs array set earlier, and if its blank will repopulate it with what ever test data you add.
If the array is zero, it will also reset the results object and count.
It uses a JavaScript function called shift() which will retrieve the first element in the array (and also remove it from the array).
The script finally sets the currentID and also updates the saved collectionVariable with the updated IDs array.
var array = JSON.parse(pm.collectionVariables.get("IDs"));
if(array.length === 0){
array = [1231, 1232, 1233];
// also reset the results object and count variable
pm.collectionVariables.set("results", JSON.stringify({}));
pm.collectionVariables.set("count", 1);
}
var currentID = array.shift();
pm.collectionVariables.set("id", currentID);
pm.collectionVariables.set("IDs", JSON.stringify(array));
In the Tests tab, you retrieve the IDs array again so you can keep looping until the array is zero length. You also retrieve the current results and count.
It has a simple test which just checks if the response is the same as what was sent.
If the test passes, then the results are updated, the count increased and both collectionVariables are updated.
There is an IF statement that will keep running the same request until the array length is zero.
The ELSE statement is where the results are finally logged to the Console. Its here where you would need to send your results to a database or API.
let array = JSON.parse(pm.collectionVariables.get("IDs"));
let currentResults = JSON.parse(pm.collectionVariables.get("results"));
let currentCount = pm.collectionVariables.get("count");
const expectedResponse = pm.collectionVariables.get("id");
const actualResponse = pm.response.json().args.test;
pm.test(`ID = ${expectedResponse}`, () => {
pm.expect(expectedResponse).to.eql(parseInt(actualResponse));
// add to results object
currentResults[`POST${currentCount}`] = actualResponse;
// increase current count
currentCount++;
// write results and current count back to collection variables
pm.collectionVariables.set("results", JSON.stringify(currentResults));
pm.collectionVariables.set("count", currentCount);
});
if (array.length > 0){
postman.setNextRequest("Postman Echo Loop");
} else {
console.log(JSON.parse(pm.collectionVariables.get("results")));
// this is where you will need to send the results to another API as you can't write directly to disk
postman.setNextRequest(null);
}