Hi Postman community, my organization has been slowly adopting Postman for regression testing. This is the first issue we’ve run into that I am hoping to verify if it’s a bug we’re seeing or is unsupported in Postman.
My question:
Are there any known issues or limitations to environment variables getting set in a folder’s Pre-Request Script code that would prevent the value being accessible in a request nested down in the folder structure?
Details: Collection structure
→ Parent folder (utils.myFunction() method defined in Pre-Request Script)
----> Child folder
--------> Grandchild folder (contains POST request calling utils.myFunction(varName, varValue) in Tests)
Code logic (simplified):
POST request calls utils.myFunction(varName, varValue) in Tests
This seems to be working as expected within the function
Returning back to the POST request after calling utils.myFunction method:
pm.environment.get(varName);
*This DOES NOT seems to be working - it is returning the prior value of varName before calling utils.myFunction)
How I found the problem:
We’re trying to consolidate redundant code within multiple requests into a common library of shared functions, which we’ve placed in the Pre-Request Script section of a higher-up folder within the collection. The collection starting failing after making this change.
I’ve already tried:
Searching online to verify if this is a known issue or unsupported option within Postman
Using console.log() messages throughout the code to verify the observations noted above
Verified the environment variable’s “Initial Value” is empty
Encountering the same issue.
If there is some technical limitation that precludes us from setting environment variables in this way it’s fine, but please document this in the official documentation.
I would really like to know this too, because I am experiencing the same exact thing: a function in a parent’s Pre-Request Script sets environment variables, which persist when retrieved within that same function, but not when retrieved in a child’s (in my case, request’s) Tests script.
I can replicate this, it seems the postman instance is not global. If i have a function in a pre request script on a parent folder that sets a variable (can be global, environment etc) and try and read this using pm.get in the child, the variable is undefined. If i pass the pm instance to the parent function to and use this to set the variable and then read it using the same instance in the child it works and this is for every variable type. I would have assumed that using the globals collection would have persisted across folders. Not sure if i’m missing something should i be using pm.globals.set ?
If you want to create a global function that needs to incorporate pm methods, then you need to pass the pm function. (Otherwise pm will be undefined within that global function).
Pre-request on folder 1
utils = {
statusCode: function (pm, code) {
pm.test(`Status code is ${code}`, () => {
pm.response.to.have.status(code);
})
return utils;
},
response: function (pm) {
console.log(pm.response.json());
return utils;
},
environment: function (pm, variable, value) {
pm.environment.set(variable, value);
return utils;
}
};
Hi thanks, yep found out by passing it to establish if pm was global or not. Was wondering if that was correct as it feels a little odd having global collections that are tied to an instance.
Good to know other people are doing this as was wondering if it was correct. Thanks for the help