Search code examples
postmanpostman-collection-runnerpostman-pre-request-script

Environment variable is not set properly during Postman Runner


I have a pre-request script that increments an enviroment variable to generate different ids each time a request is generated. Also, I have a runner that calls this request multiple times:

pre-request script for the Postman request

However, I’ve noticed that during the runner the environment variable is not updated, at least in the environment’s tab:

enter image description here

But I monitor the variable in the console log and it is incrementing correctly:

enter image description here

My problem is that if I prematurely stop the runner before all the requests end, the environment variable didn’t register all the sets that were executed, and such it keeps the value it had at the beginning of the execution. This may be a known issue or it might just be me using the tools in the wrong way, does anyone have any advice? I can manually set the variable to the value I need after each run but it would be better if the last line of the pre-request script

pm.environment.set(“runnerCounter”, Number(pm.environment.get(“runnerCounter”)) + 1);

always worked by setting the environment variable properly.

Also, I did check the Keep variable values as true, so as to persist any variable changes, even though I’m not sure this is necessary when it comes to environment variables.

enter image description here


Solution

  • pm.variables.set is for setting local variables

    to set environment variable use pm.environment.set

    Note that , it will set only the current value . INitial value remains the same

    read more about postman variables

    https://learning.postman.com/docs/sending-requests/variables/