In Newman used as package how to retrieve an environment variable during the iteration?

Disclaimer:
the approach I am following may not be the best one but this is the one with which I was thinking to reach my goal, if there’s a better solution feel free to suggest it please.

Problem:
In a custom script where I am using Newman as package I am running a Postman collection by using the iterationCount parameters (see this) so to loop the same collection in many iterations. In each iteration I am saving a dynamic data I am interested with as an environment variable:
var body = JSON.parse(responseBody); postman.setEnvironmentVariable("cart_uuid", body.uuid);

Obviously this means that the environment variable is overwritten at each iteration.
The problem is as far as I understood in Newman the environment variables are only accessible in the done callback see this that’s to say “when a collection run has completed, with or without errors”. In other words it means that I can not intercept it during the iterations in my script (while I wanted to do it on the iteration callback).

Could you tell me how can I resolve this problem of getting pragrammatically the environment variables in Newman used as package in an iteration?

Thanks in advance
Best

Hey @aurelien.lair

Welcome to the community :trophy:

Could you include the Newman run script that you have been using so far?

Here it is:

const newman = require('/usr/local/share/npm-global/lib/node_modules/newman');

const NUMBER_OF_CARTS = 2;

try {
  newman.run({
      collection: require('./config/collection.json'),
      environment: require('./config/dev.json'),
      folder: 'CART-CREATION',
      reporters: 'cli',
      bail: true,
      iterationCount: NUMBER_OF_CARTS
  }).on('iteration', function (err, args) {
    console.log(args);
  }).on('done', function (err, summary) {
    console.log('So far, so good!');
    console.log(summary.environment.values);
  });
} catch (e) {
  console.log('Postman test catch error: ', e);
}

basically the environment variables are only accessible on the done callback as far as I see.

Thanks for that @aurelien.lair,

Could you give us an idea about the requests inside that folder you’re running. If you were to run what you’re doing manually inside the app, what’s happening.

I can see that you’re setting a new variable somewhere but not really sure how that relates to the rest of the requests.

Is the request going to the same URL each time but the response is returning a different unique guid?

Hi Danny,

please check the collection you will see it directly in any case what you said is correct:
Is the request going to the same URL each time but the response is returning a different unique guid?
Yes only some parameters are changing in the request.
In any case here is what I am interesting to fetch in my script which uses Newman after each iteration

I mocked out a very simplified version of what you’re running (might not be 100% correct) but it seems to be creating a new guid and sending that in the second POST request each iteration.

What are the URLs that you’re seeing in the second POST request? Could you share an image of the output, please?

Hi Danny,

could you please refresh the collection I’ve shared? I have updated it with the base url as requested so you could reproduce the problem in Newman.

Please let me know
Aurélien

Did you find anything Danny?

What are the URLs that you’re seeing in the second POST request?

Could you share an image of the Newman CLI output, please?

Hi Danny,

please check the output on that link. I wish I could have past here but I get an error message when I try saying I can not put more than 5 links.

Please let me know
Thanks
A.

I’m not really sure what the issue is now, I can see that different guids are being using the requests.

The final environment output would be a single value, probably the last one as its overwriting the last one used.

What I needed to understand is how can I resolve this problem of getting programmatically the environment variables in Newman used as package in an iteration on a script which uses Newman run and the callbacks.

@aurelien.lair Variables are accessible in script, prerequest, and, test events as well.
Refer the below snippet:

newman.run({
    collection: {
        item: {
            request: 'http://postman-echo.com/get',
            event: [{
                listen: 'test',
                script: 'pm.environment.set("resp", pm.response.json());'
            }]
        }
    },
    reporters: ['cli']
}).on('script', function (err, args) {
    !err && console.info(args.execution.environment.values.toJSON());
}).on('test'/* or, prerequest */, function (err, args) {
    !err && console.info(args.executions[0].result.environment.values.toJSON());
});
2 Likes

Thank you for this Udit :wink: