Newman: HOw to update the environment variable or global variable with collection results when fired with newman command line

Created on 21 Dec 2016  路  15Comments  路  Source: postmanlabs/newman

  1. Newman Version (can be found via newman -v):
  2. OS details (type, version, and architecture):
  3. Are you using Newman as a library, or via the CLI?
  4. Did you encounter this recently, or has this bug always been there:
  5. Expected behaviour:
  6. Command / script used to run Newman:
  7. Sample collection, and auxilliary files (minus the sensitive details):
  8. Screenshots (if applicable):

documentation question

Most helpful comment

@Sripathi1983 There are two parts to handling this:

  1. Ensure that environment values are updated to their desired values using postman.setEnvironmentVariable("var_name", "var_value");, or postman.setGlobalVariable in your collection request test scripts, where applicable.

  2. To update the environment / globals file, you can use --export-environment / --export-globals, like so:

newman run coll.json -e env.json -g globals.json --export-environment env.json --export-globals globals.json

All 15 comments

@Sripathi1983 There are two parts to handling this:

  1. Ensure that environment values are updated to their desired values using postman.setEnvironmentVariable("var_name", "var_value");, or postman.setGlobalVariable in your collection request test scripts, where applicable.

  2. To update the environment / globals file, you can use --export-environment / --export-globals, like so:

newman run coll.json -e env.json -g globals.json --export-environment env.json --export-globals globals.json

Thanks it worked for me

@Sripathi1983 That's great! Happy developing :smile:

thank you it work for me too ! 馃憤

@kunagpal And what about that when using newman with node.js please ? I cannot find a solution....

@tegomass You'll have to use the export options, like so:

newman.run({
    collection: '/path/to/collection.json',
    exportEnvironment: '/path/to/new/file.json',
    exportGlobals: '/path/to/some/other/file.json'
}, function (err, summary) {
   // change the world here...
})

Tested and worked. Awesome, thank you !

PS: @kunagpal : Is the exportEnvironment done after the entire collection run ?

Example, I want to reuse the {{sessionId}} which is set in the request1 (in the "Tests" tab) through :
postman.setEnvironmentVariable("sessionId", '1234');

But when I get the value in the 'beforeRequest' event :

newman.run({
    collection: '/path/to/collection.json',
    exportEnvironment: '/path/to/new/file.json',
    exportGlobals: '/path/to/some/other/file.json'
}, function (err, summary) {
   // change the world here...
}).on('beforeRequest', function(err, args) {
  if (args.item.name === 'request2') {
    const sid = require('/path/to/new/file.json').values[0].value;
    console.log(sid);
  }
})

The console.log(sid) displays the old value and not the new one updated in the request1.

My question is, is it possible to do the exportEnvironment after each request ? If no, is there another solution for my needs please ?

Thanks again ! :)

For my application, what I do is that i parse the "args" and modify it as I want.

Tested and worked. Awesome, thank you !

PS: @kunagpal : Is the exportEnvironment done after the entire collection run ?

Example, I want to reuse the {{sessionId}} which is set in the request1 (in the "Tests" tab) through :
postman.setEnvironmentVariable("sessionId", '1234');

But when I get the value in the 'beforeRequest' event :

newman.run({
    collection: '/path/to/collection.json',
    exportEnvironment: '/path/to/new/file.json',
    exportGlobals: '/path/to/some/other/file.json'
}, function (err, summary) {
   // change the world here...
}).on('beforeRequest', function(err, args) {
  if (args.item.name === 'request2') {
    const sid = require('/path/to/new/file.json').values[0].value;
    console.log(sid);
  }
})

The console.log(sid) displays the old value and not the new one updated in the request1.

My question is, is it possible to do the exportEnvironment after each request ? If no, is there another solution for my needs please ?

Thanks again ! :)

Hey @tegomass ,

Did you end up solving this? I am stuck n the same problem.

Thanks

@Sripathi1983 There are two parts to handling this:

  1. Ensure that environment values are updated to their desired values using postman.setEnvironmentVariable("var_name", "var_value");, or postman.setGlobalVariable in your collection request test scripts, where applicable.
  2. To update the environment / globals file, you can use --export-environment / --export-globals, like so:
newman run coll.json -e env.json -g globals.json --export-environment env.json --export-globals globals.json

@kunagpal "Not All Heroes Wear Capes" Thanks It worked for me.

What about if you're using newman to point to an environment variable file hosted at https://api.getpostman.com?

Tested and worked. Awesome, thank you !

PS: @kunagpal : Is the exportEnvironment done after the entire collection run ?

Example, I want to reuse the {{sessionId}} which is set in the request1 (in the "Tests" tab) through :
postman.setEnvironmentVariable("sessionId", '1234');

But when I get the value in the 'beforeRequest' event :

newman.run({
    collection: '/path/to/collection.json',
    exportEnvironment: '/path/to/new/file.json',
    exportGlobals: '/path/to/some/other/file.json'
}, function (err, summary) {
   // change the world here...
}).on('beforeRequest', function(err, args) {
  if (args.item.name === 'request2') {
    const sid = require('/path/to/new/file.json').values[0].value;
    console.log(sid);
  }
})

The console.log(sid) displays the old value and not the new one updated in the request1.

My question is, is it possible to do the exportEnvironment after each request ? If no, is there another solution for my needs please ?

Thanks again ! :)

@kunagpal , can you please give the solution for this?

is, is it possible to do the export environment after each request? If no, is there another solution for my needs, please?
I want my env var file should get updated after each request and use the same in the upcoming request.

Kindly help me with this...

TIA.

i need solution for this too

is, is it possible to do the export environment after each request? If no, is there another solution for my needs, please?
I want my env var file should get updated after each request and use the same in the upcoming request.

Kindly help me with this...

TIA.

The only workaround I found was to unset the variable in the pre-request or test tab.

pm.variables.unset("variableName");
Was this page helpful?
0 / 5 - 0 ratings