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
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}}))
});
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.
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);
});
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);
});
@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.