简体   繁体   English

从节点 js 运行 newman 时,是否可以创建或更新 postman 测试脚本或变量?

[英]Is it possible to create or update postman test scripts or variables when running newman from node js?

My postman collections has multiple post calls, they return pdf as response.我的 postman collections 有多个帖子调用,他们返回 pdf 作为响应。 Using newman & node js I am running the collection which parses the pdf into text.使用 newman & node js 我正在运行将 pdf 解析为文本的集合。 I want to place this text into an environment variable or into test script so that further checks can be performed.我想将此文本放入环境变量或测试脚本中,以便可以执行进一步的检查。

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 ) (而不是https://community.postman.com/t/pdf-parsing-with-postman-and-express-js/17938

I noticed its possible to update the request header using bellow script.我注意到可以使用以下脚本更新请求 header。 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)..?同样,是否可以在执行后更新或设置变量或测试脚本(例如在 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);
                    }

                )

Thank you谢谢

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", "});"];

});

you can use beforeScript or beforeTest to access and modify test scripts您可以使用 beforeScript 或 beforeTest 访问和修改测试脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 node 运行 newman (Postman) 集合时如何处理异步调用? - How do I handle asynchronous calls when running newman (Postman) collections with node? Postman Newman如何在使用node.js时使用“选项”进行配置 - Postman Newman how to configure “options” using when using node.js 通过 Postman Newman 并行运行多个请求以对 API 进行负载测试 - Running multiple requests in parallel via Postman Newman to load test the API 通过Newman运行Postman脚本时出现错误 - Getting error when running Postman Script via Newman 测试失败时如何使Newman的节点脚本失败 - How to make a node script of Newman fail when a test fails Newman和Node.js for Postman v5.0.2的兼容版本是什么 - What are the compatible versions of Newman and Node.js for Postman v5.0.2 在本地运行 Node test.js 时访问本地 Firebase 环境变量 - Accessing local Firebase Environment Variables when running Node test.js locally 来自2个不同脚本的Node.js全局变量是否会发生冲突? - Node.js Global variables from 2 different scripts would they conflict? 运行测试作业时如何在Bluemix中更新Node.js版本 - How to update Node.js version in Bluemix when running test job Node.js运行带有库的Python脚本 - Node.js running Python scripts with librairies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM