In a single collection, I want to run certain requests OR skip requests based on the environment variable. Is there a way to set conditions inside the requests that they will only be executed when environment variable is met?
For example
Req1 for client1
Req2 for client1 and client2
Req3 for client2
Req4 for client1 and client3
Environment variable:
Client=client1
Expected: Only Req1, Req2, and Req4 should be executed.
I know that I can create different collections for different clients, but that would be a hassle. In our case, clients sometimes only have 1 or 2 requests that cannot be executed out of 50 requests. And when we do our changes, all clients will be affected, thus, maintaining only 1 collection would make sense for us.
I tried setNextRequest(), however, that already executes the request and just skips the checking. I need it to really not execute certain requests.
Thank you!
I’ve already tried: I tried setNextRequest(), however, that already executes the request and just skips the checking. I need it to really not execute certain requests.
You could put your setNextRequest() call into an IF statement.
for example:
if (pm.environment.get("client_id") === "your_client_id") {
// Your logic when client ID matches
postman.setNextRequest("Next Request Name");
} else {
// Your logic when client ID doesn't match
postman.setNextRequest("Another Request Name");
}
This is functionality that comes out of the box in most automation tools.
The ability to tag requests\tests and to then target the tests in the runners with or without those tags.
You may have a collection with a full set of tests, but only want the read only ones run against production but don’t want to have separate collections or folders in order to do this.
Not sure setNextRequest would work as the first request would always run and if that is one of the requests you don’t want to run, then you are out of luck. Also working out which request should run next would be fun.
I suspect this would be an enhancement feature for the runner to allow some conditional logic.
Outside of using the suggestion from @vg4hh6h5hq, which is a newer feature from when you asked the question, you could create the first request to go to the Postman Echo endpoint or a simple endpoint that always exists (google.com, postman.com, example.com, etc). The call just has to have a connection to move into the Post Script.
Within the Post Script using the “setNextRequest” would then become an option (if you make different requests for each client) allowing you to bounce around the calls.
Use Case (before pm.execution.skipRequest existed):
On a particular project we had to iterate over hundreds of tickets but didn’t want to run the scripts manually for each ticket. Like your scenario we needed to run different requests based on the ticket so we created a “do” request (which called example.com) that enabled us to set the next request based on the evaluation of the ticket. We would pop the tickets (remaining) into the environment variables so that when we got to the last request of the collection (named “while”) it simply set the next ticket to be processed and set the next request back to the “do” so it could be automated and left alone.