Hi,
my question relates to Environment variable scope when set in a function defined in a collection or folder prerequest script but is different.
I have a monitor where I use a collection of 4 requests.
Each request has a test subsection where the behaviour should adapt would the previous run have failed (you read well: run, not request).
I read the documentation about the scope of variables and to my understanding, this should be possible if I use pm.globals to store the result between the runs (with the need to be careful with the naming of the variables to avoid overwriting them in another monitor).
Yet, the first statement in my first pre-req script is as follows:
if (pm.globals.has("lastRunStatus")){
console.log("PRE: lastRunStatus: "+pm.globals.get("lastRunStatus"));
} else {
pm.globals.set("lastRunStatus", 'Fail');
}
and the last statement in my last request test:
pm.globals.set("lastRunStatus", currentRunStatus.toString());
console.log("lastRunStatus: "+pm.globals.get("lastRunStatus"));
Unfortunately, I don’t see the variable in the “globals” and I don’t see the first log in the console… and the variable is always ‘Fail’ as initialized in the else branch.
Do you know what I’m doing wrong? My understanding is, as explained in the related question, that the environment variable will not persist between runs, so I have to use a global variable.
Do I need to create it in the UI before I can use it in my monitor? My monitor is designed to work outside of any environment, I always check whether a variable is defined, then I use a syntax as
let blabla = pm.[scope].get('blabla')
thanks a lot!