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?
In the top right corner of Postman, click the environment selector and select Manage environments. Click Add to add a new environment where you'll define your OneLogin environment variables. Note: You'll need to use your API credentials to generate the access_token value.
You can access your environment variables from Postman and from your request elements, including the URL, parameters, body data, and test scripts. For the list of all your environments, select Environments in the sidebar.
You can access and manipulate variables at each scope in Postman using the pm API. You can use dynamic variables to generate values when your requests run. Postman supports a variety of variable scopes. The pm object provides methods for accessing global, collection, and environment variables specifically, and pm.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With