I am very sorry if this has already been covered. I have found conflicting information online and just want to know if this is an actual available feature or not.
I want to know if it’s possible to have global functions at the Collection level folder (either in the pre-request or tests section) where I can store any functions I use for nearly every test.
For example say for every request I do the following:
console.log(‘hello!’);
could I instead create a function in say the pre-request section at the Collection level:
function hello() {
console.log(‘hello!’)
}
and they for all my requests I could just call hello(); (I know this isn’t a very practical example) but you know what I mean.
I have also tried this via postman.setGlobalVariable but it did not seem to work either.
Simply defining a function inside the collection pre-request / tests will not make it available everywhere (but it would be nice ) .
The only way I know to reuse code / define it once run it multiple times is to pack the respective function is a Postman variable and when needed to use eval to run it.
Hi @vdespa - thanks for your reply. Your suggestion is what I believe my colleague and I ended up doing. I had come back to this thread to provide an update
In the Collection Pre-request script section I add my function(s):
postman.setGlobalVariable("regexFunction", () => {
let regex = /<td>[0-9]{8}<.td>/g;
let regexMatch = bodyResponse.match(regex);
return match[0];
});
That is on the todo list for sure. Not trying to derail my own thread, but if you have any resources that helped you achieve that I’d like to see them.
The problem had perplexed me for a while until I found the common way mentioned above. However, it still leaves a warning icon for each eval line, which indicates “eval can be harmful” in the postman interface.
Recently, I’ve found another way and post it here:
Users can create a prototype object with the proper function you want in the pre-request script section, like this:
However, you don’t seem to be able to set variables (at any scope - maybe global, but that is undesirable) inside such functions. The value is not seen outside.
Oct. 2020 and I’m dealing with the same. The reason I’m even needing this is to simplify calls like the following with a helper function. Anyone know a more elegant solution or are we still hooking with Object.prototype?
// The order of function calls below determines the order of precendence
function replaceContext(val) {
return pm.variables.replaceIn(pm.environment.replaceIn(pm.collectionVariables.replaceIn(val)));
}
utils = {
sayHello: function (){
console.log(Hello! ${name});
}
};
But I have found that pm.environent.set doesn’t work in this function you created if that function is in a collection script, it works if that function is in the request
I can get the variable using pm.environent.get but not set it
I think this is an issue with scope. I was logging the variable and the pm.environment.get() and both would match but once the execution continued it was gone, This might be a bug. I have a variable that needs to be set in the prerequest. I am trying to get the variable to set so I can reference it in the body with {{variable}}. return and capture it in the request I am able to save it.
collection script:
function (arg){
do something
return arg;
What is the overhead here using all functions in the Collection folder level? For all requests, the functions will be redefined over and over again, doesn’t it?
Example:
We have 100 functions written at the collection folder level.
We have 1000 requests
Every time when a request is called, the 100 functions will be redefined.
So, part of this works as advertised and I’m able to define a prototype on collection or folder level under test scripts
Object.prototype.setGlobalVariable = (value) => {
pm.globals.set('globalVariable', value);
console.log(pm.globals.get('globalVariable')); // console returns the actual value when this function runs on collection level.
};
…and call it on request level
pm.SetGlobalVariable('bleep');
…however, if, on the next line, I run:
pm.globals.get('globalVariable');
… console shows ‘undefined’ when trying to get the global variable value on request level.
So, currently, I’ve found no way to set a global/environment/collection variable from inside a function and then get the value back outside the function… very weird.
To carry on with the strategy to use Object.prototype.myFunction = function(){};
I was having the same issue with not able to write any collection variables within this defined function. I discovered if I pass in the ‘pm’ instance, it appears to work!
In Collection Pre-request Script:
Object.prototype.myGlobalFunction = function(myPm){
myPm.collectionVariables.set("myKey", "my value");
}
In Request Pre-request Script:
_.myGlobalFunction(pm);
Check the collection variables, and you now have a populated variable from the function prototype.
Just note that settimeout will not work inside this function. They messed up something with scoping… I am using settimeout for retry logic (try 3 times, 1000ms, 2000ms, 3000ms then fail)