Environment Variable values not getting set from grand-parent folder's Pre-Request Script

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):

  1. POST request calls utils.myFunction(varName, varValue) in Tests

  2. In utils.myFunction:

pm.environment.set(varName, varValue);
pm.environment.get(varName); 

This seems to be working as expected within the function

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

Hello, just checking back if anyone is able to provide some insight on this issue. Thanks!

1 Like

hi @edgetonjw,

Did you try using collection variables? This might solve your issue and the variable scope is within the collection.

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.

1 Like

Hey @edgetonjw :wave:

Are you able to give a visual example of what that looks like and the full utils function that you have created at the Collection level?

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.

Hey all.

I’m not able to replicate this issue on my end. Can you confirm that:

  • The env variable you set was indeed set in your environments tab
  • Before trying to use the set variables, you have set the right “active” environment and that,
  • You’re using the right environment variable key

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 ?

@Andrewdeakins

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

image

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

Post-response in Get request

utils.response(pm);
utils.statusCode(pm, 200);

utils.environment(pm, "testVar", "testValue")
console.log(pm.environment.get("testVar")); // testValue

utils.environment(pm, "testVar", "testValue2")
console.log(pm.environment.get("testVar")); // testValue2

image

image

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

1 Like