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
-
In utils.myFunction:
pm.environment.set(varName, varValue);
pm.environment.get(varName);
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