(My) Notes about Conditionals in Robot framework

Short notes for Conditionals in Robot Framework

Victor Fernandez
4 min readAug 21, 2022
Photo by Priscilla Du Preez on Unsplash

❓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:

  1. Short explanatory notes.
  2. Code examples.
  3. (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

Photo by Nguyen Dang Hoang Nhu on Unsplash

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 IFconstruct in the robot framework:

Example IF Construct
  1. It starts with the keyword IF follow by a python expression, if the python expression results in True the lines in the body of the IF will be executed.
  2. The content after the python conditional is the body of the IF.
  3. To close the conditional Block use the keyword END.

From the python operator Robot framework will use:

  • Comparison Operators
  • Logical Operators
Components of an IF construct

IF Construct

The IF construct is the most basic construct, Its body can contain one or more lines.

IF Construct

The lines inside the IF construct will be executed if the python expression evaluates True 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 Construct

IF / ELSE IF / ELSE Construct

This construct allows the evaluation of several expressions in one single block.

IF/ELSE IF/ElSE Construct

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.

IF Construct inline

Use conditional to chain expressions

Python expression can chain multiple expressions using and or these can be used in the IF construct as well.

IF construct with logical conditionals

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.

Compare string Conditionals

⚠️ if they “ are not used, for example IF ${string_condition} == "cat" will evaluate False.

Final Thoughts

  1. 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.
  2. 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

Victor Fernandez

--

--

Victor Fernandez
Victor Fernandez

Written by Victor Fernandez

I’m Victor, I’m a Field Application Engineer for a CCTV manufacturer. I love Raspberry Pi, Python, and Microcontrollers and I write about my personal projects.

No responses yet