How to run postman requests using Tags

Get all requests in the collection:

you can also get information about all the requests in a collection by using :

https://api.getpostman.com/collections/{{collection_UUID}}

to get uuid and api key goto :

https://app.getpostman.com

Now for generating api key >

goto account settings > api key and generate api key.

to get collection uuid goto specific workspace and collection and copy the uuid part from url:

Note: you can also get UUID by clicking collection and clicking info panel on the right side

Image for post

Now in your collection

Rename all requests as:

get user details [Regression][Smoke][Somethingelse]
get account details [Regression]

Then Create a new request called initial request and keep it as the first request in your collection:

url: https://api.getpostman.com/collections/8xxxxtheuuidyoucopied

authorization: apikey-header : key: X-Api-Key and value: yourapikey

test-script :

pm.environment.unset("requestToRun")
reqeustlist = pm.response.json().collection.item.map((a) => a.name)
requestToRun = reqeustlist.filter((a) => a.includes(pm.environment.get("tag")))
let val = requestToRun.pop()
pm.environment.set("requestToRun", requestToRun)
val ? postman.setNextRequest(val) : postman.setNextRequest(null)

Now set the envirnoment variable as what you want to look for eg: run script that contains text “Regression” then set pm.environment.set("tag","Regression")

Now in your collection-pre-request add:

if (pm.info.requestName !== "initial request") {
let requestToRun = pm.environment.get("requestToRun")
let val = requestToRun.pop()
pm.environment.set("requestToRun", requestToRun)
val ? postman.setNextRequest(val) : postman.setNextRequest(null)
}

Output:

Ran only reqeusts that has “Copy” in its name

Example collection:

https://www.getpostman.com/collections/73e771fe61f7781f8598 , you can click ctrl+o and choose link to import this collection

Note: In newman you can pass the environment variable as “ — env-var tag=Regression”

6 Likes

Looks Interesting @praveendvd. Will try this out :blush:

Thanks for sharing!!

1 Like

Thank you @bpricilla let me know your thoughts :slight_smile:

1 Like

Is it possible to do the same but with Folders? i.e. only run certain folders in collection that contain the word ‘Copy’

You can use the same logic , and make some modification

1 Like

Hi Thanks for this information. @praveendvd
can you suggest a solution while trying to execute request with tags which is in a collection with different folder structure.
For eg

Collection name

Folder1
– req1[tag]
–req2
Folder2
–req1
–req2[tag].

Here i need to execute req1 from folder1 and req2 from folder2 inside a collection using tag

Could you check the collection example . It does the same thing i believe

Adding tag is IMP for my project, here is my requirement : Currently collection running in team city
I need to update the results in Jira for each execution

for ex : 1 test cycle : 3 test cases automatically update from Team city to jira,

any idea how we can achieve this ?

Could someone share the script for the same thing but with folders? The logic is the same, but with folders in folders in folders … it doesn’t work. Unfortunately I’m not an expert in JS, so I wasn’t able to write a function, that creates plain array of requests IDs from the tree that Postman API returns.

@praveendvd Thanks, this works great, but I cant understand how to apply same logic for request in folders. Can you make an examle?

I made ugly solution for folders:

let requests = [];
reqeustlist = _getRequests(pm.response.json().collection.item, requests)

function _getRequests(items, requests) {
    items.forEach(function(item) {
        if (item.hasOwnProperty("item")) {
            _getRequests(item.item, requests);
        } else {
            requests.push(item.name);
        }
    });
    return requests
}

@tyson1986 @julian-nadar FYI