【问题标题】:output responseBody somewhere with newman script from postman collection使用邮递员集合中的纽曼脚本在某处输出 responseBody
【发布时间】:2018-09-08 09:20:22
【问题描述】:

我正在尝试使用本地保存的邮递员集合中的 newman 运行 script.js。在邮递员中,调用有效,并返回我需要访问的响应正文令牌。

我不在乎响应正文是如何返回的,我只是不想在不需要的情况下打开邮递员。

我一直遇到ReferenceError: responseBody is not defined的错误

非常感谢您在此问题上的任何帮助。

$ node script.js

var newman = require('newman'); // require newman in your project

// call newman.run to pass `options` object and wait for callback
newman.run({
    collection: require('./pathto/my_coll.postman_collection.json'),
    reporters: 'cli'
}, function (err) {
    if (err) { throw err; }
    // console.log(responseBody);
    JSON.parse(responseBody);

});

console.logJSON.parse 似乎都没有起作用,因为 responseBody 似乎从一开始就没有定义

用尽参考:

https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox

https://www.npmjs.com/package/newman

how to get whole html or json repsonse of an URL using Newman API

【问题讨论】:

    标签: javascript node.js postman newman


    【解决方案1】:

    应该可以通过解析缓冲流来实现:

    var newman = require('newman');
    newman.run({
        collection: require('./C1.postman_collection.json'),
        reporters: 'cli'
    }, function(err, summary) {
        if (err) {
            throw err;
        }
        summary.run.executions.forEach(exec => {
            console.log('Request name:', exec.item.name);
            console.log('Response:', JSON.parse(exec.response.stream));
    
        });
    });
    

    【讨论】:

      【解决方案2】:

      邮递员集合是请求的集合。

      您正在运行整个集合(这是由 Newman 一起运行的一系列请求)

      因此,在回调函数中记录/解析 responseBody 是不正确的(从逻辑上讲)

      根据Newman Docs,它声明 .run 函数的回调使用两个参数调用,即 errsummary

      回调中的 summary 参数包含运行的整个摘要,如果您想使用该摘要,可以按照文档进行操作。

      现在, 您要做的基本上是记录请求的响应。

      您需要在 测试脚本 中为集合中的每个请求编写 console.log(responseBody) / JSON.parse(responseBody),然后在使用 newman 运行集合时,将记录每个请求的每个 responseBody根据您的需要输出/解析。

      要访问摘要,您可以像这样修改函数:

      var newman = require('newman');
      newman.run({
          collection: require('./C1.postman_collection.json'),
          reporters: 'cli'
      }, function (err, summary) {
          if (err) { throw err; }
          console.log(summary);
      });
      

      【讨论】:

      • 感谢您提供非常详细且内容丰富的答案。将console.log(responseBody); 添加到我的Tests 选项卡后,然后将我的收藏重新导出到本地位置。我能够打印对 shell 的响应。非常感谢。
      【解决方案3】:

      您可以尝试console.log(summary.run.executions) 并从那里深入了解它。 Newman 脚本并不真正知道 responseBody 在该上下文中是什么,因此它不知道要注销什么。

      查看纽曼文档了解更多信息https://github.com/postmanlabs/newman/blob/develop/README.md#cli-reporter-options

      【讨论】:

      • 感谢您的意见。 ReferenceError: summary is not defined
      • 也许我从错误的目录运行node 脚本?
      • Newman 是否安装在全局范围内?如果是,那应该没问题。查看文档并尝试不同的选项来评估不同的输出。
      • stackoverflow.com/questions/40120232/… 有助于将 execution.response.stream 转换为 JSON/字符串
      猜你喜欢
      • 1970-01-01
      • 2020-01-21
      • 2020-12-14
      • 2017-09-15
      • 1970-01-01
      • 2018-03-09
      • 2021-12-08
      • 2017-06-14
      • 2021-09-27
      相关资源
      最近更新 更多