Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set numeric value in environment variable

Tags:

postman

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?

like image 954
Sam Avatar asked Jun 02 '17 20:06

Sam


1 Answers

I got workaround. By using Number function convert string to integer.

So

if (postman.getEnvironmentVariable("mycounter") == 1 ) {
    postman.setEnvironmentVariable("result", "YES");
    postman.setEnvironmentVariable("mycounter", 1+Number(postman.getEnvironmentVariable("mycounter")));  
} else {
    postman.setEnvironmentVariable("result", "NO"); 
}
like image 140
Sam Avatar answered Oct 01 '22 15:10

Sam