Robot Framework for RPA basic syntax
Robot Framework code aims to be readable so that even an untrained eye can understand what the code does. Here is an example of a Robot Framework script:
Despite being close to natural language, many syntax rules need to be followed.
Sections
A typical Robot Framework script is divided into different sections:
Section | Used for |
---|---|
*** Settings *** | Importing test libraries, resource files, variable files. Adding metadata. |
*** Variables *** | Defining variables that can be used elsewhere in the script. |
*** Keywords *** | Definining user keywords that are specific to the script. |
*** Tasks *** | Creating tasks, calling one or more keywords. |
Different sections are identified by their header row. The recommended header format is three asterisks, a space, the name of the section, a space, three asterisks: *** Settings ***
. In addition to using the plural format, singular variants like Setting
and Task
are accepted.
Space-separated syntax
Between two "tokens", there need to be two or more spaces or one or more tab characters.
In this example:
Before the keyword Input Text
, and between it and each of its arguments (username
and maria
), you need to add more than one space for them to be recognized correctly by Robot Framework.
The number of spaces used as the separator can vary, as long as there are at least two; aligning the data nicely in *** Settings ***
, and elsewhere, makes the data easier to understand. In our courses and examples, we are using four spaces as a convention.
Note: In addition to the more widely used space-separated one, Robot Framework also supports two other syntax formats: pipe separated and reStructured. In all our examples and reference material, we will use the space-separated format.
Comments
Lines starting with the #
character are ignored by Robot Framework and are used for comments like in this example:
Escaping
In case your data includes characters used by Robot Framework's syntax, you will need to escape it.
Escaping special characters
The backslash character can be used to escape special characters so that their literal values are used.
Character | Needs to be escaped because | Examples |
---|---|---|
\$ | $ introduces a scalar variable | \${notavariable} |
\@ | @ introduces a list variable | \@{notavariable} |
\% | % introduces a dictionary variable | \%{notavariable} |
\= | = used as part of the named argument syntax | not\=namedargument |
\# | # is used to start a line comment | \# not comment |
For example, if you have a variable that contains the value ${my_variable}
, you will need to escape it with the backslash character: \${my_variable}
before using it as an argument for a keyword.
Escape sequences
The backslash character also allows creating special escape sequences that are recognized as characters that would otherwise be hard or impossible to create in Robot Framework scripts.
Sequence | Meaning | Example |
---|---|---|
\n | Newline character | first line\nsecond line |
\r | Carriage return | text\rmore text |
The Built-in variable
${\n}
can be used if operating system dependent line terminator is needed (\r\n
on Windows and\n
elsewhere).
Handling empty values
Because Robot Framework's space-separated format uses a variable number of spaces to separate elements, empty values need to be escaped, either with the backslash character or the built-in variable ${EMPTY}
. We recommend using the ${EMPTY}
variable because it is more legible.
Here's how you can pass empty arguments to a keyword called Do Something
that accepts two arguments:
Handling spaces
If you need to explicitly handle empty spaces as arguments of a keyword, you will need to escape them. Similarly, as when escaping empty values, it is possible to do that either by using the backslash character or by using the built-in variable ${SPACE}
.
| Escaping with backslash | Escaping with ${SPACE}
|
| ------------------------ | ------------------------------- | --- |
| \ leading space
| \${SPACE}leading space
|
| trailing space \
| trailing space${SPACE}
|
| \ \
| ${SPACE}
| |
| consecutive \ \ spaces
| consecutive${SPACE * 3}spaces
|
Splitting data into several rows
If your content does not fit in one line, you can split the line and create continuing rows with the ellipsis (...
) sign. Ellipses can be indented to match the starting row's indentation and must always be followed by one or more spaces.