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

Postman contains a powerful runtime based on Node.js that allows you to add dynamic behavior to requests and collections by adding JavaScript to execute either before a request is sent as a pre-request script, or after a response is received as a test script.

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!

Read more about adding a pre-request script, adding a test script, and the execution order of scripts.

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.

 

What do you think about this topic? Tell us in a comment below.

Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

5 thoughts on “Keep it DRY with collection and folder elements

    I have a question about how best to chain requests to test multiple workflows in postman. I want to test the following two flows:

    1) Create Token > Activate Device > Register Device
    2) Create Token > Activate Device > Retrieve list of all devices > Deregister a device

    The create token and activate device requests are common to both workflows. How do I use the folder structure, collection runner and setNextRequest to keep it DRY?

    This is all fine and well, but what if your requests already have an Auth type set?
    Adding the type to the folder or collection doesn’t change the request settings – so you have to go through the ENTIRE COLLECTION and change everything!
    I’ve tried exporting the collection and editing out the AUTH settings and re-importing, but it keeps defaulting them to BASIC AUTH.

    Buenas… Me gustaría saber si actualmente se puede setear una variable de entorno desde la línea de comandos utilizando newman…

    Does having folders slow down running scripts?