Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read environment variables in Postman tests?

I'm using the packaged app version of Postman to write tests against my Rest API. I'm trying to manage state between consecutive tests. To faciliate this, the Postman object exposed to the Javascript test runtime has methods for setting variables, but none for reading.

postman.setEnvironmentVariable("key", value ); 

Now, I can read this value in the next call via the {{key}} structure that sucks values in from the current environment. BUT, this doesn't work in the tests; it only works in the request building stuff.

So, is there away to read this stuff from the tests?

like image 807
chad Avatar asked Jan 28 '14 22:01

chad


People also ask

How do you read an environment variable Postman?

Variables quick start Add a variable named my_variable and give it an initial value of Hello . Save, then close the environment tab. Open a new request tab and enter https://postman-echo.com/get?var={{my_variable}} as the URL. Hover over the variable name to inspect the variable's value and scope.

Where are environment variables Postman?

To set up Postman environment variables: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 print an environment variable in Postman console?

Step1 − Add the below script to get the value of the Environment variable tutorial and print it in console. Step2 − Click on Send to execute a request. Step3 − After the Response is received, open the Postman Console. It displays postman which is the value set for the Environment variable tutorial.

Can we import environment variables in Postman?

Postman can import and export Postman data, including collections, environments, data dumps, and globals. Postman can also import non-Postman data in the form of API schemas to help you consolidate your API development workflow.


1 Answers

According to the docs here you can use

environment["foo"] OR environment.foo globals["bar"] OR globals.bar 

to access them.

ie;

postman.setEnvironmentVariable("foo", "bar");  tests["environment var foo = bar"] = environment.foo === "bar";  postman.setGlobalVariable("foobar", "1");  tests["global var foobar = true"] = globals.foobar == true;  postman.setGlobalVariable("bar", "0");  tests["global var bar = false"] = globals.bar == false; 
like image 90
gooddadmike Avatar answered Oct 20 '22 14:10

gooddadmike