How to store in a file (csv, excel, etc) the values obtained in a for cycle of Test script without newman?

Hi!

I’ve a test script that is executed after a request and, on this script, I’m using two variables to get two specific items (on each iteration) of a long response body.
Now, I would like to write these values on file to use them later as input on another request. I’m trying to do this without using newman. Is this possible? If yes, how?

Below the test script:

body = JSON.parse(responseBody);
for (var i = 0; i<body.testcaseStatusList.length0; i++){
    valueTestCaseID = body.testcaseStatusList[i].testcaseId
    valueTable = body.testcaseStatusList[i].testcase
}

Thanks in advance!

HI @rcmv

So, there are a couple options that you have available that I know of.

  1. Easier, But More Manual Work: Set the body of the ResponseData to a Collection Variable. Save that variables data to a JSON file (outside of Postman), then run the secondary Requests using the Runner where there is the option to bring in a Data file. NOTE: this works if the data in the JSON is only 1 level deep of data, if its multilevel, this will not work, Postman will not accept it

  2. More Efficient, Most Challenging Code: You can take the entire body (which is stringified json data) and set it to a CollectionVariable (this is assuming that all the Requests that need to access this are in the same Collection. Then when you call the get for the Collection variable in the Pre Request area of the next Request, you then just need to obtain the values that you need. As for the part about when using the nextRequest and it doing the same entry over and over again, the solutions are either to use the pop command from the array, or if you are like me and want to keep the JSON intact, I create a CollectionVariable as the “index” that keeps the count of the current TestCase. From here, use a forEach to locate the information you need, and then incrementing the index.

  3. Most Efficient, but still a lot of coding: using your code above (except you should change the for to a forEach, much easier), create a new empty array, and push the items to the array to build a new array with the exact information you need. Then stringify to a Collection Variable, and from there, depending on the structure of the JSON, the options are either export to JSON file then import using running for iterations, or use NextRequest and a running index to navigate through the items.

I hope this helps. Let me know if you need any other information.

Thank you nrelkov!
I guess there is no easy way :sweat_smile:.
I’ll try to develop your second option.
Thanks for the hints!

Haha, yeah, there is no real easy way to do the setup but if you do the setup using the second option, it will make it much easier to rerun and expand if you need to. Let me know if you need any additional information. Good luck!

1 Like