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

The following robot demonstrates how to use the Run Keyword Unless and Run Keyword If keywords (when using Robot Framework 3), and the native IF, ELSE IF, ELSE constructs (when using Robot Framework 4) to achieve conditional execution in your robot. 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 logic, check it out!

See the Conditional execution chapter in the Robot Framework User Guide for more information.

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

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

*** Keywords ***
Robot Framework 3: Generate Numbers And Act On Result
    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

*** Keywords ***
Robot Framework 4: Generate Numbers And Act On Result
    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

*** Tasks ***
Do if-else logic
    Generate Numbers And Act On Result

Skip task execution on condition: Skip If, Skip

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

*** Settings ***
Documentation     How to skip task execution on 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 condition using the new skip functio...
==============================================================================
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 condition using the new skip functio...
==============================================================================
Conditional task: Skip if a specific file exists                      | SKIP |
True
------------------------------------------------------------------------------
Main task                                                             | PASS |
------------------------------------------------------------------------------
Tasks :: How to skip task execution on condition using the new ski... | 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.