繁体   English   中英

通过Newman运行Postman脚本时出现错误

[英]Getting error when running Postman Script via Newman

我试图通过纽曼运行以下邮递员脚本以将响应写入文件:

  //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());

该请求给出响应,但是在写入文件时出现以下错误:1? 测试脚本中的TypeError

    ┌─────────────────────────┬──────────┬──────────┐
    │                         │ 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"

请帮忙

邮递员本身无法执行这样的脚本。 要保存所有api请求的响应,可以创建一个nodeJS服务器,该服务器将通过newman调用api,然后将响应保存到本地文件中。 这是一个例子-

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));
}
});

请注意,上面的代码将仅保存响应。 可以以类似方式提取其他数据(例如request或UR1)。 要运行此脚本,请将newman与脚本安装在同一目录中,然后使用-运行脚本

node file_name.js

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM