Collection Runner- How we can save response in local computer for documentation

Yes, you’re right about it failing because the response is xml.

However, there’s an easy fix. Just convert the xml to json

@allenheltondev : Your post has helped me in the past where I was trying to capture the responses from a “Run Collection” into a variable. Now my requirements have slightly changed.

I have test case at the beginning of my script followed by the code to capture all the responses from the “Run Collection”


pm.test(“Body matches string”, function () {
pm.expect(pm.response.text()).to.include(“Sales”);
});

let responses = pm.collectionVariables.get(‘collectionResponses’)
if(responses) {
responses = JSON.parse(responses);
} else {
responses =
}

responses.push(pm.response.json());
pm.collectionVariables.set(‘collectionResponses’, JSON.stringify(responses));


I am unsure what I can do to capture only the responses which have passed the test case. Is this something you can help me with ?

Thanks a lot in advance.

Yes, you can put all that code inside of your test after pm.expect.

If any assertion fails inside of a test, it aborts the remaining code (in that specific test). So if you want to add the response to the array only if the assertion passes, simply add it right after. If the assertion fails, the code will abort and you will not add the response to your array.

pm.test(“Body matches string”, function () {
  pm.expect(pm.response.text()).to.include(“Sales”);

  let responses = pm.collectionVariables.get(‘collectionResponses’)
  if(responses) {
    responses = JSON.parse(responses);
  } else {
    responses = [];
  }

  responses.push(pm.response.json());
  pm.collectionVariables.set(‘collectionResponses’, JSON.stringify(responses));
});
1 Like

Thanks a lot for your help. It worked for me.

Can you point me to the right learning resources where I can learn more about the scripts written like above ?

I’m not sure about resources. This is something I learned just by observing the behavior of how the application works.

Hello. What if response is in .jpg format and I need to save it each iteration? I have to GET ~2000 images, each from different URL

1 Like

I have that problem too

Hi, Allen

Which is given script loaded in test. After run collection and successfully exceuted but where is file located, i cannot to find and not able to view all the response results.

Kindly advise.

I don’t fully understand your question @srinivasan_balan, but I think you’re asking about where the test scripts are stored? They are stored in the Tests tab of the request.

Hi Allen,

Is it possible to save all responses into the export’s JSON?

I tried to run a test script, but the responses are still missing from the file.

What does your test script look like?

I just used what you’ve posted before:

let responses = pm.collectionVariables.get('collectionResponses')

if(responses) {

responses = JSON.parse(responses);

} else {

responses = []

}

responses.push(pm.response.json());

pm.collectionVariables.set('collectionResponses', JSON.stringify(responses));

In that case the values should be in a collection variable called collectionResponses. It is your responsibility to take the value from it and save it to a file.

Hi there!
That’s great with JSONs but what me to do if the responses are .mp3 files?
A TTS service generates them after my requests (about 200 times).
Certainly, I can manually send/save but… so, please point me how to batch it.

1 Like

@allenheltondev what is the maximum size of data i can store in a collection variable. Im making a json list in the collection variable and each element in the list will be the json object corresponding to each api call. The json object will be simple like this :
{
“a” : “a”,
“b” : “b”,
“c” : “c”
}
how many iterations can i make?

Hi All,
I hope someone can answer this.

I am new in Postman, in the Collection Runner, How can I possibly save all response body, status codes, response size and time, and also the test results in a csv file?

Thanks!