I like to use newman with a images as POST data in the body for form-data. I can update upload an image but I like to run over multiple images as iterations.
How can I make the form-data variable like {{image}} as file place holder and then link it to a local folder when using it with Newman?
I am still digging up a way to do this.
But here is a good way to start:
Newman can be imported like any other NodeJS module.
Right now I’m investigating if we can add a variable like {{foo}}
into src and be able to iterate over each one using iterationData
However, I can’t get clear confirmation just yet, I’ll post a more detailed answer after I get some feedback.
You can also set src in the formdata of the collection as an array of data types, but I don’t believe that’ll set off multiple requests instead of one large request.
@marc.vantveer & @postman-kyle
This is actually possible. Just export your collection once and replace the src property with a Postman variable. Reimport the collection in Postman and you are ready to go.
Haha I sent a HUGE message to vivek about this so let me just copy again here:
Could you help me read this object? I am reading the summary.run.executions.request.formdata I want to be able to iterate over files in the local directory in newman and send requests sending each file. I currently don’t see if this can be done as the UI in the Postman App only allows me to upload a document and not add a variable.
My node script with newman as a library:
newman.run({
collection: c,
iterationData: [{ "variable": "./Chris.png"}, {"variable": "./abinav.png"}],
}).on('iteration', function (err, args) { // on start of run, log to console
console.log('starting an iteration...');
}).on('done', function (err, summary) {
if (err || summary.error) {
console.error('collection run encountered an error.');
}
else {
console.log('collection run completed.');
// console.log(summary.run.stats);
summary.run.executions.forEach( item => console.log(item.request.body.formdata) );
}
});
my collection with variable in the formdata:
"body": {
"mode": "formdata",
"formdata": [
{
"key": "img",
"type": "file",
"src": "{{variable}}",
"disabled": true
}
]
The response I get when I try to see the formdata and if {{variable}} actually populated.
> PostmanPropertyList { > members: > [ PostmanFormParam { > disabled: true, > key: 'img', > value: '', > type: 'file', > src: './Chris.png', > contentType: undefined } ], > reference: > { img: > PostmanFormParam { > disabled: true, > key: 'img', > value: '', > type: 'file', > src: './Chris.png', > contentType: undefined } }, > Type: > { ... }, > _postman_listIndexKey: 'key', > _postman_listAllowsMultipleValues: true } > PostmanPropertyList { > members: > [ PostmanFormParam { > disabled: true, > key: 'img', > value: '', > type: 'file', > src: './abinav.png', > contentType: undefined } ], > reference: > { img: > PostmanFormParam { > disabled: true, > key: 'img', > value: '', > type: 'file', > src: './abinav.png', > contentType: undefined } }, > Type: > ...
I can’t determine if the response I’m getting back is successfully parsing the {{variable}} with the iterationData, I don’t know the difference from members and reference in the output object
Hi Kyle,
This is the js script I’m now building:
const newman = require('newman'); // require newman in your project
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: "/Users/marc/Desktop/Image Recognition copy.postman_collection.json",
iterationData: [{ "variable": "/Users/marc/Desktop/bolt.jpg"}, {"variable": "/Users/marc/Desktop/hammer.png"}],
reporters: 'cli'
}, function (err) {
if (err) { throw err; }
console.log('collection run complete!');
});
And this is the collection:
And the error is:
┌
│ 'Form param image
, file load error: “{{image}}”, no
│ such file’
â””
Met vriendelijke groet,
Marc van ’t Veer
Polteq
Mobile test expert
+31 (0) 33 - 277 35 22
+31 (0) 6 - 18 35 41 34
Passion for testing** . **** Be
an example to others.**
Amersfoort Amsterdam Deventer Eindhoven Groningen Rotterdam Leuven (BE)
(Attachment Image Recognition copy.postman_collection.json is missing)
Hi Kyle,
This is the js script I’m now building:
const newman = require('newman'); // require newman in your project
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: "/Users/marc/Desktop/Image Recognition copy.postman_collection.json",
iterationData: [{ "variable": "/Users/marc/Desktop/bolt.jpg"}, {"variable": "/Users/marc/Desktop/hammer.png"}],
reporters: 'cli'
}, function (err) {
if (err) { throw err; }
console.log('collection run complete!');
});
And this is the collection:
And the error is:
┌
│ 'Form param image
, file load error: “{{image}}”, no
│ such file’
â””
Met vriendelijke groet,
Marc van ’t Veer
Polteq
Mobile test expert
+31 (0) 33 - 277 35 22
+31 (0) 6 - 18 35 41 34
Passion for testing** . **** Be
an example to others.**
Amersfoort Amsterdam Deventer Eindhoven Groningen Rotterdam Leuven (BE)
(Attachment Image Recognition copy.postman_collection.txt is missing)
Hi Marc,
In this code you have variable
in itertationData.
You need to change that to image
as that’s your template-variable name, {{image}}
const newman = require('newman'); // require newman in your project
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: "/Users/marc/Desktop/Image Recognition copy.postman_collection.json",
iterationData: [{ "variable": "/Users/marc/Desktop/bolt.jpg"}, {"variable": "/Users/marc/Desktop/hammer.png"}],
reporters: 'cli'
}, function (err) {
if (err) { throw err; }
console.log('collection run complete!');
});
I just confirmed this works.
Basically the collection.json
just needs to have a variable and the runtime should update the data.
Hi Kyle,
Thank you for your answer and it works. This was not an easy puzzle to grasp. The only thing that is still wondering is the following:
<img apple-inline="yes" id="AB7EFD0A-2661-4962-9CF8-D69766326CC5" src="cid:[email protected]" class="">
The input file into newman is an input file (json or cvs). Is it also possible to assign a folder within the folder image variables?
Met vriendelijke groet,
Marc van ’t Veer
Polteq
Mobile test expert
+31 (0) 33 - 277 35 22
+31 (0) 6 - 18 35 41 34
Passion for testing** . **** Be
an example to others.**
Amersfoort Amsterdam Deventer Eindhoven Groningen Rotterdam Leuven (BE)
(Attachment GeplakteAfbeelding-1.tiff is missing)
Hi Marc,
Do you mean you have files within directories?
You just need to add the full path to the image.
The iterationData
is a JSON file with each JSON object as a different iteration.
So if you have [ { "image: file1" }, { "image": "file2"} ],
it will run twice with image being the value at each index. To get files under subdirectories just list the path.
Was that your question?