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

newman.run({
    collection: require('./Request.json'),
    reporters: 'cli'
}).on('request', (error,data) => {
    let rsp = data.response.stream.toString()
   // 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
pm.environment.get("lastSetDate")

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

newman.run({
 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.

newman.run({
 collection: require('./collection.json'),
 reporters: 'cli'
}).on('beforeDone', (error, data) => {
   console.log(JSON.stringify(data.summary.environment))
});

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:

newman.run({
 collection: require('./collection.json'),
 reporters: 'cli'
}).on('done', (error, data) => {
   console.log(JSON.stringify(data.summary.environment))
});
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
{"type":"any","value":"2010-08-06T18:30:00.000Z","key":"lastSetDate"}

I just got this to work. You can tryā€¦

console.log(summary.environment.values.reference..value);

Next I will be dumping this to a file.

You can also play around with the summary.environment object and dump things likeā€¦

//this should list all variable valuesā€¦

    for (i=0; i<summary.environment.values.members.length; i++){
        console.log(summary.environment.values.members[i]);
    }

Hi @kappagaa ,
thanks, this is working. Am able to read environment variable value now.
Is there any way to read specific collection variable from summary.collection?
If yes, can you please share the syntax?

Thanks!