How do I set a Collection variable?

#1

The documentation says I can have variable on several scopes:

Scopes

The following scopes are available for use:

But I can’t find instructions on how to add a Collection variable in the app UI. Is this something I can only do via script, such as a pre-request or test script?

#2

If you edit the collection ("…" > Edit), you can see a variables tab for it :slight_smile:

2 Likes
#3

Thank you very much!

#4

Just to add some additional information here:

Once those are set within that area on the UI, you’re able to use the values in the same way that you would use the other variables.

This doesn’t have a pm.collections.get() type syntax but you can use pm.variables.get("var_name") syntax to programatically access the collection level variable value.

#5

Thank you, @dannydainton.

How about setting a collection variable in a pre-request script? I’m reading the docs to try to figure out why I can’t seem to make pm.variables.set() work.

I mean, if I need to set a variable from within a pre-req script, must I use Globals?

#6

Currently, you can only use the .get() function to access the collection level values. You’re not able to use the .set() function within the prerequest/test scripts. Hence the reason why the manual setting of these variables, was offered as the solution. :grin:

There is an open issue for that enhancement, over on GitHub. :grin:

1 Like