Hi guys. I tried different solutions but nothing seems to work. I’m a beginner in Postman/Newman collections. My end goal is to have a working solution with Newman being integrated with Jenkins.
So, I have the collection and a “Pre-request Script” where I basically handle random Strings/Numbers to be assigned to a Global Variable, something like this:
const dateNow = new Date();
pm.globals.set("currentDate", dateNow.toISOString());
pm.globals.set("randomNumber", _.random(1,9999));
function randomString(minValue, maxValue, dataSet = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') {
if (!minValue) {
minValue = 8;
maxValue = 8;
}
if (!maxValue) {
maxValue = minValue;
}
let length = _.random(minValue, maxValue),
randomString = "";
for (let i = 0; i < length; i++)
randomString += dataSet.charAt(Math.floor(Math.random() * dataSet.length));
return randomString;
}
pm.globals.set("randomPincode", randomString());
As you can see in the following screenshot the variables are correctly defined:
Then, in the request, I just call that variable and the value will be stored correctly in the Global Variable.
{
"momentOfOrigin": "{{currentDate}}",
"eventId": {{randomNumber}}
}
So, running directly from the Postman works fine, but the issue occurs when running it from Newman CLI.
I already exported those global variables as JSON and when running from the command line I’m using: newman run Collection.json -g global_variables.json
As a result, all the requests are having the status 400 Bad Request. I guess something goes wrong in defining the variables but I don’t know what.