从节点 js 运行 newman 时,是否可以创建或更新 postman 测试脚本或变量? [英] Is it possible to create or update postman test scripts or variables when running newman from node js?
问题描述
我的邮递员集合有多个 post 调用,它们返回 pdf 作为响应.使用纽曼&node js 我正在运行将 pdf 解析为文本的集合.我想将此文本放入环境变量或测试脚本中,以便可以执行进一步检查.
My postman collections has multiple post calls, they return pdf as response. Using newman & node js I am running the collection which parses the pdf into text. I want to place this text into an environment variable or into test script so that further checks can be performed.
我正在尝试这样做,以便整个请求、响应和验证都发生在相同的请求中.(而不是 https://community.postman.com/t/pdf-parsing-with-postman-and-express-js/17938)
I am trying to do this way so that the entire request, response and validation happens in the same requests. (instead of https://community.postman.com/t/pdf-parsing-with-postman-and-express-js/17938)
我注意到可以使用波纹管脚本更新请求标头.同样,是否可以在执行后更新或设置变量或测试脚本(例如在 request 或 beforeTest 或 beforeScript 事件中)..?
I noticed its possible to update the request header using bellow script. Similarly would it be possible to update or set variable or test script after the execution (for instance in request or beforeTest or beforeScript events)..?
const newman = require('newman');
PostmanHeader = require('postman-collection').Header;
newman.run({
collection: require('./myCollection.json'),
reporters: 'cli'
})
.on('beforeItem', (error, data) => {
console.log("BeforeUpdate------------------");
console.log(data.item.request.headers.members);
var myHeaderVal = 'Test123';
additionalHeader = new PostmanHeader({
key: 'CustomHeader',
value: myHeaderVal
});
data.item.request.headers.members.push(additionalHeader);
console.log("Updated------------------");
console.log(data.item.request.headers.members);
}
)
谢谢
推荐答案
const newman = require('newman'); // require newman in your project
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: require('./test.postman_collection.json'),
reporters: "cli",
}, function(err) {
if (err) {
throw err;
}
console.log('collection run complete!');
}).on('beforeTest', (error, data) => {
console.log("BeforeUpdate------------------");
data.events[0].script.exec = ["pm.test(\"Status code is 500\", function () {\r", " pm.response.to.have.status(00);\r", "});"];
});
您可以使用 beforeScript 或 beforeTest 来访问和修改测试脚本
you can use beforeScript or beforeTest to access and modify test scripts
这篇关于从节点 js 运行 newman 时,是否可以创建或更新 postman 测试脚本或变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!