输出responseBody与邮递员集合中的newman脚本一起

编程入门 行业动态 更新时间:2024-10-23 20:25:19
本文介绍了输出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

穷举的引用:

www.getpostman/docs/v6/postman/scripts/postman_sandbox

www.npmjs/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脚本一起

本文发布于:2023-10-29 03:59:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:邮递员   脚本   responseBody   newman

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!