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 @danny-dainton
Thank you guy for detailed answer, it helped me :slightly_smiling_face:

1 Like

Hello @praveendvd
Thank you very much.

Hi Team/ @harutharut ,

Kindly help me out in this one… similarly one issue my … else if part is NOT working… correctly

let jsonData  = pm.response.json();
let contextAction = ["New", "Confirm", "Different" ]
//console.log(jsonData.contextAction);

if(jsonData.contextAction === "New") {
   console.log(jsonData.contextAction);
   pm.test(`Verify the ContextAction Value is -> ${jsonData.contextAction}`,()=>{
      pm.expect(jsonData.contextAction).to.be.eqls("New");     
   })
} else if (jsonData.contextAction === "Confirm") {
   pm.test(`Verify the contextAction Value is -> ${jsonData.contextAction}`,()=> {
      pm.expect(jsonData.contextAction).to.be.eqls("Confirm");
      
      //pm.expect(jsonData.contextAction).to.be.oneOf(contextAction)
   })
} else { 
   pm.test(`Verify the contextAction Value is -> ${jsonData.contextAction}`,()=>{
      pm.expect(jsonData.contextAction).to.be.eqls("Different");
      
   })
}

Hi @ppoorna ,
first of all you can check and see that
console.log(jsonData.contextAction);
the result is undefined. It means you can not check in your if block undefined condition.
You should set an variable and then get it in condition.
Using variables
Please check it and look above on my issue and solution.

@harutharut well, i understand it, but the issue is not on the variables i did some research that postman is not supporting the ‘if else if else’ ladder

@harutharut In case if your free pls do connect i will explain it sir. thanks

@ppoorna

else if does work

Pretty sure its just because you have a few }) in the wrong places.

If you post code, can you please use the preformatted text option in the editor, so it aligns correctly and is easier to read. (Also easier to see mistakes in code).

let contextAction = "Confirm";


if (contextAction === "New") {
    console.log("New");
} else if (contextAction === "Confirm") {
    console.log("Confirm")
} else {
    console.log("Different")
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.