I am setting environment variable in one request and then using it in a subsequent request. I want to test if the environment variable is defined (it should fail if env variable not defined). However the test is passing when the variable is not defined.
My test code is:
var esi = postman.getEnvironmentVariable(“es_id”);
console.log(“search ES id: “+esi)
tests[“ES ID (”+esi+”) is defined”] = esi !== “(undefined)”;
The Test Results tab shows:
PASS ES ID (undefined) is defined
I found a usage for this where I am pulling elements out of an array stored in an environment variable, and I want to test that the array is not empty as a precaution.
// retrieve the list of tests from the environment variable
// and parse it into a typed object
if( pm.environment.has('actionList') ) {
var actionList = JSON.parse(environment.actionList);
// check if there are still test actions to run
if (actionList.length > 0) {
//remove first element and set it as the next request
var test_request = actionList.shift();
//convert the list to a string and store it back in the environment variable
//minus the test we just pulled out
postman.setEnvironmentVariable("actionList",JSON.stringify(actionList));
postman.setNextRequest(test_request);
console.log("next test action: "+test_request);
//** etc... **//