Hi,
I am facing a strange problem when trying to loop through a list of numbers that was previously set in an environment variable.
Why is the environment variable not set correctly in the URL, although it has been set in the pre-request script and seems correct, according to the console logs in procedure 1 (details see below)?
Steps performed - using the collection runner:
- executing a bunch of requests before steps 2 and 3 (no impact on processing logic of steps 2 and 3)
- saving a list of document numbers from the response as an array via the test script in an environment variable.
- afterwards I use array.shift() in the pre-request script of the following request to loop over the array from step 2) until all elements have been processed. The current number should be used in the request URL of step 3) to execute a GET to retrieve detailed information of the document.
I have used following code:
Test script of step 2):
var response = JSON.parse(responseBody);
var documents = [];
for (var i = 0; i < reponse.value.length; i++) {
var document = response.value[i].documentNumber;
documents.push(document);
}
pm.environment.set("documents", documents);
console.log("documents: " + documents);
pm.test(pm.variables.replaceIn("Documents created: {{documents}}"), function () {
pm.response.to.have.status(200);
})
Pre-request script of step 3):
let documents = pm.environment.get("documents");
let document = documents.shift();
pm.environment.set("document", document);
console.log("Current document pre-request: " + pm.environment.get("document"));
pm.environment.set("documents", documents);
console.log("Current list pre-request: " + pm.environment.get("documents"));
Test script of step 3):
var document = pm.environment.get("document");
console.log("Current document test: " + document);
pm.test(pm.variables.replaceIn("Document details read: {{document}}"), function () {
pm.response.to.have.status(200);
})
var documents= pm.environment.get("documents");
if (Array.isArray(documents) && documents.length > 0) {
postman.setNextRequest("step 3");
}
The strange behavior:
procedure 1)
If I execute steps 1, 2 and 3 together in one collection run, the loop in step 3 is not executed correctly and only the last array element of the documents list is used in the URL.
procedure 2)
However, If I run all the requests from step 1 and run step 2 + 3 via a seperate collection run subsequently, the loop works fine. How is it possible that the outcome is different compared to procedure 1, since I have used the logic with array.shift() in the exact same way many times without any issue. In fact, I am using this logic for looping through a different list which is part of step 1) in the same way and it is working as expected, using identical code, except that the variable names are different.
I have added the console logs to the code to see the behavior of the variables and everything looks correct. The only issue is, that the environment variable, which should be updated in the pre-request is not used in the URL when using procedure 1). When using procedure 2), the console logs look identical, but the current document number is set correctly.
See screenshots:
procedure 1)
numbers logged in console and used via variable in URL are not matching, both times the last array element is used in the request. Why?
procedure 2)
working correctly
Attempted solutions:
- using collection variables instead of environment โ didnโt work
- deleting the environment and creating everything from scratch โ didnโt work
Version used:
Postman for Windows (Win 10 Pro)
Version
10.22.9
UI version
10.22.9-ui-240129-0641
Desktop platform version
10.22.0
Architecture
x64
OS platform
win32 10.0.19045
Thanks in advance for your help.
Regards