Retrying failed playback actions with Coded UI

Retrying failed playback actions with Coded UI

Rate This
  • Comments 4

This blog assumes that you have basic understanding of Coded UI and are familiar with the code generated from a recording on Winforms, WPF or Web applications.

A lot of times, while running a UI test, our test runs into an unexpected page\state, say an unexpected error dialog, dismissing which would make our test work, or maybe the server takes too long to respond on occasion and if that happens, rather than having the test rerun from the start, all we want is for it to resume when the page is loaded. As part of VS Update 1, we have added API support for Retrying or Skipping a failed UI action and continuing playback of the remaining steps in the test.

For the purpose of this blog I am taking the example of a winforms application with an alert window. The steps would be no different if you were trying the same on any other supported technology.

Here’s the scenario:- I have a simple Aut with a textbox where I can enter the age of a user, in case the value exceed “50” an alert prompt is thrown. I want my automation to enter some value and click on “Submit Age” button, followed by entering some value in the Name TextBox. Clearly, if the age entered exceeds 50, I won’t be able to proceed with entering my Name in the textbox. For such a case, I want the test to dismiss the alert and retry entering the Name in the textbox.

The Coded UI Test generated code would look something like this at present


Now I would expect step 3 i.e. entering some text into uINameEditEdit to fail. In order to dismiss the alert box and retry the action, I need to follow these steps:-


  1. Set a retry count- Playback.PlaybackSettings.MaximumRetryCount, i.e. you can specifiy the number of times you want a failed action to be retried. This is not a mandatory argument, if no value is set, the default value will be 1.
  2. Add a retry handler for the failed step. This is nothing but the handler function which will get called in case of a failure. You can add\remove this handler for any step in the coded UI test method, or even in test\class initialize as a generic handler for all failures.


      3.  The handler method signature is as shown below



Here PlaybackErrorOptions to Retry\Skip\Default- for further details, msdn documentation for these options is present here.


    4.  Now simply setting the PlaybackErrorOptions to Retry will retry your failed step over and over until either the step passes or the MaximumRetryCount is hit. If you want to do any special handling, like say dismissing an alert, you can add the code for the same within the handler.


The final code for the above defined scenario looks like this:-


And the Retry handler method code:-

Couple of points to keep in mind:-

  1. This is supported only for the following exceptions:-
    1. PlaybackFailureException
    2. UITestControlNotFoundException
    3. UITestControlNotAvailableException
    4. TechnologyNotSupportedException
    5. InvalidUITestExtensionPackageException
    6. DecodingFailedException
    7. FailedToPerformActionsOnBlockedControlException
    8. FailedToPerformActionsOnHiddenControlException
    9. UITestControlNotVisibleException
    10. ValidationFailedException
    11. FailedToLaunchApplicationException
    12. ActionNotSupportedOnDisabledControlExcept
  2. You can write multiple handlers for different actions within a method. Registering and deregistering handlers as applicable would work. User can at any given time deregister a handler and the retry code will not be hit.
  3. Asserts will still fail even if there is a handler attached.
  4. If ContinueOnError flag is set, 1st the retry logic is hit until max count after which we continue if there is still an error.
  5. Only the action that fails can be retried, there is no api support to navigate to a different step within the test method.


Attaching below a sample wpf project which has an unwanted popUp and the corresponding Coded UI Test implementing the retry method to dismiss the same.


Leave a Comment
  • Please add 3 and 8 and type the answer here:
  • Post
  • When I build this solution I get the following error Error 1 'Microsoft.VisualStudio.TestTools.UITesting.PlaybackSettings' does not contain a definition for 'MaximumRetryCount' and no extension method 'MaximumRetryCount' accepting a first argument of type 'Microsoft.VisualStudio.TestTools.UITesting.PlaybackSettings' could be found (are you missing a using directive or an assembly reference?)

    in Visual Studio 2012 - any idea?

  • Thanks for the guidance on errorhandling.  

    From what point does the retry start?  In the code type John step has to be repeated, so does that step get repeated? Kindly clarify.

  • I know this is a very simple example, but I would be encourage test engineers to be careful implementing this solution. This is because simply having the SendKeys("{Enter}") code in the handler would result in the dismissal of potentially valid error messages (which should result in a failed test).

    My point is; the comment above the SendKeys code is very important - I would encourage the test engineer to take special care when implementing this solution and ensure that all dismissals are handled with specific reference to the message being handled (i.e. this should not be used as a sure fire way of ensuring your test will always complete).

    Despite my warning, I have to say this is a very good blog post - thanks for the info!

  • Hi,

    I'm trying this in Visual Studio 2013 and it doesn't seem to work. My code looks like this:


           public void Test()


               Playback.PlaybackSettings.MaximumRetryCount = 10;

               Playback.PlaybackError += Playback_PlaybackError;

               var map = new SomeUIMap();



           private void Playback_PlaybackError(object sender, PlaybackErrorEventArgs e)


               e.Result = PlaybackErrorOptions.Retry;


    I run the test with the window (on which the button to be clicked is) closed. When the Playback_PlaybackError method is reached (I set a breakpoint inside it) I open the window and continue the test. Now, at this point I am expecting the test to retry the button click and pass, however it just throws an exception and fails. Am I doing something wrong here? Have I misunderstood the intended use of the PlaybackError event? Any help would be greatly appreciated.

Page 1 of 1 (4 items)