Differences between "IF... ELSEIF..." and "Run Keyword If"

Hello,

I would like to know what if the difference between using “IF…ELSEIF… ELSEIF…” and “Run Keyword if” ?
Is it only a perfonmance issue or there is a fundamental difference ?

Exemple :

IF ${var1} == A
Execute keyword A
   ELSEIF ${var1} == B
   Execute Keyword B
   END .....
END

and :

Run Keyword if "${var1} == A" Keyword A
Run Keyword if "${var1} == B" Keyword B

1 Like

It is an historical thing. Run Keyword if was the only option until Robot Framework 5 (if I am not mistaken). IF, ELSE IF, ELSE, END is newer and is the recommended way for future test cases.

But note that the ELSE IF may imply that there is another ELSE after it. (I did not try if this is possible, but logically it seems)

Run Keyword if   "${var1}" == "A"   Keyword A
...    ELSE   Run Keyword if   "${var1}" == "B"     Keyword B
...    ELSE    Keyword C
1 Like

Hello,

Thanks for your answer.
So, if I have well undersantood, we can use both, there isn’t one more efficient than the other.

And for the report ? Is one more readable than the other ?
because if I use “IF … ELSE” on waterfall and the good condition is the latest “ELSE”, I must expand all the “IF” tree… Is the same with “Run keyword if” ?

John

It’s historical, but also I would say it’s fundamental. IF…ELSE is much more readable, flexible and allow to do more things.

Hi John,

Yes you can use both, however the documentation for Run Keyword If says:

NOTE: Robot Framework 4.0 introduced built-in IF/ELSE support and using that is generally recommended over using this keyword.

Run Keyword If, has not been deprecated yet but may be in the future, though as far as I know there are no plans to deprecate them any time soon, This thread from quite a while back might be interesting reading on the topic.

Basically while these are the old way of doing things, these are kept around so the many thousands of existing test cases don’t break, I guess there could be an argument that there are times when it’s necessary to use them, particularly when combining them with other keywords but in general as Aleh (@JaPyR) mentioned, using the new style is easier to read and makes tests easier to maintain.

Dave.

2 Likes

Thanks a lot at all !

1 Like