If/else statement

Hi everyone,

I set enviroment variable and want to check SID data for local and develop machines. After sending request I get the same result - is checked only else statemant.
Please, help me understand why if statement is ignored in any case.
Thanks so much.

Change it to

     If(pm.environment.get("environment")==="local")
1 Like

Hey @harutharut

As well as needing to be:

if (pm.environment.get('environment') === 'local') {
    // your test code
}

The syntax of your if/else statement isn’t quite right.

https://www.w3schools.com/js/js_if_else.asp

if (condition) {
  //  block of code to be executed if the condition is true
} else {
  //  block of code to be executed if the condition is false
}

To add additional conditions you would need to use else if

if (condition1) {
  //  block of code to be executed if condition1 is true
} else if (condition2) {
  //  block of code to be executed if the condition1 is false and condition2 is true
} else {
  //  block of code to be executed if the condition1 is false and condition2 is false
}

You could make use of pm.environment.name here to check the active environment name, rather that a value within it:

if(pm.environment.name === 'local') {
  //  do something...
} else if (pm.environment.name === 'develop') {
  //  do something...
} else {
  //  do not do anything...
}
2 Likes

Hey @dannydainton
Thank you guy for detailed answer, it helped me :slightly_smiling_face:

1 Like

Hello @praveendvd
Thank you very much.