[Issue #675] Bug report: if elseif else with run

I have test code that should result in only one true but results in two trues.

Steps:

  1. Execute code.

  2. Examine the values of these three variables:
    if_matched
    else_matched
    elseif_matched

On executing the test code, the erroneous result is:
if_matched = true
elseif_matched = false
else_matched = true

With an if-then-else structure, the correct result should be:
if val is 1 then
if_matched = true
elseif_matched = false
else_matched = false

I’m running UI.Vision in Chrome Browser.

Test Code Follows

Contents of “Try Out Commands”
Begin ===========================

{
  "Name": "Try out commands",
  "CreationDate": "2020-1-26",
  "Commands": [
    {
      "Command": "store",
      "Target": "false",
      "Value": "if_matched"
    },
    {
      "Command": "store",
      "Target": "false",
      "Value": "elseif_matched"
    },
    {
      "Command": "store",
      "Target": "false",
      "Value": "else_matched"
    },
    {
      "Command": "store",
      "Target": "1",
      "Value": "val"
    },
    {
      "Command": "if_v2",
      "Target": "${val} == \"1\"",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "true",
      "Value": "if_matched"
    },
    {
      "Command": "run",
      "Target": "Sub_R",
      "Value": ""
    },
    {
      "Command": "elseif",
      "Target": "${val} == \"2\"",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "true",
      "Value": "elseif_matched"
    },
    {
      "Command": "run",
      "Target": "Sub_R",
      "Value": ""
    },
    {
      "Command": "else",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "true",
      "Value": "else_matched"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    }
  ]
}

End ===========================

Contents of “Sub_R”
Begin ===========================

{
  "Name": "Sub_R",
  "CreationDate": "2020-1-26",
  "Commands": [
    {
      "Command": "pause",
      "Target": "200",
      "Value": ""
    }
  ]
}

End ===========================

Thanks for this interesting bug report. I confirmed it!

Once I comment out the call to Sub_R, then all works as expected:

I think the reason I discovered this is that UI Vision RPA has enough power that I can do somewhat sophisticated programming. It might be called a master-slave or maybe a client-server. I might be misusing the terms, but I will say client-server.

I have a “client” program running on the desktop written in MS Access VBA. The client program starts the server program which is written in UA Vision RPA. The server reads the last line of the csvFile for request from the client. Then writes to the same csvFile when it has fulfilled the request.

So, I have a bunch of if-ifelse statements to determine whether there is a request and which request. If the last line of the csvFile is not a server response and is not a client request, then the else statement throws an error. The server was executing the request correctly, then throwing an error. That lead me to isolate this bug.

I think the UI Vision team can be proud that they have produced a browser interface that can be used the way I am using it.

I find the indentation from using the while and if statements is helpful in following and debugging my code. Thank you very much for UI Vision RPA, and thank you for looking into this bug report.

1 Like

Here is a candidate for a workaround.

First the code example of the workaround, then the explanation.

Two macros’ contents follow:
========= Begin Macro: “if elseif else workaround” ================
{
“Name”: “if elseif else workaround”,
“CreationDate”: “2020-1-27”,
“Commands”: [
{
“Command”: “store”,
“Target”: “false”,
“Value”: “if_tripped”
},
{
“Command”: “store”,
“Target”: “false”,
“Value”: “elseif_1_tripped”
},
{
“Command”: “store”,
“Target”: “false”,
“Value”: “elseif_2_tripped”
},
{
“Command”: “store”,
“Target”: “false”,
“Value”: “else_tripped”
},
{
“Command”: “executeScript_Sandbox”,
“Target”: “return 0”,
“Value”: “num”
},
{
“Command”: “if_v2”,
“Target”: “${num} == 0”,
“Value”: “”
},

{
  "Command": "store",
  "Target": "true",
  "Value": "if_tripped"
},
{
  "Command": "run",
  "Target": "if elseif else sub-macro",
  "Value": ""
},
{
  "Command": "gotoLabel",
  "Target": "end_of_if",
  "Value": ""
},
{
  "Command": "elseif",
  "Target": "${num} == 1",
  "Value": ""
},
{
  "Command": "store",
  "Target": "true",
  "Value": "elseif_1_tripped"
},
{
  "Command": "run",
  "Target": "if elseif else sub-macro",
  "Value": ""
},
{
  "Command": "gotoLabel",
  "Target": "end_of_if",
  "Value": ""
},
{
  "Command": "elseif",
  "Target": "${num} == 2",
  "Value": ""
},
{
  "Command": "store",
  "Target": "true",
  "Value": "elseif_2_tripped"
},
{
  "Command": "run",
  "Target": "if elseif else sub-macro",
  "Value": ""
},
{
  "Command": "else",
  "Target": "",
  "Value": ""
},
{
  "Command": "store",
  "Target": "true",
  "Value": "else_tripped"
},
{
  "Command": "run",
  "Target": "if elseif else sub-macro",
  "Value": ""
},
{
  "Command": "label",
  "Target": "end_of_if",
  "Value": ""
},
{
  "Command": "end",
  "Target": "",
  "Value": ""
}

]
}
========= End Macro: “if elseif else workaround” ================

========= Begin Macro: “if elseif else sub-macro” ================
{
“Name”: “if elseif else sub-macro”,
“CreationDate”: “2020-1-27”,
“Commands”: [
{
“Command”: “pause”,
“Target”: “200”,
“Value”: “”
}
]
}
========= End Macro: “if elseif else sub-macro” ================

Explanation:
The workaround is to add gotolabel statements to force execution to go to the end of the if statement after executing the if-body and the else-body.

HOWEVER, NOTICE that the gotolabel statement is absent from the last elseif-body. The gotolabel statement is optional before the last elseif-body.

The run statement seems to be the cause of the if-elseif-else statements not branching properly. So, this workaround seems to be necessary only when there is a run statement. I don’t know whether there is a difference between the run statement being inside or outside the if-elseif-else statements.

I am feeling that using this workaround for the if-elseif-else statements is preferable to using gotoif statements because the if-elseif-else statements have indentation which greatly helps seeing the code structure.