0
votes

I am trying to run the following Postman Script via Newman to write the response to file:

  //verify http response code

    pm.test("Report Generated", function () {
    pm.response.to.have.status(200);
    });

    var fs = require('fs');

    var outputFilename = 'C:/Users/archit.goyal/Downloads/spaceReport.csv';
    fs.writeFileSync(outputFilename, pm.response.text());

The request gives a response but getting the following error when writing to file: 1? TypeError in test-script

    ┌─────────────────────────┬──────────┬──────────┐
    │                         │ executed │   failed │
    ├─────────────────────────┼──────────┼──────────┤
    │              iterations │        1 │        0 │
    ├─────────────────────────┼──────────┼──────────┤
    │                requests │       20 │        0 │
    ├─────────────────────────┼──────────┼──────────┤
    │            test-scripts │       20 │        1 │
    ├─────────────────────────┼──────────┼──────────┤
    │      prerequest-scripts │        0 │        0 │
    ├─────────────────────────┼──────────┼──────────┤
    │              assertions │        2 │        0 │
    ├─────────────────────────┴──────────┴──────────┤
    │ total run duration: 1m 48.3s                  │
    ├───────────────────────────────────────────────┤
    │ total data received: 1.24MB (approx)          │
    ├───────────────────────────────────────────────┤
        │ average response time: 5.3s                   │
    └───────────────────────────────────────────────┘
 #  failure        detail

 1.  TypeError      fs.writeFileSync is not a function
                at test-script
                inside "3i_BMS_Amortization_Schedule / GetReport"

Please help

1
If you're adding fs in a Postman test it won't work as it's not one of the modules that you can call from the application. getpostman.com/docs/v6/postman/scripts/…Danny Dainton
I added the code in application but running and calling its modules via Newman. Will it not work that way as well? If not, is there any way to write the Response(Coming in Text Format) to a CSV FileArchit Goyal
It won't work if you have anything in the application that's not supported. Can you not handle that action outside of a Postman collection and add something similar straight into a Newman script? Newman has options to return the response data.Danny Dainton
I am trying to do it via newman only. I am running the code shared above using newman and response is also from newman console. Would be great if you can share any piece of working code that will work via newman to save the response.Archit Goyal
If you open the collection in the Postman UI, is the code above containing the fs statement, in the Test tab? If its in there, you're not running it correctly with Newman. I would add the fs module that what ever I wanted to do with it to something like this - github.com/postmanlabs/newman/blob/develop/…Danny Dainton

1 Answers

0
votes

Postman itself cannot execute scripts like that. To save your responses of all the api requests, you can create a nodeJS server which will call the api through newman and then save the response to a local file. Here is an example -

var fs = require('fs'),
newman = require('newman'),
allResponse=[],
outputFilename = 'C:/Users/archit.goyal/Downloads/spaceReport.csv';


newman.run({
collection: '//your_collection_name.json', 
iterationCount : 1
})
.on('request', function (err, args) {
if (!err) {

    //console.log(args); // --> args contain ALL the data newman provides to this script.
    var responseBody = args.response.stream,
        response = responseBody.toString();
    allResponse.push(JSON.parse(response));
   }
 })

.on('done', function (err, summary) {
fs.writeFile(outputFilename,"");
for(var i =0;i<allResponse.length;i++)
{
    fs.appendFileSync(outputFilename,JSON.stringify(allResponse[i],null,5));
}
});

Note that, the above code will save only the responses. Other data like request , or URl can be extracted in a similar manner. To run this, install newman in the same directory as the script, then run the script using -

node file_name.js