1

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

Matt
  • 144
  • 15

1 Answers1

4

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/

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • I am using pm.environment.set though, not sure what you meant. I used pm.variables.set for my local variables, but I did use pm.environment.set for my environment variable. – Matt Aug 23 '21 at 02:08
  • @user3091996 how do you determine its not update – PDHide Aug 23 '21 at 07:34
  • only the second column current value will be updated. initial value remains the same – PDHide Aug 23 '21 at 07:34
  • But when I use pm.environment.get it returns the current value, so it's ok that initial value remains the same, because I'm interested in current value – Matt Aug 23 '21 at 14:00