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:-
- 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.
- 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:-
- This is supported only for the following exceptions:-
- 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.
- Asserts will still fail even if there is a handler attached.
- If ContinueOnError flag is set, 1st the retry logic is hit until max count after which we continue if there is still an error.
- 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.