How to validate an environment variable in a response of get request

I have set an environment variable from a response of Post request and variable has set well. now I want to verify whether that value is saved in database by pulling the data using Get request.
I used below code under tests of the Get request.
tests[“Body matches string”] = responseBody.has(promo);

but it is not able to get the promo and showing the below error message

“ReferenceError | promo is not defined”

Excuse me if I did not understand your question correctly, but you basically want to compare your variable from the POST against what is returned from the response? If that’s the case, assuming the response is json, you can do something like the following…

pm.test(“name of your test”, function () {
const jsonData = pm.response.json()
pm.expect(jsonData.{{property}}).to.eql(pm.environment.get({{variable}}))
});

1 Like

Hi, thank you for the reply.
But my problem is in the pm.environment.get({{variable}}). Environment variable “promo” is not getting and showing error in test result.

When calling pm.environment.get({promo}) try quotation marks instead of the curly braces.

pm.environment.get(“promo”)

4 Likes

It works. Thank you.

1 Like

This worked! Thank you. Have been searching for this for awhile now, really appreciate your help here.

What if the variable value type is Number if we run this test case it gives quotes to the value which doesn’t match the response

Hi @parasverma,

That’s because when you store a value in a variable, it automatically gets stored as a string, regardless of its original type. You’ll need to do a simple type conversion back, so that they have identical types again, like this:

pm.test("lat is matching", function () {
    var jsonData = pm.response.json();
    var expectedLat = parseFloat(pm.environment.get("lat"));
    pm.expect(jsonData.data.lat).to.eql(expectedLat);
});
1 Like

Thanks @neilstudd

This is better and meanwhile I used another way below but what you told is better coz as it is under the function.

var longitutde = pm.environment.get(“long”);
longitutde = longitutde.toNumber();
pm.test(“lng is matching”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.data.lastSuccessLocation.geoAddress.coordinates.lng).to.eql(longitutde);
});

Hi @neilstudd

Can you please also help me with how to validate Date and time, please correct me what I am missing

@parasverma In this case, the values actually are different! The dates are the same, but the time that you retrieved from the environment variable is 5½ hours earlier. It seems that the environment variable may be saved in UTC time.

Are you able to share the code which is setting the LocationUpdateTime environment variable? Assuming you want the environment variable to contain the same value that you are now retrieving, you’ll need to make some changes to that code.

2 Likes

Yes code was changing the time format actually thanks for your help @neilstudd
Thanks for your quick responses.

1 Like

Hello
I have created a video to explain how to assert response in postman Postman API Test Cases Script tutorial with examples and tips to solve common errors - YouTube
Hope it helps