使用邮递员集合中的纽曼脚本在某处输出 responseBody
我正在尝试使用本地保存的邮递员集合中的 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 is not defined
非常感谢您在此问题上的任何帮助.
Any help on this matter would be really appreciated.
$ 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.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 响应
推荐答案
邮递员集合是请求的集合.
A postman collection is a collection of requests.
您正在运行整个集合(这是由 Newman 一起运行的一系列请求)
因此,在回调函数中记录/解析 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
回调中的 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).
您需要在 test scripts 中为每个请求编写 console.log(responseBody
)/JSON.parse(responseBody)
集合,然后在使用 newman 运行集合时,每个请求的每个 responseBody 都将根据您的需要注销/解析.
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);
});
相关文章