Conditional IF / ELSE IF / ELSE execution in Robot Framework

Sometimes, there is a need to execute some keywords conditionally. Starting from Robot Framework 4.0 there is a separate if expression syntax, but there are also other ways to execute keywords conditionally. Notice that if the logic gets complicated, it is typically better to move it into a custom Python library. - Robot Framework User Guide

IF, ELSE IF, ELSE, Run Keyword Unless, Run Keyword If

How to use if-else conditions in Robot Framework? The following robot demonstrates how to do IF / ELSE IF / ELSE logic in Robot Framework using the IF, ELSE IF, and ELSE constructs (Robot Framework 4), and the Run Keyword Unless and Run Keyword If keywords (Robot Framework 3 and 4). Complicated if-else conditional logic might fit better in a Python library that exposes a keyword for the robot, to keep things simple robot-code-wise.

The RPA.Tasks library offers another way for building conditional if-else logic. Check it out!

See the Conditional execution chapter in the Robot Framework User Guide for more information on using if-else logic in Robot Framework.

*** Settings ***
Documentation     IF / ELSE IF / ELSE example using Robot Framework 3 and 4.
...               Generate a random number.
...               Do if-else logic based on the generated number.
...               Stop (pass) when the condition is met.

*** Variables ***
${MAX_TRIES}=     ${50}
${NUMBER_TO_PASS_ON}=    7

*** Keywords ***
Robot Framework 4: IF, ELSE IF, ELSE condition
    FOR    ${i}    IN RANGE    ${MAX_TRIES}
        ${random}=    Evaluate    random.randint(0, 10)
        IF    ${random} == ${NUMBER_TO_PASS_ON}
        Pass Execution    "${random} == ${NUMBER_TO_PASS_ON}"
        ELSE IF    ${random} > ${NUMBER_TO_PASS_ON}
        Log To Console    Too high.
        ELSE
        Log To Console    Too low.
        END
    END

*** Keywords ***
Robot Framework 3: Use IF, ELSE IF, ELSE condition
    FOR    ${i}    IN RANGE    ${MAX_TRIES}
        ${random}=    Evaluate    random.randint(0, 10)
        ${is_expected}=    Evaluate    ${random} == ${NUMBER_TO_PASS_ON}
        Run Keyword Unless
        ...    ${is_expected}
        ...    Log To Console    Condition not met.
        Run Keyword If
        ...    ${is_expected}
        ...    Pass Execution    "${random} == ${NUMBER_TO_PASS_ON}"
        ...    ELSE IF
        ...    ${random} > ${NUMBER_TO_PASS_ON}
        ...    Log To Console    Too high.
        ...    ELSE
        ...    Log To Console    Too low.
    END

*** Tasks ***
Use if - else if - else conditions in Robot Framework
    Robot Framework 4: Use IF, ELSE IF, ELSE condition
    Robot Framework 3: Use IF, ELSE IF, ELSE condition

Skip task execution on IF condition: Skip If, Skip

Robot Framework 4 added the support for skipping tasks on IF condition using the Skip If keyword, or the unconditional Skip keyword. The following robot demonstrates how to skip task execution on IF condition using the new skip functionality in Robot Framework 4:

*** Settings ***
Documentation     How to skip task execution on IF condition using the
...               new skip functionality in Robot Framework 4.
Library           RPA.FileSystem

*** Variables ***
${FILE_PATH}=     ${CURDIR}${/}file.txt

*** Tasks ***
Conditional task: Skip if a specific file exists
    ${file_exists}=    Does File Exist    ${FILE_PATH}
    Skip If    ${file_exists}
    Create File    ${FILE_PATH}    Content

*** Tasks ***
Main task
    ${content}=    Read File    ${FILE_PATH}

The first time the robot is run, it will run both tasks and mark their status as PASS:

==============================================================================
Tasks :: How to skip task execution on IF condition using the new skip func...
==============================================================================
Conditional task: Skip if a specific file exists                      | PASS |
------------------------------------------------------------------------------
Main task                                                             | PASS |
------------------------------------------------------------------------------
Tasks :: How to skip task execution on condition using the new ski... | PASS |
2 tasks, 2 passed, 0 failed
==============================================================================

The second time, the conditional task is skipped, and marked as SKIP:

==============================================================================
Tasks :: How to skip task execution on IF condition using the new skip func...
==============================================================================
Conditional task: Skip if a specific file exists                      | SKIP |
True
------------------------------------------------------------------------------
Main task                                                             | PASS |
------------------------------------------------------------------------------
Tasks :: How to skip task execution on IF condition using the new ... | PASS |
2 tasks, 1 passed, 0 failed, 1 skipped
==============================================================================

Python libraries can raise an exception that tells that the test should be skipped. The easiest way is using the new robot.api.SkipExecution exception.

Learn more about Robot Framework syntax

Use the Robot Framework cheat sheet and quick reference in your daily development to look up how to work with tasks, keywords, arguments, for loops, conditional if / else / else if execution, variables, lists, dictionaries, libraries, etc.

May 6, 2021