【问题标题】:Getting error when running Postman Script via Newman通过 Newman 运行 Postman 脚本时出错
【发布时间】:2019-01-02 08:44:22
【问题描述】:

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

  //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?测试脚本中的类型错误

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

请帮忙

【问题讨论】:

  • 如果您在 Postman 测试中添加 fs,它将无法工作,因为它不是您可以从应用程序调用的模块之一。 getpostman.com/docs/v6/postman/scripts/…
  • 我在应用程序中添加了代码,但通过 Newman 运行和调用其模块。它不会那样工作吗?如果没有,有没有办法将响应(以文本格式出现)写入 CSV 文件
  • 如果您的应用程序中有任何不支持的内容,它将无法工作。您不能在 Postman 集合之外处理该操作,而是直接将类似的内容添加到 Newman 脚本中吗? Newman 可以选择返回响应数据。
  • 我正在尝试仅通过 newman 进行操作。我正在使用 newman 运行上面共享的代码,并且响应也来自 newman 控制台。如果您可以共享任何可以通过 newman 工作以保存响应的工作代码,那就太好了。
  • 如果您在 Postman UI 中打开集合,上面的代码是否包含 fs 语句,在 Test 选项卡中?如果它在那里,你没有用 Newman 正确运行它。我会将我想用它做什么的 fs 模块添加到这样的东西 - github.com/postmanlabs/newman/blob/develop/…

标签: node.js postman newman


【解决方案1】:

Postman 本身不能执行这样的脚本。要保存所有 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 或 URl 可以以类似的方式提取。要运行它,请将newman 安装在与脚本相同的目录中,然后使用 -

运行脚本
node file_name.js

【讨论】:

  • @ArchitGoyal 如果我的回答对您有帮助,您可以通过单击将变为绿色的空心刻度线来接受我的回答 :)
猜你喜欢
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 2018-01-30
  • 2019-03-29
  • 2021-03-06
  • 1970-01-01
  • 2011-02-09
相关资源
最近更新 更多