(My) Notes about Conditionals in Robot framework
Short notes for Conditionals in Robot Framework
❓The problem
To execute some test cases or some scenarios it is necessary to evaluate some conditions. The robot framework is capable of doing this by the IF
construct and, I need some notes to refresh my memory and to consult this topic at a later date.
💡The Solution
Creating an article that will contain compressed notes of how the IF
statements (call IF
constructs) and WHILE
loops are used in RobotFramwork.
📓The Implementation
Follow the pattern:
- Short explanatory notes.
- Code examples.
- (if needed it) Tips or remarks.
For context, this is Robot Framework
It is an automation framework used to automate software testing, this allows the creation of test cases that read in plain English.
Here are my notes about it.
Now the notes regarding how to use the conditionals in this framework.
Robot Framework Conditionals IF/ELSE, IF/ElSE IF/ElSE
Robot Framework in version 4 accepts the following conditional or constructs:
IF
IF/ELSE
IF/ELSE IF/ElSE
On a test case hide complexity so for most implementations is recommended to implement complex conditionals in a python library or use a
Keyword
as a way to abstract the conditional.
The IF
construct in the robot framework:
- It starts with the keyword
IF
follow by a python expression, if the python expression results inTrue
the lines in the body of theIF
will be executed. - The content after the python conditional is the body of the
IF
. - To close the conditional Block use the keyword
END
.
From the python operator Robot framework will use:
- Comparison Operators
- Logical Operators
IF
Construct
The IF
construct is the most basic construct, Its body can contain one or more lines.
The lines inside the
IF
construct will be executed if the python expression evaluatesTrue
otherwise, the holding block will be skipped.
IF / ELSE Construct
In this case, we have two blocks, one that is executed when the expression evaluates True
other will handle the cases where the python expression evaluates False
IF / ELSE IF / ELSE Construct
This construct allows the evaluation of several expressions in one single block.
Multiple ELSE IF
can be used, however, if the complexity increase it is better to abstract the complexity with a python library or by using Keywords.
(RobotFramework 5) Inline
IF
construct can be written in one line, in this case, the keyword END
is not needed it.
Use conditional to chain expressions
Python expression can chain multiple expressions using and
or
these can be used in the IF
construct as well.
Compare string conditions
Python uses “ to surround strings, the expression on the robot framework IF
construct must use the “ even if they are using variables.
⚠️ if they “ are not used, for example IF ${string_condition} == "cat"
will evaluate False
.
Final Thoughts
- It is important to have in mind conditionals in a test case will make them difficult to read and even confusing in most cases, therefore it is a good idea to use a keyword to refer to them.
- For complex conditionals, it is a better idea to implement them in a python function and then import them as an external library and use those that function as a keyword