When a JSON file of a collection is executed through Newman then A global variable cannot retrieve its value which is assigned through the Pre-request script. Tests are shown as failed as the value of that global variable was not retrieved.
If anyone can share any solution to this issue, it will be very helpful.
I tried pm.globals.set("variableName", variableValue) in a pre-request script, and then pm.globals.get("variableName) in the test script, and that seems to work when running the collection via Newman.
Thanks Joyce for your reply. Let me explain this issue in detail.
GET Location/getDetails?security_key={{securitykey}}&locationID={{RecentLocationID}}
I wanted to automate this API. From another API response I get an array of locations and counting the array length I assign that value to a global variable - X ( i.e. pm.globals.set(âVariable Nameâ, âVariable Valueâ); ). I iterate the run of this âgetDetailsâ API for X times where X is the number of total locations. In every iteration I changed the value of âRecentLocationIDâ variable through pm.globals.set in the âTestsâ section.
Everytime I run the iterations using âCollection Runnerâ, I can successfully run the iterations. But when I use newman for executing the tests then it returns an error for the variable âRecentLocationIDâ.
As the execution can be properly done, using âCollection Runnerâ but why I cannot execute properly using newman? This was my query.
I re-created your similar scenario, and seems to be working for me. Two things I would doubly confirm:
If youâre iterating newman runs, like with a script or something, note that global variables are not persisted across collection runs (but should work fine within the collection run).
Also, you canât import existing global variables into a newman run, but you can create them during a newman run.
However, from your description, sounds like youâre iterating within the same newman run, and also creating the new global variable during the run too. Can you confirm?
The last check I would do is to add some console.log() statements perhaps with your array, for some additional visibility. You can see the logs in the Postman console or your terminal when running with the Collection Runner or Newman, respectively. And see how they compare.