Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Postman environment in pre-request script?

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?

like image 751
Roey Avatar asked Nov 04 '19 09:11

Roey


People also ask

How do you switch between Postman environments?

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.

How do I see Postman environment variables?

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.

What is PM environment in Postman?

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.


1 Answers

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);
}
like image 165
Arlemi Avatar answered Oct 19 '22 22:10

Arlemi