Repository navigation
Description
Robot Framework doesn't currently have native support for IF/ELSE syntax. Library keywords obviously can use whatever programming structures they need, but native IF/ELSE syntax would be useful also when implementing user keywords. When automating tasks (#2788), conditional logic is needed even more often and hiding it into libraries is not always desired.
There are various ways currently to use conditional logic in Robot's test data, but the most common one is Run Keyword If keyword. It works pretty well in simple cases, but it has some major limitations:
- It can only execute a single keyword. Higher level keywords or
Run Keywordsis needed if there's a need to run multiple keywords based on a condition. - It gets pretty complex if
ELSE/ELSE IFbranches are needed because also they need to passed to this keyword as arguments. - It's a bit awkward in general to use keywords to implement programming logic like this. We already have
FORloops.
To make Robot Framework more flexible and to avoid the problems with Run Keyword If, we have made a decision to add the native IF/ELSE support. The main design decisions are listed below:
- Syntax will use
IFcase-sensitively and without colons similarly asFORworks nowadays (Allow creating for loop usingFOR(case-sensitive) in addition to:FOR(case-insensitive) #2990). - The expression will be evaluated in Python similarly as with
Run Keyword If. - Explicit
ENDmarker is needed at the end.FORloops support this marker as well (End for loops withENDand avoid escaping keywords inside loops #3009).IFwon't support escaping included keywords with\(andFORloops will drop that support in the future too). - Indentation is optional but recommended.
ENDis needed also if there's only one keyword.ELSEbranches are supported. It is optional and there can be exactly one.ELSE IFbranches are supported and there can be any number of them.ELSE IFwording will be used because it is consistent withRun Keyword Ifand also reads better than variants likeELIForELSIF.
Examples with native IF syntax and Run Keyword If:
-
Simple usage:
IF ${x} > 0 Some positive keyword END Run Keyword If ${x} > 0 Some positive keyword -
Multiple keywords
IF ${x} > 0 Some positive keyword Another non-negative keyword argument END Run Keyword If ${x} > 0 Run Keywords ... Some positive keyword AND ... Another non-negative keyword argument -
Else
IF ${x} > 0 Some positive keyword ELSE Negative keyword argument END Run Keyword If ${x} > 0 ... Some positive keyword ... ELSE ... Negative keyword argument -
Complex usage
IF ${x} > 0 Some positive keyword Another non-negative keyword argument ELSE IF ${x} < 0 Negative keyword argument Another non-positive keyword argument ELSE Zero keyword END Run Keyword If ${x} > 0 ... Run Keywords ... Some positive keyword AND ... Another non-negative keyword argument ... ELSE IF ${x} < 0 ... Run Keywords ... Negative keyword argument AND ... Another non-positive keyword argument ... ELSE ... Zero keyword
The major problem with adding IF/ELSE support if that Robot's current test data parser is pretty complicated and adding new syntax to it isn't easy. We have thus made a decision to first re-implement the parser in RF 3.2 and this enhancement needs to wait for RF 3.3. The target is to get this feature implemented during 2019 but more concrete target date cannot be given at the moment.
Activity
Enhance error reporting when using e.g. 'for' instead of 'FOR'.
Document IF/ELSE (#3074)
pekkaklarck commentedon Nov 26, 2020
Several problems in the alpha 3 have been fixed and functionality ought to now be ready. Docs were also added in the above commit and enhanced in d48b234 and 9c4caca. This issue is done!
Enhance error reporting when using e.g. 'for' instead of 'FOR'.
Document IF/ELSE (robotframework#3074)
Document IF/ELSE (robotframework#3074)