[英]How do I handle asynchronous calls when running newman (Postman) collections with node?
[英]Is it possible to create or update postman test scripts or variables when running newman from node js?
我的 postman collections 有多個帖子調用,他們返回 pdf 作為響應。 使用 newman & node js 我正在運行將 pdf 解析為文本的集合。 我想將此文本放入環境變量或測試腳本中,以便可以執行進一步的檢查。
我正在嘗試這樣做,以便整個請求、響應和驗證都發生在同一個請求中。 (而不是https://community.postman.com/t/pdf-parsing-with-postman-and-express-js/17938 )
我注意到可以使用以下腳本更新請求 header。 同樣,是否可以在執行后更新或設置變量或測試腳本(例如在 request 或 beforeTest 或 beforeScript 事件中)..?
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 訪問和修改測試腳本
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.