22
votes

Feature

Postman added support for variables, authorization, pre-request and test scripts to collections. (as of version 5.4.1 this exists at both the collection AND folder level)

Use case

Let's say I want to store a refresh token when the login endpoint is hit. My test script needs to create/update a COLLECTION variable, NOT a global or environment variable.

Once that refresh_token is available to the collection, other tests and pre-request scripts, I would think there is a way to access them through an API similar to pm.environment or pm.globals. (pm.collection, for instance)

Question

I cannot find any documentation on how to access or modify those via pre-request scripts, or tests... does anyone know how to do this? Maybe this hasn't been thought out completely, or not fully implemented, but I thought I would check with others for some help.

Temporary Solution

As a complete hack, I am storing the things I need as namespaced environment variables. It's not ideal (makes things kind of messy when working in other collections) but it works just fine.

3
Have you created an issue in postman github ? - verglor
I think issue#4449 requests this. - verglor

3 Answers

22
votes

Collection variables

You can access collection variables (and all variables) in the pre-request and test script sections using pm.variables.get("variableName").

However, you can only define and update collection variables by editing the collection details via modal.

Note: For your current solution using environment variables getting messy, remember you can always use pm.environment.set() to reset the value or pm.environment.unset() to clear it.

18
votes

Postman v7.9.0 added support for new pm.collectionVariables, so you can update them on test scripts:

pm.collectionVariables.set("collection_variable", newValue);

https://github.com/postmanlabs/postman-app-support/issues/5053#issuecomment-542555156

2
votes

Setting collection variables manually and then getting them was always possible.

Setting collection variables in scripts and not just manually became possible in version 7.9.0 which was released in October 2019. As of writing this, there is still a lot of obsolete misinformation about it out there - on the internet in general - but sadly also here at stackoverflow.

Although joseluislopez87 has already correctly answered the question, I am adding this answer in an attempt to help clear up any remaining confusion.


To find out who was right and who was wrong, I made a simple little experiment.
Below I describe what I did. I explain how you can replicate the exact same experiment yourself.

I have created a Postman collection by the name ManipCollVars
(ManipulateCollectionVariables seemed a little too long).
You can download and save it to your local drive from:
https://schulze.000webhostapp.com/postman/variables/ManipCollVars.pm_coll.json.

Then - from your Postman desktop app (not the chrome extension) - import ManipCollVars as shown in the figure below. (The GET request is https://postman-echo.com/get.)

Import the collection ManipCollVars.


To see the initial value of the collection variable CollectionVar, click on the three mini circles next to the collection name (tooltip: View more actions). Then click Edit. See the figure below.

Edit the collection properties of ManipCollVars.


As in the figure below, click on the Variables tab. Notice how the CURRENT VALUE of CollectionVar is equal to Initial Value. Close the EDIT COLLECTION window.

Notice: the initial value of CollectionVar equals Initial Value.


Click on the request ManipCollVars-Request, and then on its Tests tab as shown in the figure below.
Disregard the two tests and instead focus on lines 7-11:

// Will now try to change `CollectionVar` to some new value:
pm.collectionVariables.set('CollectionVar', 'Some New Value');
// Then print the new value of `CollectionVar` to the console:
console.log(pm.collectionVariables.get('CollectionVar'));
// ^^ Does `collVar` contain "Initial Value" or "Some New Value"?

The Tests contents.


Click on the blue Send button, and then open the Console in the bottom left corner. See the figure below. Notice how the value of the collection variable has changed from Initial Value to Some New Value. - Issue settled!

The value of CollectionVar has changed to Some New Value.


To double-check that the value has indeed changed, click once again on the ellipsis (? ? ?) next to the name of the collection, on Edit, and then on the Variables tab. See the figure below. Notice how the CURRENT VALUE of CollectionVar is now Some New Value. - Confirmed!

The value of CollectionVar has changed to Some New Value.


References: