How to handle task failures in Robot Framework RPA robots

What should I do if my task fails occasionally?

You may run into situations where a task will occasionally complete in the expected amount of time, or certain parts of it take longer and could potentially cause the run to fail. For example, a web page may load up in less than half a second sometimes or up to three seconds other times. Luckily Robot Framework has options for creating retry logic to make task runs less brittle. These options are found in the BuiltIn library that is already installed with it.

Wait Until Keyword Succeeds

Wait Until Keyword Succeeds retries keywords multiple times:

# Retry three times at half-second intervals.
Wait Until Keyword Succeeds    3x    0.5 sec    Your keyword that you want to retry
# Retry with a one-minute timeout and at one second intervals.
Wait Until Keyword Succeeds    1 min    1 sec    Your keyword that you want to retry

TRY / EXCEPT / FINALLY

You can use TRY, EXCEPT, and FINALLY to catch and handle errors or exceptions. This is similar to handling exceptions in Python. See the TRY / EXCEPT / FINALLY exception catching and handling in Robot Framework article for more information.

Manage exception and failures with Work data management and Work items

Examples of deterministic retry logic for browser and HTTP tasks

The following example utilizes the Wait Until Keyword Succeeds keyword in browser and HTTP tasks. This will demonstrate deterministic retry logic that will move forward quickly with the rest of the task when an attempt is successful, or fail with a useful console message after the expected global amount of retries have been used up.

*** Settings ***
Documentation       Multiple tasks with retry keywords.

Library             RPA.Browser.Selenium
Library             RPA.HTTP

Suite Teardown      Close All Browsers


*** Variables ***
${URL}=                         https://httpstat.us
${GLOBAL_RETRY_AMOUNT}=         3x
${GLOBAL_RETRY_INTERVAL}=       0.5s


*** Tasks ***
Describe your HTTP task here.
    Send GET request and keep checking until success

Describe your browser task here.
    Open browser and keep checking until success


*** Keywords ***
Send GET request and keep checking until success
    Wait Until Keyword Succeeds
    ...    ${GLOBAL_RETRY_AMOUNT}
    ...    ${GLOBAL_RETRY_INTERVAL}
    ...    Send GET request

Send GET request
    ${headers}=    Create Dictionary    Content-Type    text/plain
    ${http_response}=    GET
    ...    url=${URL}/418
    ...    params=sleep=3000
    ...    expected_status=418
    ...    headers=${headers}
    Log To Console    ${http_response.text}
    Log    ${http_response.text}

Open browser and keep checking until success
    Open Available Browser    ${URL}/200?sleep=3000
    Wait Until Keyword Succeeds
    ...    ${GLOBAL_RETRY_AMOUNT}
    ...    ${GLOBAL_RETRY_INTERVAL}
    ...    Wait Until Page Contains
    ...    200 OK
    ...    0.5s
    Capture Page Screenshot

The ${GLOBAL_RETRY_AMOUNT} variable will control the amount of retries for each of the tasks, you can use amount of iterations (for example, 3x) or a timeout duration (for example, 3s). The ${GLOBAL_RETRY_INTERVAL} variable will control how quickly each retry is triggered.

Take note of how the Wait Until Keyword Succeeds is used in the HTTP and browser keywords. These are the retry mechanisms that will prevent both of these tasks from failing immediately. There are also Log To Console and Log examples that assist with capturing more details of the HTTP task.

May 5, 2022