Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making an environment local for collection in Postman

Tags:

postman

I've seen the postman doc about environments:

https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables

where an image expresses that the enviornments are local to a collection, which I'm not seeing that happens in my Postman.

Postman image from doc

I have many projects, each one with its collecion, and I want to set the different environment urls for each project. However, I'm seeing that the environments are shared through all the collections. How can I make the environments local to a collection?

Thank you!

like image 766
Leticia Esperon Avatar asked Aug 02 '18 13:08

Leticia Esperon


2 Answers

Or maybe just abandon environment feature of postman, and simplify and use only Pre-req script and {{URL}} in methods, just repeat for every collection:

const ENVIRONMENTS = {
    DEV: {
        name: "development", 
        url: "http://localhost:3000"
    }, 
    STAGE : {
        name: "staging", 
        url: "..."
    }, 
    PROD : {
        name: "production", 
        url: "..."
    } 
};
pm.collectionVariables.set("URL", (ENVIRONMENTS.DEV).url);

Or maybe... use environment feature of Postman just for setting environment variable 💡🤣 . Then read that variable in every collection pre-request script just choose environment:

...

envName = pm.environment.get('Environment')
const selected = ENVIRONMENTS[envName]

pm.collectionVariables.set("URL", selected.url);
pm.collectionVariables.set("Token", selected.token);
like image 183
big-toni Avatar answered Oct 22 '22 11:10

big-toni


The picture that you posted actually defines as to how the variables are resolved. You can have different types of variables.

Which are:

  • Global Variables
  • Environment Variables
  • Collection Variables
  • Local Variables
  • Variables from data

Suppose you have a variable 'a' with a value 5 defined as a global variable. But you also have a collection variable 'a' with value '7' So, when you're sending the request, the order in which the variables will be resolved is what the picture tells you.

Globals > Collection > Environment > Local > Data - That's the order of resolution.

So, that'll be done like so:

Globals (a = 5) > Collection (a = 7) > Environment (a not defined) > Local (a not defined) > Data (a not defined)

So, the final value of 'a' after going through the resolution order would come out to be 7

Environment variables are available to all the collections in a Workspace Each environment is specific to the workspace. So are the globals.

If you want to use variables only specific to each collection then you need to use the Collection Variables

To add collection variables, just go to the sidebar and hover on the collection you want to add the variables for and click '●●●'

Then click 'Edit' > 'Variables' > Add the variables that you want only specific to this collection.

Postman Edit Collection

Now in the Request you can use these variables just in a similar fashion as you use the other variables. For eg.: https://{{url}}/get?foo=bar

or in a test script:

console.log(pm.variables.get('car'));  // 'astonMartin'

Postman will do the rest for you.

Environment variables come handy when you have different environments such as PROD, STAGING, BETA etc. Many people use them like so. Otherwise, they're just variables so use it as you like.

Alternatively, if you want to use an environment for a few collections and another environment for another one. Then create multiple Workspaces and add collections to it with the corresponding environment. Finally, switch between these workspaces as per the need.

like image 8
Sivcan Singh Avatar answered Oct 22 '22 11:10

Sivcan Singh