I’ve ilustrated my question in the following scenario:
i have collection pre-request where i define an object that contains a function that returns the value of an environment variable.
In my request, i have a pre-request script where i set the environment variable, then call the object.function of the collection pre-request. I would have expected the value to be defined, as i defined earlier in the execution. But it is not defined. Please help me understand this.
Details (like screenshots):
The collection Pre-request script
The request pre-request script
The environment (i need the “oneVar” to be without an initial value, as i need to set this in the request pre-request script)
The console log:
In the environment print, the “oneVar” is not defined. If i print it in the request pre-request script, both variables are defined.
How I found the problem:
I need to dynamically build the parameters to multiple requests, and i am using functions that i defined at the collection level.
I’ve already tried:
using globals and/or collections variables, and i have the same result.
I suspect the collections-pre-request object somehow uses another instance of pm.environments. One that was instanced when the script is run. And even if the execution order seems to be:
- collections script: initialize utils
- request script: set variables
- request script: use utils.function
i suspect that in the third step, utils object somehow uses the environment instance of it’s own moment of instantiation (at step 1).