DEV Community

Alessio Michelini
Alessio Michelini

Posted on

How to use Postman tests to populate variables

I've been using Postman for years, and I do love that product, but I'm aware that sometimes I just use a fraction of the features available on it, and today I found almost by accident a super handy one.

The problem

I recently build some APIs, and I'm using Postman to test them, I've created a collection and created all the requests on it and grouped in folders.
I use Postman variables a lot to populate paths, data to send, etc...

Also my APIs use JWT token to authenticate my requests to it, and of course I used a variable on all the requests, as you can see here:

Screenshot 2021-09-15 at 15.41.58

And it works great. The only problem for me, is that the token expires after some time, I don't use JWT token with long life (and you shouldn't), so every time it expires I have to call the login endpoint, authenticate, get the token, copy it, and paste it in the variables.

And if there's something I hate, is repetitive work.

The solution

So I started to google to see if there were any way to automate this, and I found my answer on the "Scripting in Postman documentation".

What I found is that you can write JavaScript code to write tests against your endpoints, which is by itself extremely useful, and something I'll start to do very soon, but I also found out that you can use the pm object available in the tests editor, to also write variables programmatically!

So I edited my login endpoint, selected the "Tests" option (see image), and started to write my script.
Screenshot 2021-09-15 at 15.49.43

Here the script, which is just plain JavaScript:



const { token } = pm.response.json();
// Write to collection variables
pm.collectionVariables.set('jwt_token', token);

pm.test('It should contain the token', () => {
    pm.expect(token).to.be.a('string');
});


Enter fullscreen mode Exit fullscreen mode

So once you run the endpoint via postman, the tests are executed, then I read the response data, and get the token variable from it, then simply use that variable to populate my collection variables, and that's it!

Now once I run that endpoint, all other endpoints, as long I use that variable, they will work with the correct JWT token.

Just marvellous!

Also in my example I use the collection variables, but you can do the same for global, environment and local variables as well, here some examples:



// Global
pm.globals.set("variable_key", "variable_value");

// Enviroment
pm.environment.set("variable_key", "variable_value");

// Collection
pm.collectionVariables.set("variable_key", "variable_value");

// Local variables
pm.variables.set("variable_key", "variable_value");


Enter fullscreen mode Exit fullscreen mode

You can also get or unset those variables, for example:



// get a variable value
const myVar = pm.variables.get("variable_key");

// unset a variable
pm.variables.unset("variable_key");


Enter fullscreen mode Exit fullscreen mode

For more info you can check the Postman related docs

Top comments (1)

Collapse
 
otumianempire profile image
Michael Otu

haha!! I see the test always but never clicked it until now.. So far, I can relate to most of your articles.. It was helpful..