首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Newman运行Postman脚本时出现错误

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

Stack Overflow用户
提问于 2018-07-25 21:41:50
回答 1查看 2.1K关注 0票数 0

我正在尝试通过Newman运行以下Postman脚本,以将响应写入文件:

代码语言:javascript
复制
  //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

代码语言:javascript
复制
    ┌─────────────────────────┬──────────┬──────────┐
    │                         │ 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"

请帮帮忙

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-30 20:06:30

Postman本身不能执行这样的脚本。要保存对所有api请求的响应,您可以创建一个nodeJS服务器,该服务器将通过newman调用api,然后将响应保存到本地文件。下面是一个例子-

代码语言:javascript
复制
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或URl。要运行此脚本,请将newman安装在与脚本相同的目录中,然后使用-

代码语言:javascript
复制
node file_name.js
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51520442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档