Every script and component script should have an On Failure section. This article talks about what the On Failure action is and reviews some scenarios for its use.
When a script is executed without an “On Failure” step, chances are pretty good when a failure occurs things will be left in a state that prevents the next script or iteration of a script from being successful. Therefore, it is always a good idea to add “On Failure” to every script and component script.
To add “On Failure” to your script, drag “On Failure” from the Control Action section to your script. It is placed at the end of the script and cannot be moved. If the script reaches the “On Failure” section you can set whether it records a failure for the script or not. To have it report the script as having failed, select the Raise Failure checkbox. The checkbox is selected by default. All actions to be taken on script failure need to be added as child steps of the “On Failure” step.
For “On Failure”, think about what you want to happen if the script fails. Can it recover and keep going? Should it clean up and get ready to move on to the next script?
Many steps can be added to the “On Failure” section allowing it to handle various failure scenarios.
Let’s look at some scenarios using the “On Failure” control action.
Scenario 1 – Closing the application
In my script, if it fails there is no good way to recover so I want to close the application and then run the initialization script to reset the application for additional script executions.
To do this I would drag the Close Application action on to the “On Failure” step.
Next, configure the Close Application action. Use the option that will work best to close your application.
- Application Name – enter the executable name (i.e. iexplore.exe for Internet Explorer).
- Applications Started by this Scenario – if the script opened any applications using the “Start Application” action it can close them when this checkbox is selected.
- Use task kill command line – if the best (or only) way to close the application is to execute a task kill command, select this checkbox.
- Close Active Application – Alt + F4 is a windows command that closes the currently active window. Selecting this checkbox will cause the Alt + F4 command to be executed. Note that if there is a value in the Application Name box, this checkbox is ignored.
After the “Close Application” step add a “Play Script” step and set it to execute the script used for initialization.
Scenario 2 – Rolling Back Failed Steps
When executing our script it is failing quite frequently at a specific section of the application due to bad data. The data being used comes from a database and the potential for bad data, while infrequent, does happen and must be handled. However, the bad data is used in a section that is not required for the script to be completed successfully. Rather than fail the entire script and close the application, we want to clear out the bad data, skip that section, and then move on to the next section in the hopes the script would then complete successfully.
The first thing we would do is figure out what error is given when the script attempts to use the bad data. In our case a warning appears on the page. To handle this, here are the steps to follow:
- Clear the Raise Failure property checkbox on the “On Failure” step. If we leave it, it will mark our script as having failed when it recovers from the error and that isn’t what we want to happen.
- Add the “Raise Failure” action to the end of the “On Failure” steps so if it runs through all other steps that attempt to recover and continue it will properly log the script as having failed.
- Add a Label before the step it should return to after the bad data failure.
- In the “On Failure” section, add a Find Image step and have it find the warning. Set it to continue on failure. Give a meaningful name to the Execution Status Variable.
- Add an “If” step that checks whether it found the image (%imagevariablename%==SUCCESS).
- Add steps to clear out the bad data as children steps of the “If” step.
- If needed, add a step to move to the correct location in preparation to resume the script in a new location.
- Add a GoTo step that moves to the label added in step 1 and continues script execution.
Scenario 3 – Handling Infrequent Pop-Ups
Adding in steps to handle infrequent pop-ups is another good use of the “On Failure” step. Let’s say there is a pop-up that appears, but only in a few situations. Rather than adding conditional logic into the script to handle it, it is better to add those steps to the “On Failure” section and then have the script resume execution. That way you don’t slow down the script with unnecessary logic checking when the pop-up doesn’t occur.
The steps to do this are the same as what we saw in Scenario 2. The only difference is instead of using the “GoTo” action to move to a specific location in the script, add a “Resume” action to go back to the step in the script that failed once the pop-up has been handled.
For more about this topic, check out this video on understanding the On Failure action.