How to handle task failures

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.

From Robocorp Lab, add the following to the *** Settings *** section of an existing task .robot file in your robot or create a new one.

*** Settings ***
Documentation   Multiple tasks with retry keywords.
Library         RPA.HTTP
Library         RPA.Browser

Suite Teardown  Close All Browsers

Also add the following to the *** Variables *** section of your .robot file. 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.

*** Variables ***

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

In the *** Keywords *** section of your .robot file, add the following and 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.

*** 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
    Create Session    httpstat    ${URL}    headers=${HEADERS}    verify=true
    ${HTTP_RESPONSE}=    Get Request    httpstat    /418?sleep=3000    headers=${HEADERS}
    Should Be Equal As Strings    ${HTTP_RESPONSE.text}    418 I'm a teapot
    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

In the *** Tasks *** section of your .robot file, create a description for the HTTP and browser tasks. Set the Send Get Request And Keep Checking Until Success and Open Browser And Keep Checking Until Success to the appropriate task description.

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

Run these tasks by clicking the Run Robot in the top right corner of Robocorp Lab. After the tasks have finished running, a Terminal window should eventually display in Robocorp Lab with the following passing results in this screenshot.

Robocorp Lab retry passed

If all of the global retries are used up, then a deterministic failure is displayed in both the Robocorp Lab Terminal window and the automatically generated log.html file. The following two screenshots demonstrate what this looks like.

Robocorp Lab failed retries

Log failed retries