How to set and get dynamic environmental variable?

  1. Below is Pre-request:-

    pm.environment.set("employeeName", getRandomName());
    
    function getRandomName() {
        const employeeName = "{{$randomFirstName}}";
        console.log(pm.environment.get("employeeName"));
        return employeeName;
    }
    

2.Body:-

    {
        "name": "{{employeeName}}",
        "job": "leade"
    }

3.Environmental variable:-

employeeName {{$randomFirstName}}

I want to set the real value of $randomFirstName on the environmental level so I can access the test script. I am able to see the value generated by $randomFirstName in response body but the same value Iā€™m unable to set as an environmental variable. please guide me!

I managed to do it by saving the name to a different Environment Variable.

image

Shows in Environment Variables as:

image

Thereā€™s probably a better way of doing this, I think Iā€™d opt for it to be logged in the console if it were me, as I could have the logs documented in Jenkins.

Hope this is helpful!

1 Like

Hi @bikash019, welcome to the community! :wave:

As @LiamC has shared, itā€™s one way and itā€™s good!

Two ground concepts:

  1. What happens when you hit ā€˜Sendā€™?
Step 1. Pre-request Script gets Executed 
Step 2. Request is sent
Step 3. Response is received 
Step 4. Test script is executed
  1. Random variables are assigned during the phase when the request is being sent, now you can see that you wonā€™t be able to access a random variable even before it has been assigned to something in a request. (Since youā€™re trying to access it in the pre-request script)

  2. The curly braces syntax for variables (for eg: {{name}}) is only for the parts that belong to a request and not for pre-request or test scripts. To access variables in the scripts, you need to use the variable APIā€™s which are for eg: pm.environment.get('variableName');


Now, to get around your problem of getting what random name was generated during the phase when the request was sent, you need to make use of the Test Script, which is executed after the response has been received.
In this test script, you need to take out the name from the request that was sent / response that was received.
You can access the request using pm.request and the response using pm.response.

For eg:
You want to find out the randomFirstName that was sent as part of a query param of the request.
Then you can write the following:

let randomName = pm.request.url.query.get('name');
pm.environment.set('randomName', randomName);

As you can see in the image below, I was able to log the random name that was sent in the request in the console:

Similarly, you can access the body, headers etc. of the request and the write that to the environment variable.

2 Likes

In the version 7.6.0, which is currently on the Canary Channel but soon to be released, you can achieve this by using the new replaceIn() function.

I modified your code slightly to add this new feature in there:

pm.environment.set("employeeName", getRandomName());

function getRandomName() {
    const employeeName = pm.variables.replaceIn("{{$randomFirstName}}");
    return employeeName;
}

Or you could just get rid of that function and do it in one line :slight_smile:

pm.environment.set("employeeName", pm.variables.replaceIn("{{$randomFirstName}}"));

7 Likes

Hey Liam, thank you for your guidance!
you can see in environmental variable, employeeName value is showing as {{$randomFirstName}}. what I want is the actual value in env variable which is passed in body. so that I can assert test on name attribute. :slight_smile:

Hey, Sivcan, I got your point 100% and it helps me. Thank you. :grinning:

1 Like

Wow, thatā€™s what i was looking forā€¦ Amazing improvement in postman in 7.6 V. Thanks a lot dannydainton. :smiley:

1 Like

This is very helpful

If I use these code snippets, test results say: ā€œpm is not definedā€. What should I do???

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