Conditional execution in Robot Framework

In general, it is not recommended to have conditional logic in tasks, or even in user keywords, because it can make them hard to understand and maintain. Instead, this kind of logic should be in test libraries, where it can be implemented using natural programming language constructs. However, some conditional logic can be useful at times, and even though Robot Framework does not have an actual if/else construct, there are several ways to get the same effect. - Robot Framework User Guide

The following robot demonstrates how to use the Run Keyword Unless and Run Keyword If keywords 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, and the Run Keyword If keyword documentation for more information.

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

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

*** Keywords ***
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.

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