3

Is there a way to unset environment variables dynamically? I would like to access the environment vars and do a find & replace/delete action so I can test more dynamically.

For instance, say I want to test the creation of users, I create vars like {{tmp-username}}, {{tmp-email}}, etc... replace them with other values for the next test and remove them when I'm done.

I would do a stringsearch on tmp- if I knew how to access these using code...

Thanks in advance for any reply

To clarify, this question is different: Postman: How to delete/clear postman environment variable at run-time This deals with knowing the exact name of the var you wish to unset. I want to search or iterate trough the vars to remove or edit them.

cutlass
  • 33
  • 4
  • Possible duplicate of [Postman: How to delete/clear postman environment variable at run-time](https://stackoverflow.com/questions/43246721/postman-how-to-delete-clear-postman-environment-variable-at-run-time) – Ori Marko Jun 25 '18 at 13:33
  • Could you not just use a data file and the collection runner to do this? – Danny Dainton Jun 26 '18 at 11:26
  • @Danny Dainton I'd like to avoid separate files as we can then share collections & environments without the need of sharing data files as well. There's also a security issue to share these files. – cutlass Jun 26 '18 at 12:45

1 Answers1

1

Could you use a function in the Tests tab to iterate through the variables and clear them out after the last test had run?

For example:

function cleanup() {
    const clean = _.keys(pm.environment.toObject())
    _.each(clean, (arrItem) => {
        pm.environment.unset(arrItem)
    })
}
cleanup()

This wouldn't 'replace' the values but this would give you confidence that the ones that are set during the run are not being used again.

EDIT

If you wanted to clear out a specific set of variables, ones that you have given a certain prefix, you could use this:

function cleanup() {
    const clean = _.keys(pm.environment.toObject())
    _.each(clean, (arrItem) => {
        if (arrItem.startsWith("tmp")) {
            pm.environment.unset(arrItem)
        }
    })
}
cleanup()

If you want to see all the keys and the values you could use this to log them to the console:

_.map(pm.environment.toObject(), (value, key) => console.log(`The key is '${key}' and the value is '${value}'`))
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • While this is definitely an interesting script, it uses the exact variable names. I was hoping to access the environment vars more dynamically. – cutlass Jun 27 '18 at 07:35
  • What have you tried so far? - It seems like you're just waiting for an answer to be presented. Maybe try and create the `clean` array values by using the `keys` from the `pm.environment.toObject()` function. – Danny Dainton Jun 27 '18 at 08:17
  • I've updated my answer, this should now be more dynamic and based on the variables that have been set. – Danny Dainton Jun 27 '18 at 08:26
  • Excellent! That is exactly what I was looking for. I didn't know they could be set to an object. Thanks @Danny Dainton! I've tested this by logging the key values like so: var env = pm.environment.toObject(); for (var k in env){ if (Object.prototype.hasOwnProperty.call(env,k)) { console.log("Key is " + k + ", value is" + env[k]); } } – cutlass Jun 27 '18 at 08:43
  • You could probably add an `if`statement to only clear the ones that start with `tmp` etc. Worth just messing about with it to find what works. All API ref info is here https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference – Danny Dainton Jun 27 '18 at 08:45
  • @cutlass I've also added an easier method of logging them to the console. – Danny Dainton Jun 27 '18 at 09:34