Tasks

In Robot Framework for RPA, Tasks are where the action happens. When run, the robot script will execute the keywords specified in its tasks in sequence.

Take this example .robot file:

*** Settings ***
Documentation       Example robot that downloads an Excel file and opens it.

Library             RPA.Excel.Files
Library             RPA.HTTP


*** Tasks ***
Download an Excel file, open it, and close it
    Download
    ...    https://robotsparebinindustries.com/SalesData.xlsx
    ...    overwrite=True
    Open Workbook    Data.xlsx
    Close Workbook

This script has one Task that we called Download an Excel file, open it, and close it.

To accomplish the task, we call three keywords: Download, Open Workbook, and Close Workbook.

Tasks basic syntax

Tasks must be contained in a *** Tasks *** section.

The first line contains the name of the task. All other lines need to be indented with spaces.

Tasks documentation

Using the [Documentation] setting, you can add a more detailed description of what the task does:

*** Settings ***
Documentation       Example robot that downloads an Excel file and opens it.

Library             RPA.Excel.Files
Library             RPA.HTTP


*** Tasks ***
Download an Excel file, open it, and close it
    [Documentation]    Download the sales data file, open it, and close it.
    Download
    ...    https://robotsparebinindustries.com/SalesData.xlsx
    ...    overwrite=True
    Open Workbook    Data.xlsx
    Close Workbook

Task setup and teardown

Using the [Setup] and [Teardown] settings, you can execute keywords before and after a task is run:

*** Settings ***
Documentation       Example robot that takes a screenshot of a webpage.

Library             RPA.Browser.Selenium


*** Tasks ***
Take a screenshot of the intranet homepage
    [Setup]    Open Available Browser    https://robotsparebinindustries.com/
    Screenshot
    [Teardown]    Close All Browsers

The [Setup] setting will always execute as the first action called by the Task section. Likewise the [Teardown] setting will always execute as the last keyword, even upon a failure. The [Teardown] setting is great to use to ensure that specific cleanup actions are taken by the bot in both successful as well as failed runs.

Additionally, you can also place both the [Setup] and [Teardown] settings in the settings section like the below example. The below example also demonstrates how the [Teardown] setting works in conjunction with a failure.

*** Settings ***
Library             OperatingSystem

Task Setup          Do setup steps for the task
Task Teardown       Do teardown steps for the task


*** Tasks ***
Minimal task
    Log    Done.
    # 'Fail' keyword is here for simulating task failure
    Fail


*** Keywords ***
Do setup steps for the task
    Log To Console    %{RC_PROCESS_ID=-1}

Do teardown steps for the task
    ${variables}=    Get Variables
    # Logging ${variables} to the console (WARN level log will do that)
    Log Variables    level=WARN
    # Logging ${env_variables} to the console (WARN level log will do that)
    ${env_variables}=    Log Environment Variables    level=WARN
    IF    "${TEST_STATUS}" != "PASS"
        TRY
            Log To Console    Task failed, and we need to do something extra
        EXCEPT    ${error_message}
            Log    FATAL! Our error handling steps failed also.    level=ERROR
        END
END

The [Setup] and [Teardown] setting also work in conjuction with Work Data Management / Work Items.

For information on handling Work Item failures refer to the work items page. The portal bot Producer-Consuemer template also demonstrates how to handle work item failures.

You can find information on handling task errors on our linked docs page.

TRY-EXCEPT blocks are also perfect for handling errors, in keywords, tasks, or for Work Items.

December 3, 2022