How to export / load Body Response to an external file from Runner Collection Runner results?

I am working on a project where I have to hit the web service several times with different variable values

For example, http://mywebservice.com?variable1= {{value}}

and different values ​​are transmitted using the Postman mail collector.

I want to load the response body of all requests into a file. How to do it?

+5
source share
5 answers

, . , , . , .

Send and Download, :

enter image description here

, , , , , .

pm.globals.set('response_body', JSON.stringify(pm.response.json()))

, - , .

Manage Environment.

+4

, scripts Postman , .

. Newman.

, POST . POST- .

+1

- http://blog.getpostman.com/2017/09/01/write-to-your-local-file-system-using-a-postman-collection/, - nodeServer. , ( Postman) URL.

var fs = require('fs'),
newman = require('newman'),
allRequest =[],
allResponse = [],
allName = [],
requestUrl = "",
allRequestUrl = [];


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

    //console.log(args);    // --> args contain ALL the data newman provides to this script.
    var requestBody = args.request.body, 
        request = requestBody.toString(); 

    allRequest.push(JSON.parse(request)); 
    var responseBody = args.response.stream,
        response = responseBody.toString();
    allResponse.push(JSON.parse(response));

    var nameBody = args.item.name;
    allName.push(nameBody);
    var protocol = args.request.url.protocol;
    var host = args.request.url.host;
    var path = args.request.url.path;
    requestUrl+=protocol+"://";
    for(var j = 0;j<host.length;j++)
    {
        requestUrl+= host[j];
        if(j!=host.length-1)
        {
        requestUrl+=".";
        }
    }
    requestUrl+='/';

    for (var k =0;k<path.length;k++)
    {
        requestUrl+= path[k];
        if(k!=path.length-1)
        {
        requestUrl+= "/";
        }
    }
    allRequestUrl.push(requestUrl);
}
 })
 .on('done', function (err, summary) {
fs.writeFile('test.html',"");
//modify html output here.
for(var i =0;i<allRequestUrl.length;i++)
{
    fs.appendFileSync('test.html', "<br><h>Name:  </h>");
    fs.appendFileSync('test.html',allName[i]);
    fs.appendFileSync('test.html', "<br><h>URL:  </h>");
    fs.appendFileSync('test.html',"\"" + allRequestUrl[i] + "\"");
    fs.appendFileSync('test.html', "<br><h>Request</h><br>");
    fs.appendFileSync('test.html',JSON.stringify(allRequest[i],null,4));
    fs.appendFileSync('test.html', "<br><h>Response</h><br>");
    fs.appendFileSync('test.html',JSON.stringify(allResponse[i],null,5));
//fs.writeFileSync('migration-report.json', JSON.stringify(results, null, 4));
}
});

, newman, CLI Postman. npm , newman -

    npm install newman

js 'filename.js' -

    node filename.js

, , "test.html" .

+1

, , .JSON Collection Runner.

( Postman) , responseData, [].

"" Builder . Postman , , JSON API .

var jsonData = JSON.parse(responseBody);
var old = pm.environment.get("responseData");
old = JSON.parse(old);
// filter jsonData if needed

old.push(jsonData);
old = JSON.stringify(old);
pm.environment.set("responseData", old);
console.log(pm.environment.get("responseData"));

JSON , (. ).

: responseData [] Collection Runner, .

+1

CLI newman

JSON. newman :

sudo npm install -g newman

, HTML- , newman-reporter-html .

sudo npm install -g newman-reporter-html

, :

newman run <path to your collection json file> -e <path to your environment json file> -r cli,html

By default, the HTML file will not contain the body of the request and response. To do this, first load the default steering wheel template, and then tweak it a little. You can find the default steering pattern here . Download the file and save it as template.hbs. Then open it in any editor and find the code in which it displays Status Code. It might look like this:

<div class="col-md-12">&nbsp;</div>
<br/><div class="col-md-4">Status code</div><div class="col-md-8">{{response.code}}</div><br/>

Add the following lines below this part:

<div class="col-md-12">&nbsp;</div>
<br/><div class="col-md-4">Request body</div>
<div class="col-md-8">
    <textarea class="json" disabled rows="8" cols="70">
        {{request.body}}
    </textarea>
</div><br/>
<div class="col-md-12">&nbsp;</div>
<br/><div class="col-md-4">Response body</div>
<div class="col-md-8">
    <textarea class="json" disabled rows="8" cols="70">
        {{response.body}}
    </textarea>
</div><br/>

Now you can run the following command to render HTML with the body of the request and response:

newman run <path to your collection json file> -e <path to your environment json file> -r cli,html --reporter-html-template template.hbs

Hope this helps!

0
source

Source: https://habr.com/ru/post/1691784/


All Articles