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:

*** Settings ***
Documentation     Robot to enter weekly sales data into the RobotSpareBin Industries Intranet.
Library           RPA.Browser

*** Variables ***
${URL}=    https://robotsparebinindustries.com/

*** Keywords ***
Open The Intranet Website
    Open Available Browser    ${URL}

*** Keywords ***
Log In
    Input Text    username    maria
    Input Password    password    thoushallnotpass
    Submit Form

*** Tasks ***
Open the intranet site and log in
    Open The Intranet Website
    Log In

Despite being close to natural language, many syntax rules need to be followed.

Sections

A typical Robot Framework script is divided into different sections:

SectionUsed for
*** Settings ***Importing test libraries, resource files, variable files. Adding medatada.
*** Variables ***Defining variables that can be used elsewhere in the the script.
*** Keywords ***Definining user keywords that are specific to the script.
*** Tasks ***Creating tasks, calling one or more keywords.

Different sections are recognized 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.

When using Robocorp Lab, use the plural form of section headers. To take advantage of the cell by cell execution, add each keyword and task in its own *** Keywords *** or *** Tasks *** section.

Space-separated syntax

Between two "tokens", there need to be two or more spaces or one or more tab characters.

In this example:

*** Keywords ***
Log In
    Input Text    username    maria

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, making it possible to align the data nicely in settings and elsewhere when it 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:

# This is a code comment.
*** Settings ***
Documentation     Robot to enter weekly sales data into the RobotSpareBin Industries Intranet.
Library           RPA.Browser

*** Variables ***
${URL}=    https://robotsparebinindustries.com/

*** Keywords ***
Open The Intranet Website
    Open Available Browser    ${URL}

Markdown in comments

Using Robocorp Lab, you can apply additional formatting to your comments by adding Markdown Cells to your Notebook. More information on the official documentation of JupyterLab.

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.

CharacterNeeds to be escaped becauseExamples
\$$ introduces a scalar variable\${notavariable}
\@@ introduces a list variable\@{notavariable}
\%% introduces a dictionary variable\%{notavariable}
\=$ used as part of the named argument syntaxnot\=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.

SequenceMeaningExample
\nNewline characterfirst line\nsecond line
\rCarriage returntext\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:

*** Tasks ***
Escape empty argument using backslash
    Do Something    first arg    \
    Do Something    \            second arg

*** Tasks ***
Escape empty argument using ${EMPTY}
    Do Something    first arg    ${EMPTY}
    Do Something    ${EMPTY}     second arg

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 backslashEscaping with ${SPACE}
\ leading space \${SPACE}leading space
trailing space \trailing space${SPACE}
\ \${SPACE}
consecutive \ \ spacesconsecutive${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.

Example:

*** Settings ***
Documentation      Here we have documentation for this robot.
...                Documentation is often quite long, and
...
...                It can also contain multiple paragraphs.

*** Variables ***
@{LIST}            this     list     is      quite    long     and
...                items in it can also be long

*** Tasks ***
Example
    Do X    first argument    second argument    third argument
    ...    fourth argument    fifth argument    sixth argument
    ${var} =    Get X
    ...    first argument passed to this keyword is pretty long
    ...    second argument passed to this keyword is long too