postmanlabs / newman

Newman is a command-line collection runner for Postman
https://www.postman.com
Apache License 2.0
6.88k stars 1.16k forks source link

Newman does not support Javascript functions like Math.floor / Math.round? #3037

Open mahmoudozil opened 1 year ago

mahmoudozil commented 1 year ago

Is there an existing issue for this?

Describe the Issue

we use this function in global variables :

function generatePhone () {
    let numTel = Math.round(Math.random() * 2);
    numTel = numTel.toString();
    return numTel;
}

then in the pre-script, we set a variable with the return value of the fucntion

eval(pm.globals.get("generatePhone"));
pm.collectionVariables.set("testPhoneLogin", generatePhone());

but the function does not return anything and i don't have any logs ! any solutions?

Steps To Reproduce

first we create a postman collection with global variables then we add a request with pre script to set the variable then we xecute the collection using newman run but the variable is not set (view description)

Screenshots or Videos

No response

Operating System

Linux

Postman Version

10.0.1

Postman Platform

Postman App

Additional Context?

NEWMAN_VERSION=5.3.1 (focker)

DannyDainton commented 1 year ago

Hey @mahmoudozil

What's the command you use to run the Collection with Newman? You've placed that function at the global level, are you exporting the global file and using that in the Newman run?

For additional information, Newman has its own repository for issues related to that project - https://github.com/postmanlabs/newman

mahmoudozil commented 1 year ago

newman run https://api.getpostman.com/collections/${collectionId}?apikey=${apiKey} -e env.json -g globals.json -r htmlextra --reporter-htmlextra-export result_${collectionName}.html

i'm using this command line to execute a collection with a given environment and globals. the weird thing that javascript functions returning a value are working fine but I couldn't use the Math library.

Should i import something to my docker image?

mahmoudozil commented 1 year ago

DOES NOT WORK

function generatePhone () {
    let numTel = Math.round(Math.random() * 2);
    numTel = numTel.toString();
    return numTel;
}

DOES WORK

function generatePhone () {
    return "static string";
}
DannyDainton commented 1 year ago

Rather than having the function at the Global level, you can add something like this to the Collection pre-request script:

utils = {
    generateNumber: function () {
        let numTel = Math.round(Math.random() * 200);
        numTel = numTel.toString();
        return numTel;
    }
};

You can call that function and assign it to a variable at the request level:

pm.variables.set('number', utils.generateNumber());

When run with Newman, adding the variable to a request body will work. Here's an example of that in the report:

image

mahmoudozil commented 1 year ago

@DannyDainton thank you for this well illustrated example. Unfortunely, i need to use this function (and others yet to come) across a whole workspace (which contains multiple enviornments, collections and requests,..) I don't want to duplicate my scripts everywhere .. I don't know if i should import something there

DannyDainton commented 1 year ago

Are you sure that the globals file contains the correct information? Was the function script in the initial and current values before you exported it?

I ran your code locally and the data was populated in the report. What is coming through in the generated HTML report for you?

More visual information will help debug this issue - As I can see that Newman is working as expected, I believe the issue is more than likely in the way you have it set up.

mahmoudozil commented 1 year ago

initial value same as current value

function generatePhone () {
    var numTel = Math.round(Math.random() * 2);
    numTel = numTel.toString();
    return numTel;
}

can you show me how you did it?

DannyDainton commented 1 year ago

You're going to need to show an image of what you have set up in the app, the code snippet doesn't show anything.

Without more information, there's not a lot we can do here.

mahmoudozil commented 1 year ago

you mean the postman setup? https://photos.app.goo.gl/j5DSVjc2dADwTqGf8