简体   繁体   中英

Action after failed coded ui test

I am using multiple c# Ordered tests on daily runs. Each test requires a revert to a snapshot so all data and changes are lost (which is ok for me)

But... sometimes the test fails and all the info is lost. Visual studio provides a good explanation and a screenshot (very usefull)

My question : Is there a way or function to implement that if the test fails, some files(log / crash) are copied to a network location (hard coded function or link to a bat/script file or something in playback function) I have tried with try catch options in my tests but than you lose the good info you get from visual studio. If there is no way to achieve this I will go back to the try catch

Regards

as requested some code:

This is a TestMethod i use:

//playback options + delay
Logging.playback();
Try{
Mouse.Click(uITaskListItems);
foreach (WinControl TaskList in uITaskListItems.GetChildren())
{
 if (TaskList.Name.Contains(Variables.TaskItem1))
 {
   Mouse.Click(TaskList, MouseButtons.Right);
   Mouse.Click(uIListMonitor);
   break;
  }
 }
}catch(Exception ex){
Logging.Log(ex.Message);
Assert.Fail();

And playback options

private static void Playback_PlaybackError(object sender, PlaybackErrorEventArgs e)
{
   // Wait a second
   System.Threading.Thread.Sleep(1000);
   // Retry the failed test operation
   e.Result = PlaybackErrorOptions.Retry;
 }
 //playback options
 public static void playback()
 {
   Playback.PlaybackSettings.MatchExactHierarchy = true;
   Playback.PlaybackSettings.SmartMatchOptions = SmartMatchOptions.Control;
   Playback.PlaybackSettings.SmartMatchOptions = SmartMatchOptions.TopLevelWindow;
   Playback.PlaybackSettings.SmartMatchOptions = SmartMatchOptions.None;
   Playback.PlaybackSettings.SearchTimeout = 2000;
   Playback.PlaybackSettings.ShouldSearchFailFast = true;
   Playback.PlaybackSettings.ThinkTimeMultiplier = 2;
   Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
   Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.UIThreadOnly;
   Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.Disabled;
   Playback.PlaybackSettings.WaitForReadyTimeout = 2000;
   Playback.PlaybackError -= Playback_PlaybackError;
   Playback.PlaybackError += Playback_PlaybackError;
   Playback.PlaybackSettings.DelayBetweenActions = 300;
  }

You can try the Exception class.

Using the Exception class you can get all the information of the error. -"Just like what visual studio do"

try
{
  //make some noise...
}
catch(Exception ex)
{
     Console.WriteLine(ex.InnerException);
     //ex.Data, ex.HelpLink, ex.HResult, ex.Messages etc..
}

You might use a [TestCleanup] method that will be executed after every test. The code might be based on the following.

[TestCleanup()]
public void MyTestCleanup()
{
    switch (TestContext.CurrentTestOutcome)
    {
        case UnitTestOutcome.Passed:
            // Success.
            break;

        case UnitTestOutcome.Aborted:
        case UnitTestOutcome.Error:
        case UnitTestOutcome.Failed:
        case UnitTestOutcome.Inconclusive:
        case UnitTestOutcome.InProgress:
        case UnitTestOutcome.Timeout:
        case UnitTestOutcome.Unknown:
            // Oh dear.
            break;

        default:
            // Should never be called.
            break;
    }
}

A simpler routine might use just a single if statement:

    if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed )
    {
        // Oh dear.
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM