My question:
I would like to assign my variables, nothing more.
Details (like screenshots):
I do: Create a new request (there is no other place in the application to work in):
var uuid = require('uuid');
pm.collectionVariables.set("transactionId", uuid.v4().toString("B"));
console.log(pm.collectionVariables.get("transactionId"));
throw new Error("enough"); //read https://community.postman.com/t/stop-request-from-sending-if-pre-request-test-fails/
How I found the problem:
Collection variable retains old value. If this variable was not existing, this variable is not created.
The Console logs a new GUID always, but seemengly after throw, variables are reset to pre-run state.
What I tried else
I tried to specify urls:
GET https://example.com
GET https://httpstat.us/201
But it is slower, and I want to avoid bothering these folks with senseless calls.
What I want
Assign GUID to my variable to have it available through several sequential scripts.
This means that the whole script is evaluated first before its run and as you’ve found out you can’t guarantee that the throw new error will run after the other lines of code.
There are various topics in the forum in relation to the async nature of Postman with various workarounds using IF statements to enforce the running order which may or may not work for you.