简体   繁体   中英

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. 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.

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 )

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);
                    }

                )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM