Webinar

November 3rd, 2021 12:00 PM EDT
Automation for Field Services & DistributionNovember 3rd, 2021 12:00 PM EDT
Learn how creating a digital workforce can improve your supply chain processes!

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 3 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 can be used to retry keywords multiple times, and can be used in the following ways.

# Use the Wait Until Keyword Succeeds 3 times and at half-second intervals
Wait Until Keyword Succeeds    3x    0.5 sec    Your Keyword That You Want To Retry
# Use the Wait Until Keyword Succeeds with a timeout and at 1 second intervals
Wait Until Keyword Succeeds    1 min    1 sec    Your Keyword That You Want To Retry

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.

rpaframework version 11.4.0 or newer is recommended.

*** 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

*** 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

*** 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

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.

Learn more about the libraries mentioned on this page:

October 21, 2021