Environment variable is not set correctly in request URL

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:

  1. executing a bunch of requests before steps 2 and 3 (no impact on processing logic of steps 2 and 3)
  2. saving a list of document numbers from the response as an array via the test script in an environment variable.
  3. 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

This will be a scope issue.

You have an environment variable called the same name as the variable in the pre-request script, which is also called the same in the tests tab.

The local variable in the pre-request script, will still be available to the tests tab.

Just call the environment variables something different to the local variables you have in the pre-request and test script tabs to avoid any potential of scope issues.

Something like currentDocument and documentArray.

You donโ€™t need to have your IF statement with the setNextRequest in the test script for step 3. It can go at the end of the pre-request script as this only controls what request is run next after all of the code in the pre-request script and tests tab are complete.

Other observations:

  1. You donโ€™t need to loop through the response to pull out the document numbers. You can use the JavaScript map function instead. (Which will be a one liner). Or you can use the Lodash map function (as Lodash is available in the Postman Sandbox).

  2. Your test checking for the response in step 2 should be at the top of the script. The rest of the code should be held within that test case name. The way assertions work is they will stop executing any more code in that test block as soon as the first assertion fails. This way, none of the other code will execute if you donโ€™t receive a 200ok status.

If you post an example response for step 2, I can show you a working example using Postman Echo.

2 Likes

First of all, thanks a lot for the fast response and the general tips, I appreciate that.

Your assumption was right, but the location which caused the scope issue was another one. I used the same variable name โ€œdocumentโ€ in a request from step 1 with a different scope. Now it also makes sense that steps 2 and 3 worked when running separately from step 1.

After changing the variable in step 1, it worked.

Thanks!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.