Use Global variables as an elements of array in Test

Hi all. I have a global variables and I want to us them as an elements of array in my test. Is it possible

Hi @ghazaryan.nelli,

You should be able to access the global variables using pm.globals.get("variable_key"). If you want more information, it should be available here:
https://learning.postman.com/docs/postman/variables-and-environments/variables/#using-variables-in-scripts

If you’re creating an array, then you could use the pm.globals.get("variable_key") to set the respective elements in your array. I hope this helps!

I know that it’s not correct, but I am trying like this
var photo1 = pm.globals.get(“uploaded_photo_id”);
var photo2 = pm.globals.get(“uploaded_photo_id1”);
var photo3 = pm.globals.get(“uploaded_photo_id2”);

var photoArray = pm.environment.get(“photoArray”);
if(!photoArray){
photoArray = [“photo1”, “photo2”, “photo3”];
}

Now I try to find the solution how to use photo1, photo2, photo3 in my array as expected

@ghazaryan.nelli,

A few things that hopefully provide some help. If you look at the following section here: https://learning.postman.com/docs/postman/variables-and-environments/variables/#understanding-variables-and-environments.

More specifically:

Postman will store environment and global variables as strings. If you’re storing objects or arrays, remember to JSON.stringify() them before storing, and JSON.parse() them when you retrieve them.

I’m assuming your photoArray variable is by default, right? If you’re retrieving it, it will retrieve a String. So you’d do something like:

var photoArray = JSON.parse(pm.environment.get(“photoArray”));

Additionally, regarding your if statement, are you sure your condition is checking what you are expecting? If you’re expecting it to check if the Array is empty I’d suggest reviewing that. Maybe something like photoArray.length == 0 .

Now I try to find the solution how to use photo1, photo2, photo3 in my array as expected

Once you’ve stored the elements in photoArray, you can set the environment variable accordingly with pm.environment.set:

https://learning.postman.com/docs/postman/variables-and-environments/variables/#defining-variables-in-scripts

Hopefully, this helps.

That’s very helpful, thank you so mush

@ghazaryan.nelli Glad that helped!

there are a lot of unneded JSON.stringify and parsing in the request.

This code is working:

var photo1 = pm.globals.get("uploaded_photo_id"); 
var photo2 = pm.globals.get("uploaded_photo_id1"); 
var photo3 = pm.globals.get("uploaded_photo_id2"); 

var photoArray = pm.environment.get("photoArray"); 
if(!photoArray){
   var photoArray = [photo1, photo2, photo3];
}

var currentPhotoArray = photoArray.shift();
pm.environment.set("currentphotos", currentPhotoArray);
pm.environment.set("photoArray", photoArray);

I removed the JSON.parse from the first line

var photoArray = pm.environment.get("photoArray");

if(photoArray && photoArray.length > 0){
    postman.setNextRequest("Submit to Photo Challenge");
} else{
    postman.setNextRequest(null);
}

Add mix between adding and removing quotes from the value. I can’t run this locally so i don’t know for sure what you need here:

{
	"photo_id": "{{currentphotos}}"
}

The only thing that need to d=o before, is to delete previous ENV variables regarding this

1 Like

Thanks to @danny-dainton for this solution

1 Like