I'd like to declare a function once in the pre-request script of my first postman request and then use it in every request thereafter. I've set plenty of variables on the postman object and as environment variables but I haven't found a way to do the same with functions.
In the pre-request script:
function wrapTest(param1, param2, param3) {
...
}
Then I've tried
postman.prototype.wrap = wrapTest;
postman.wrap = wrapTest;
postman.setGlobalVariable("wrap", wrapTest);
In the request I'm attempting to use this function:
postman.wrap(one,two,three);
which results in "postman.wrap is not a function" in all cases.
We can add, update, get or remove global and environment variables at runtime using scripts in Postman. We use a global function named “pm. *” of Postman Sandbox environment. We can do it in both Pre-request Script and Tests.
Postman, which is used for various activities, supports the following variable scopes. Local is the smallest variable scope, followed by Data, Environment, Collection, and Global. Global Variables functions outside of the environment and are not affected by it.
The function can be saved as a string and then evaled when it's used.
var stringWrap = function wrapTest(param1, param2, param3) {
...
};
postman.setEnvironmentVariable("wrap", stringWrap);
var parsedFunc = eval("("+environment.wrap+")");
parsedFunc("1", 2, 3);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With