Newman - Postman Collection runs but email output is empty

My question :I am running into a situation where my Postman collection runs successfully, but the email values in Newman are empty.

this is my code in collection pre script
reading external file for body payload

then in the request script i am using this variable to traverse and get my payload object

when i run getting # failure detail

Your code is pointing to an environment file, but you mention “reading external files for body payload”. Is the initial email address coming from the Environment file or a data file?

If it is the Environment file. Do you have an email set in the initial value in your exported environment.json file?

Current values are not included in the export, only initial values.

If there is no current value when you try and read an environment variable, it will copy what ever is in the initial value or it will return null.

This is my Environment file:

This My Email Token 2.0 Oauth setup

I thought the problem was with the email address in the body, not the OAuth setup!

You are getting 400 bad requests, which would seem to indicate an issue with the request, rather than the authentication. (Which would usually be a 401).

Your environment variable appears to be called “user_email_id” (or potentially randomEmail) but your body (and pre-request script) is showing {{email}}.

As mentioned, current values are not exported, so in your screenshot, randomEmail would be undefined as it does not have an initial value.

Not sure if the screenshot is showing all of the environment variables though.

I’m assuming that you do have an email variable somewhere, as it would seem the code in the IF block is being triggered.

Where is the “value of yourEnvironmentVariable” coming from? As I can’t see that in your IF block?

If you have a bunch of required variables, you could add a test for this in your pre-request script.

For example…

pm.test("Pre-request check for Environment Variables", function () {
    let vars = ['clientId', 'clientSecret', 'tenantId', 'username', 'password', 'scope'];
    vars.forEach(function (item) {
        // console.log(item);
        pm.expect(pm.environment.get(item), item + " variable not set").to.not.be.undefined;
        pm.expect(pm.environment.get(item), item + " variable not set").to.not.be.empty;
    });
})

You would add the code to increment the email address within that test block after the assertions.

Or you could just console log all environment variables. (Are any being returned, or is it just the email variable that is missing?)

pm.environment.toJSON().values.forEach((object) => {
    console.log(`${object.key} : ${object.value}`);
});

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.