How to set numeric value in environment variable

Viewed 28638

Problem: I want to set a counter variable (a numeric value) in environment variable, so in "Test" we can control the flow.

My experiment: I wrote a simple API call with following-

  1. Prescript sets the counter variable-

    postman.setEnvironmentVariable("mycounter", 1);
    
  2. Test verifies the counter variable, if its value equals to 1, increment it by one-

    if (postman.getEnvironmentVariable("mycounter") == 1 ) {
    
        postman.setEnvironmentVariable("result", "YES");       
        postman.setEnvironmentVariable("mycounter", 
            1+postman.getEnvironmentVariable("mycounter")); 
    } 
    else {
        postman.setEnvironmentVariable("result", "NO"); 
    }
    

But when i check the value for "mycounter"-

  • Actual value: 11
  • Expected value: 2

Can anybody point out how to set numeric value in environment variable?

3 Answers

With the new Postman API pm:

let myCounter = pm.environment.get("mycounter");
if (myCounter == 1 ) {
    pm.environment.set("result", "YES");
    pm.environment.set("mycounter", 1+Number(myCounter));  
} else {
    pm.environment.set("result", "NO"); 
}

the old postman. API is going to be deprecated and will be removed in future.

Related