Wednesday, 6 May 2015

unexpected popups handling in CodedUI

A lot of times, while running a UI test, our test runs into an unexpected page\state, say an unexpected error dialog, or an pop-up window, which would make our test fail 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.
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.
Suppose let me say upon clicking a button there was an unexpected pop-up appearing. This pop-up blocks my TestMethod and makes it fail as it will not be able to perform any action as per the TestMethod. In this case these are the following steps which are to be followed
  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.                         Playback.PlaybackError += Playback_PlaybackError;
  4. The handler method signature is as shown below
      1. private void Playback_PlaybackError(object sender, PlaybackErrorEventArgs e)
                    Console.WriteLine("Retrying .... ");
                      // By default, the exception will be thrown. Over here we are switching to retry mode.
                      e.Result = PlaybackErrorOptions.Retry;
                        // Just a simple enter to dismiss the Alert. If you are sure of any specific properties for
                        // a popup dialog or any other specific action you need to do, this is the place to 
                        // implement them.
        1. 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.
        2. TestMethod code looks like below
                        using (ApplicationUnderTest aut = ApplicationUnderTest.Launch("UnexpectedPopupAut.exe"))
                            #region Variable Declarations
            // Important to set the MaximumRetryCount count for the retry handler to be invoked.
                            Playback.PlaybackSettings.MaximumRetryCount = 3;
                            // Add the error handler.
                            Playback.PlaybackError += Playback_PlaybackError;
            //Code of your TestMethod
                            Playback.PlaybackError -= Playback_PlaybackError;
          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

        No comments:

        Post a comment