Getting the error [error] Cannot read property 'find' of undefined on using else if

I’m trying to use else if with a different value everytime of a variable but getting the error Cannot read property ‘find’ of undefined.

Could you please help on this.

“Command”: “verifyElementPresent”,
“Target”: “xpath=//a[contains(@title,‘abc’)]”,
“Value”: “”
},
{
“Command”: “while_v2”,
“Target”: “{!statusOK}", “Value”: “” }, { “Command”: “storeText”, “Target”: “xpath=//*[contains(@id,‘def’)]”, “Value”: “name” }, { “Command”: “echo”, “Target”: “{name}”,
“Value”: “”
},
{
“Command”: “if_v2”,
“Target”: “{name}.includes (“diksha”) == true”,
“Value”: “”
},
{
“Command”: “click”,
“Target”: “xpath=//a[contains(text(),‘Vehicle’)]”,
“Value”: “” },
{ “Command”: “elseif”,
“Target”: "{name}.includes (“baluja”) == true”,
“Value”: “”
},
{
“Command”: “click”,
“Target”: “xpath=//a[contains(@title,‘abc’)]”,
“Value”: “”
},
{
“Command”: “end”,
“Target”: “”,
“Value”: “”
},
{
“Command”: “pause”,
“Target”: “11000”,
“Value”: “”
},
{
“Command”: “endWhile”,
“Target”: “”,
“Value”: “”
},
{
“Command”: “”,
“Target”: “”,
“Value”: “”
}

Hi @admin could you please help on this ? Thank you so much

It seems the $ is missing in front of every variable?

Instead of

if | {name}.includes (“diksha”) == true

use

if | ${name}.includes (“diksha”) == true

This works:

{
  "Name": "1",
  "CreationDate": "2019-9-26",
  "Commands": [
    {
      "Command": "store",
      "Target": "abc",
      "Value": "name"
    },
    {
      "Command": "if_v2",
      "Target": "${name}.includes(\"abc\") == true",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "yes",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    }
  ]
}

Hi @ulrich I’m using “else if”
I re tired by using $ symbol and it still throws the same error

@admin I agree with @DikshaBaluja. After 1h of trying to use almost the same code like him I googled the error and I found this thread. I’ve also tried to use multiple elseif inside a while loop and I get that error.

Have you tried instead of putting an elseif statement in, just putting an if statement in their instead?

I got the same error in a script that used to work. Then I added a valid if_v2 early in the script and a later elseif throws the error.

@admin please comment on this error! Any insights?

I got the same error with you.
My solution is using another if…end instead of elseif.

I’m getting the same error when I have too many if/elseif/end conditions.

Scenario:

  1. Nested IF (IF inside and IF inside and IF) and the later ELSE IF fails
  2. I have an if/else if condition for each of the letters of the alphabets and it fails.

@ulrich

Anyone can help us on this? @ulrich @Plankton @newuserkantu @admin

We are still encountering the issue on nested IFs.

Hi, do you have a test macro for me? If yes, we can probably fix this quite quickly.

But in my test all works fine:

{
  "Name": "nested if",
  "CreationDate": "2020-10-14",
  "Commands": [
    {
      "Command": "store",
      "Target": "1",
      "Value": "a"
    },
    {
      "Command": "if_v2",
      "Target": "${a} == 1",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${a} == 1",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${a} == 3",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "not this one",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${a} == 1",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "This one",
      "Value": "green"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    }
  ]
}

@Plankton @ulrich

Below is a sample macro with multiple nested if/elseif/else conditions. Apologies if this is not “cleaned-up” because I had to remove all the variables or links I’ve used on the macro.

Basically we have a variable where we extract the first letter from and from there, I split the ALPHABETS and translated it into IF/ELSE conditions so that the macro will traverse faster.

In my test suite, there are other submacros ran before this one but with the pasted script below, I can recreate the issue. The workaround I implemented here is instead of using IF/ELSE, I used gotoif_v2 and labels but on some of my processes, this workaround cannot be applied as it will mix up a lot of things on our submacro so I hope there is another workaround for this or a fix if this is a valid issue.

I cannot upload the file as I am a new user here but here is the macro that fails on my end:

[Line 55]: Cannot read property 'find' of undefined
{
  "Name": "TestMacro-Failed",
  "CreationDate": "2020-11-20",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed"
    },
    {
      "Command": "store",
      "Target": "12345",
      "Value": "DATA"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${DATA}.toUpperCase()",
      "Value": "TEST_VAR_1"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${TEST_VAR_1} + \"XXX\"",
      "Value": "TEST_VAR_2"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var getFirstChar = ${TEST_VAR_1}.charAt(0); return getFirstChar.toUpperCase()",
      "Value": "TEST_VAR_1_FIRST_CHAR"
    },
    {
      "Command": "comment",
      "Target": "***A to F***",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${TEST_VAR_1_FIRST_CHAR} == 'A' || ${TEST_VAR_1_FIRST_CHAR} == 'B' || ${TEST_VAR_1_FIRST_CHAR} == 'C' || ${TEST_VAR_1_FIRST_CHAR} == 'D' || ${TEST_VAR_1_FIRST_CHAR} == 'E' || ${TEST_VAR_1_FIRST_CHAR} == 'F'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***A***",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${TEST_VAR_2} == 'A1PPP'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${TEST_VAR_2} == 'A1ZZZ'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_2} == 'A3XXX'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "else",
      "Target": "${TEST_VAR_2} == 'A3YYY'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'A2'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'A3'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${TEST_VAR_2} == 'A3PPP'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_2} == 'A3ZZZ'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_2} == 'A3XXX'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_2} == 'A3YYY'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_2} == 'A3ABC'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'B1'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'B2'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'B3'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***C***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'C'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***D***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'D'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***E***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'E'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***F***",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'F'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "else",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***G to L***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1_FIRST_CHAR} == 'G' || ${TEST_VAR_1_FIRST_CHAR} == 'H' || ${TEST_VAR_1_FIRST_CHAR} == 'I' || ${TEST_VAR_1_FIRST_CHAR} == 'J' || ${TEST_VAR_1_FIRST_CHAR} == 'K' || ${TEST_VAR_1_FIRST_CHAR} == 'L'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***G***",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${TEST_VAR_1} == 'G'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***H***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'H'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***I***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'I'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***J***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'J'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***K***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'K'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***L***",
      "Value": ""
    },
    {
      "Command": "else",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***M to R***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1_FIRST_CHAR} == 'M' || ${TEST_VAR_1_FIRST_CHAR} == 'N' || ${TEST_VAR_1_FIRST_CHAR} == 'O' || ${TEST_VAR_1_FIRST_CHAR} == 'P' || ${TEST_VAR_1_FIRST_CHAR} == 'Q' || ${TEST_VAR_1_FIRST_CHAR} == 'R'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***M***",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${TEST_VAR_1} == 'M'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***N***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'N'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***O***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'O'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***P***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'P'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***Q***",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***R***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'R'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'S'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***T***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'T'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***U***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'U'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***V***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'V'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***W***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'W'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'X'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***Y***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'Y'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***Z***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == 'Z'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "else",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "***0 to 9***",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1_FIRST_CHAR} == '0' || ${TEST_VAR_1_FIRST_CHAR} == '1' || ${TEST_VAR_1_FIRST_CHAR} == '2' || ${TEST_VAR_1_FIRST_CHAR} == '3' || ${TEST_VAR_1_FIRST_CHAR} == '4' || ${TEST_VAR_1_FIRST_CHAR} == '5' || ${TEST_VAR_1_FIRST_CHAR} == '6' || ${TEST_VAR_1_FIRST_CHAR} == '7' || ${TEST_VAR_1_FIRST_CHAR} == '8' || ${TEST_VAR_1_FIRST_CHAR} == '9'",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${TEST_VAR_1} == '123'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == '1234'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_1} == '12345'",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${TEST_VAR_2} == '12345PPP'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_2} == '12345ZZZ'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${TEST_VAR_2} == '12345XXX'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${!statusOK} == true",
      "Value": ""
    },
    {
      "Command": "pause",
      "Target": "0",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${!statusOK} == true",
      "Value": ""
    },
    {
      "Command": "pause",
      "Target": "0",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${!statusOK} == false",
      "Value": ""
    },
    {
      "Command": "pause",
      "Target": "0",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "pause",
      "Target": "0",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "else",
      "Target": "${TEST_VAR_2} == '12345YYY'",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "else",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "ERROR",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "Please check TEST_VAR_1 value.",
      "Value": "#shownotification"
    },
    {
      "Command": "comment",
      "Target": "insert sub processes here",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    }
  ]
}```

Encountering the same issue @admin @ulrich

Why you do not use one if with multiple condition ?

I never seen any utility to use multiple separated if uno inside another if

Just FYI: This ticket is about the same “Nested
IF” issue. It includes a small test macro, too:

Currently we already have a workaround in place but having a nested if condition will be the best for our case.

Please keep me posted if the fix is already in place for this.
Thank you!