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.
Hey @bukhari.adac
This is a very common question and doing a quick search in the forum would have returned a few different ways of writing those to file.
Using Newman as a library and paring that with the Node fs module you can save the responses to a file like this:
const newman = require('newman'),
fs = require('fs');
newman.run({
collection: '<Postman Collection>'
}).on('request', (err, args) => {
fs.writeFile(`./results/${args.item.name}.json`, args.response.stream, (errā¦
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?
danny-dainton:
environment
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?
danny-dainton:
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:
Sample_Newman_Summary.json
{
"cursor": {
"position": 0,
"iteration": 0,
"length": 1,
"cycles": 1,
"empty": false,
"eof": false,
"bof": true,
"cr": false,
This file has been truncated. show original
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
danny-dainton:
console.log(JSON.stringify(data.summary.environment))
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"}
kappagaa
(Aditya Kappagantula)
February 28, 2022, 4:56am
15
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!