Keep it DRY with collection and folder elements
Besides a handy way to organize, document, and share your requests, Postman Collections are actionable groups of requests that can be run in a sequence mirroring your workflow. For all of you smarties using Postman collections, you’ll be pleased to know that collection- and folder-level elements are now available for the Postman native apps!
- You can now add variables, authorization, pre-request and test scripts to a collection. This applies to all requests in the collection.
- You can now add authorization, pre-request and test scripts to a folder. This applies to all requests in the folder.
These elements let you reuse common elements of your code. Keeping it DRY (Don’t Repeat Yo’ self) means you don’t have to WET (Write Every Time).
Variables for Collections
Just like variables in programming, variables in Postman store different values to be used in multiple places. Variables are one of the most useful features in Postman. Now, with collection variables, you can declare a variable scoped to a single collection. This means the value of the collection variable is accessible and modifiable throughout the collection. Use collection variables as well as global, environment, and other variables. Read more about variable scopes and defining collection variables.
Scripts for Collections and Folders
You can now add these scripts to collections and folders. Scripts added to a collection will run either before or after every request in a collection, and scripts added to a folder will run either before or after every request in a folder.
Let’s say you want to add a test to every request to check if a successful response code is returned. Instead of adding the same test to every request individually, you can now add a single test script to the collection or folder. Adding a single collection-level test script will ensure the test runs after every request in the collection, or adding a single folder-level test script will ensure the test runs after every request in the folder. Super DRY!
Authorization for Collections and Folders
Postman provides authorization types that make it easy for you to handle authentication protocols in the Postman app, with Newman, or Postman monitors. You can use environmetnt, collection, or global variables with all authorization types.
Suppose you add a folder to a collection. The default authorization type is set to “Inherit auth from parent”, meaning that every request in this folder by default uses the authorization type used by the parent, in this case a parent collection or parent folder. In this example, the collection is using “No Auth”, so the folder uses “No Auth”, meaning all requests in that folder will not use any authorization.
What if you want to leave the parent collection authorization type as “No Auth”, but update this specific folder’s authorization helper? Edit the folder details and input your credentials for “Basic Auth”. Doing this makes every request in this folder use “Basic Auth” while the rest of the requests in the parent collection or parent folder still doesn’t use any authorization.
If you have a group of requests that all require the same authorization, you can define the authorization for all requests in a collection or folder, or simply for every request individually. If you create a new collection or folder, every subsequent request in the parent element inherits the authorization definition, unless the user explicitly selects another type.
Read more about adding authorization to a collection or folder.