Skip to content

Native support for IF/ELSE syntax #3074

Closed
Closed

Description

@pekkaklarck

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 Keywords is needed if there's a need to run multiple keywords based on a condition.
  • It gets pretty complex if ELSE/ELSE IF branches 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 FOR loops.

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 IF case-sensitively and without colons similarly as FOR works nowadays (Allow creating for loop using FOR (case-sensitive) in addition to :FOR (case-insensitive) #2990).
  • The expression will be evaluated in Python similarly as with Run Keyword If.
  • Explicit END marker is needed at the end. FOR loops support this marker as well (End for loops with END and avoid escaping keywords inside loops #3009). IF won't support escaping included keywords with \ (and FOR loops will drop that support in the future too).
  • Indentation is optional but recommended.
  • END is needed also if there's only one keyword.
  • ELSE branches are supported. It is optional and there can be exactly one.
  • ELSE IF branches are supported and there can be any number of them. ELSE IF wording will be used because it is consistent with Run Keyword If and also reads better than variants like ELIF or ELSIF.

Examples with native IF syntax and Run Keyword If:

  1. Simple usage:

    IF    ${x} > 0
        Some positive keyword
    END
    
    Run Keyword If    ${x} > 0    Some positive keyword
    
  2. 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
    
  3. Else

    IF    ${x} > 0
        Some positive keyword
    ELSE
        Negative keyword    argument
    END
    
    Run Keyword If    ${x} > 0
    ...    Some positive keyword
    ...    ELSE
    ...    Negative keyword    argument
    
  4. 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

  1. added this to the v3.3 milestone on Feb 4, 2019
  2. modified the milestones: v3.3, v4.0 on Feb 7, 2020
  3. modified the milestones: v5.0, v4.0 on Oct 21, 2020
  4. added a commit that references this issue on Nov 25, 2020
  5. pekkaklarck commented on Nov 26, 2020

    @pekkaklarck
    MemberAuthor

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions