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 article 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.
See the Conditional execution chapter in the Robot Framework User Guide for more information on using if-else logic in Robot Framework.
Code examples
Firstly, here's a quick reference guide covering most basic constructions in one concise code example. For more information scroll further down and check out the detailed explanations!
*** Settings ***
Documentation IF / ELSE IF / ELSE example using Robot Framework.
... How to use conditional execution.
*** Tasks ***
Use IF construct in Robot Framework
IF ${True}
Log This line IS executed.
END
IF ${False}
Log This line is NOT executed.
END
IF "cat" == "cat"
Log This line IS executed.
END
IF "cat" != "dog"
Log This line IS executed.
END
IF "cat" == "dog"
Log This line is NOT executed.
END
IF "cat" == "cat" and "dog" == "dog"
Log This line IS executed.
END
IF "cat" == "cat" and "dog" == "cat"
Log This line is NOT executed.
END
IF 1 == 1
Log This line IS executed.
END
IF 2 < 1
Log This line is NOT executed.
END
IF 2 <= 2
Log This line IS executed.
END
IF len("cat") == 3
Log This line IS executed.
END
IF (1 == 1 and 2 == 2) and 3 == 3
Log This line IS executed since the expressions evaluate to True.
END
IF (1 == 2 or 3 == 4) or 3 == 3
Log This line IS executed since one of the expressions evaluates to True.
END
Use inline IF construct in Robot Framework
IF "cat" == "cat" Log This is logged. ELSE Log This is NOT logged.
Use IF / ELSE construct in Robot Framework
IF 1 == 1
Log This line IS executed.
ELSE
Log This line is NOT executed.
END
IF 1 == 2
Log This line is NOT executed.
ELSE
Log This line IS executed.
END
Use IF / ELSE IF construct in Robot Framework
IF 1 == 1
Log This line IS executed.
ELSE IF 2 == 2
Log This line is NOT executed.
END
IF 1 == 2
Log This line is NOT executed.
ELSE IF 2 == 2
Log This line IS executed.
END
IF 1 == 2
Log This line is NOT executed.
ELSE IF 2 == 3
Log This line is NOT executed.
END
Use IF / ELSE IF / ELSE construct in Robot Framework
IF 1 == 1
Log This line IS executed.
ELSE IF 2 == 2
Log This line is NOT executed.
ELSE
Log This line is NOT executed.
END
IF 1 == 2
Log This line is NOT executed.
ELSE IF 2 == 2
Log This line IS executed.
ELSE
Log This line is NOT executed.
END
IF 1 == 2
Log This line is NOT executed.
ELSE IF 2 == 3
Log This line is NOT executed.
ELSE
Log This line IS executed.
END
Use Run Keyword If in Robot Framework
Run Keyword If ${True} Log This line IS executed.
Run Keyword If ${False} Log This line is NOT executed.
Use Run Keyword Unless in Robot Framework
Run Keyword Unless ${True} Log This line is NOT executed.
Run Keyword Unless ${False} Log This line IS executed.
Writing IF expressions
The IF
construct starts with IF
followed by a Python expression:
IF 1 == 1
The Python expression evaluates to a boolean value - either True
or False
. When constructing a boolean expression in Python, you have the following operators available:
Python Comparison Operators
Comparison operators are used to compare two values:
Python Operator | Description | Example | Eample result |
---|---|---|---|
== | Equal | 1 == 1 | True |
!= | Not equal | 1 != 1 | False |
> | Greater than | 1 > 1 | False |
< | Less than | 1 < 2 | True |
>= | Greater than or equal to | 1 >= 1 | True |
<= | Less than or equal to | 1 <= 0 | False |
Python Logical Operators
Logical operators are used to combine conditional statements:
Python Operator | Description | Example | Example result |
---|---|---|---|
and | Return True if both statements are true | 1 == 1 and 2 == 2 | True |
or | Return True if on of the statements is true | 1 == 1 or 1 == 2 | True |
not | Reverse the result, returns False if the result is True | not (1 == 1 and 2 == 2) | False |
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Python Operator | Description | Example | Eample result |
---|---|---|---|
is | Returns True if both variables are the same object | x = os(); y = os(); x is y | True |
is not | Returns True if both variables are not the same object | x = os(); y = re(); x is not y | True |
Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Python Operator | Description | Example | Eample result |
---|---|---|---|
in | Returns True if a sequence with the specified value is present in the object | x = [1,2]; 1 in x | True |
not in | Returns True if a sequence with the specified value is not present in the object | x = [1,2]; 4 not in x | True |
The IF construct
The lines after the expression are the IF
construct body. There can be one or many lines:
IF 1 == 1
Log This is the IF construct body. It can span many lines!
Log Another line!
The lines in the IF
construct body are executed only if the Python expression evaluates to True
. If the Python expression evaluates to False
, the lines in the body are not executed:
IF 1 == 1
Log This line IS executed.
IF 1 == 2
Log This line is NOT executed.
The IF
construct ends with END
:
IF 1 == 1
Log This is the IF construct body. It can span many lines!
END
Any Python expression that evaluates to True
or False
is allowed. Here we check the length of a string using the len
Python function:
IF len("cat") == 3
Log This line IS executed.
END
The IF / ELSE construct
The IF
/ ELSE
construct is similar to the basic IF
construct. If the IF
expression evaluates to True
, the execution continues in the IF
construct body:
IF 1 == 1
Log This line is executed.
END
The ELSE
construct handles the scenario where the IF
expression evaluates to False
:
IF 1 == 2
Log This line is NOT executed.
ELSE
Log This line IS executed.
END
The IF / ELSE IF / ELSE construct
The IF
/ ELSE IF
/ ELSE
construct enables evaluating multiple separate expressions. If any of the expressions evaluates to True
, the lines in that body will get executed. The other lines are ignored:
IF 1 == 1
Log This line IS executed. ELSE IF and ELSE are ignored.
ELSE IF 2 == 2
Log This line is NOT executed since the IF expression evaluated to True.
ELSE
Log This line is NOT executed since the IF expression evaluated to True.
END
IF 1 == 2
Log This line is NOT executed since the expression evaluated to False.
ELSE IF 2 == 2
Log This line IS executed since the IF expression evaluated to False
Log and the ELSE IF expression evaluated to True.
ELSE
Log This line is NOT executed since the ELSE IF expression evaluated to True.
END
IF 1 == 2
Log This line is NOT executed.
ELSE IF 2 == 3
Log This line is NOT executed.
ELSE
Log This line IS executed since other expressions evaluated to False.
END
You can have multiple ELSE IF
statements:
IF 1 == 2
Log This line is NOT executed.
ELSE IF 2 == 3
Log This line is NOT executed.
ELSE IF 3 == 3
Log This line IS executed.
ELSE
Log This line is NOT executed.
END
Inline IF
Starting from Robot Framework 5, IF statements can be written on one line:
*** Tasks ***
Inline IF: No need for IF / END construct
IF True Log Inline IF is nice!
Inline IF / ELSE
IF False Log False ELSE Log True
Inline IF / ELSE IF / ELSE: Not pretty but works!
IF False Log False ELSE IF False Log False ELSE Log True
Inline IF: Conditional variable assignment
${value}= IF True Get Current Date ELSE Get Time
How to use multiple conditions: and
/ or
The IF
and ELSE IF
expressions can have multiple conditions. Since the expressions are Python, you can use and
and or
to add multiple conditions:
IF 1 == 1 and 2 == 2
Log This line IS executed since both expressions evaluate to True.
END
IF 1 == 2 or 2 == 2
Log This line IS executed since one of the expressions evaluates to True.
END
You can group conditions using parentheses (()
):
IF (1 == 1 and 2 == 2) and 3 == 3
Log This line IS executed since the expressions evaluate to True.
END
IF (1 == 2 or 3 == 4) or 3 == 3
Log This line IS executed since one of the expressions evaluates to True.
END
Comparing string conditions
The IF
condition expression is evaluated as Python. In Python, you need to quote ("
) strings. When using strings in a Python expression in Robot Framework, you need to quote ("
) the strings.
In the following example, the ${string_condition}
is a Robot Framework variable. Even though Robot Framework internally converts cat
to a string, you still need to quote the variable if using it as a string in the IF
condition:
${string_condition}= Set Variable cat
IF "${string_condition}" == "cat"
This example where the string variable is not quoted would NOT work and would cause an error:
${string_condition}= Set Variable cat
IF ${string_condition} == "cat"
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.