Access environment using newman terminal command

I have a tests script in Postman where I access the data using pm.environment.get(“lastSetDate”) How do I achieve the same using{
    collection: require('./Request.json'),
    reporters: 'cli'
}).on('request', (error,data) => {
    let rsp =
   // pm.environment.get("lastSetDate")

Hey @girishnair12345

Not really sure what you’re trying to do there, that Newman run doesn’t include an environment so it wouldn’t currently run with one.

You can’t use any of the same pm.* commands outside of the collection like that because that node script doesn’t know what they mean.

The Newman Summary object will contain information about the full run though and you can access that in a similar way as you have done in the ‘request’ event.

I need to just write to file, is that possible with test?
How can I write file in test section of request

You cannot directly write to an external file from within the Collection.

You can write multiple things to a file, using the node fs module in that script. The data from the run will be in the Newman Summary object.

The summary.environment object will contain the environment variables used for the run.

Posts like this have shown different ways that items can be written to a file.

That’s the same that I am using but cannot access the values the way we can access in postman Eg pm.environment.get() is not available

It will give you an object of all the values, that you can then use get anyone you like.

What have you tried in the node script and what is currently not working?

newman.summary.environment is Invalid and newman.summary is undefined

That’s not the why you would reference that.

Where did you get the original script, which is using the request on event? Is that something you have copied or wrote yourself?

Took from a youtube video, here is the code{
 collection: require('./Request.json'),
     reporters: 'cli'
 }).on('request', (error,data) => {
     // console.log(newman.summary)

There are several on actions (you can find these in the Newman docs) which give you different pieces of information about the Collection run.

That is using the request event which has information about the request, this is all stored in the data arg of that function.

Doing a console.log(JSON.stringify(data)) in that function will show you that request information.

You probably want the beforeDone or done event to get the environment variable.{
 collection: require('./collection.json'),
 reporters: 'cli'
}).on('beforeDone', (error, data) => {

I haven’t run any of this so it might be wrong, I’m not at my machine to try this out.

Unfortunately, the data is not in JSON but in HTML but that is not what I want, it’s the value of pm.environment.get() to be written on a file. Tried using beforeDone, done and JSON.stringify (data. environment) but all are giving undefined

The whole Newman Summary is in JSON, it’s a JSON file.

I can’t see what you have done or what the output of that is without you sharing images of what’s in front of you.

When something is undefined, try working backwards and log out the whole thing before trying the log something specific.

The Summary object of a Newman run will look something like this:

You would need this to log the environment variables:{
 collection: require('./collection.json'),
 reporters: 'cli'
}).on('done', (error, data) => {
1 Like

Yes, it was a typo on my side. It’s working. Just a quick question How do I access a variable from data.summary.environment let say the variable name is lastSetDate. This is the output