输出responseBody与邮递员集合中的newman脚本一起 [英] output responseBody somewhere with newman script from postman collection

查看:201
本文介绍了输出responseBody与邮递员集合中的newman脚本一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am attempting to run a script.js with newman from a locally saved postman collection. In postman the call works, and returns a response body token that I need access to.

我不在乎响应主体是如何返回的,我只是不想开邮递员,如果不需要的话。

I don't care how the response body is returned I just don't want to open postman if I don't have to.

我一直遇到错误 ReferenceError:未定义responseBody

在此问题上的任何帮助将不胜感激。

Any help on this matter would be really appreciated.

$节点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.log JSON.parse 似乎可以解决问题,因为 responseBody 似乎没有从头开始定义

neither console.log or JSON.parse appear to be doing the trick because responseBody doesn't appear to be defined from the start

穷举的引用:

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

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

如何使用Newman API获取URL的整个html或json回复

推荐答案

邮递员集合是一个集合

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

因此,在回调函数中记录/解析responseBody是不正确的(逻辑上对此进行说明)

Thus, logging / parsing the responseBody in a callback function is incorrect (Stating this logically).

按照 Newman Docs 的规定, .run 函数的回调使用 err summary

As per the Newman Docs it states that the .run function's callback is called with two parameters that are err and summary

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

The summary argument in the callback contains the whole summary for the run and you can follow the documentation if you want to make use of that summary.

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

Now, What you're trying to do is basically log the response of the request(s).

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

You need to write a console.log(responseBody) / JSON.parse(responseBody) inside the test scripts for each request in the collection and then on running the collection using newman, each responseBody for each request will be logged out / parsed as per your needs.

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

To access the summary you can modify your function like so :

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

这篇关于输出responseBody与邮递员集合中的newman脚本一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆