How to change Postman environment in pre-request script?
Asked Answered
S

2

8

I'm having several collections of REST queries and tests in postman, and for each collection I've created a set of environment variables. I wish to make sure the right environment is selected before running the tests. Is there a way to automatically switch between environments in pre-request script section?

Stans answered 4/11, 2019 at 9:42 Comment(1)
You set variables for Collections, so the best course of action, would be to have one collection with one set of variables for -dev, same for -stage and -production.Muco
A
9

You can't switch environments from the pre-request script section.

That being said you can still check the name of the current environment with pm.environment.name; and stop the execution if it's not the one you expect, for example:

if(pm.environment.name !== "dev") {
   postman.setNextRequest(null);
}
Archivolt answered 4/11, 2019 at 10:11 Comment(0)
W
0

You can set variables names by environment and specially the url of your REST API.

Walston answered 4/11, 2019 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.