Tuesday, 1 July 2014

Get the CodedUI test results into a specific folder or directory in organised way.


We can make the html logs getting displayed for checking or viewing the results of the Tests.
If we are using a TFS server, then its easy to view the results and logs. What if you do not have a TFS server and wanted to have your own custom application for viewing the results.

By considering this, I would like to share how to get all those results of a whole project into single folder with proper naming conventions.

Prior to it lets see how the existing html logs are stored
When each test is run, we see the file getting created on the following location

...\ Project Location \ TestResults \ Administrator \ In \ Some junk folder \ HYD-WIN7-XXX

For each test method in a codedUITest you see each new folder getting created and every html file getting saved with same name, which makes us hard to find which test method html file is it.

The method or the way i am making my results organised is
Creating a file or have a share location where the results are to be stored.
Create a folder with the name of your codedUI solution or even it can be done when u run ur tests i.e. its automated.
for each run of the Test method, i will be getting the name of the test and then pulling the file from the default location and placing it in the share location with the TestMethod name

Step 1

Define the directory or the share location and few variables

namespace CodedUITestProject1
{
    [CodedUITest]
    public class CodedUITest1
    {
        public static string TestResultDir;
        public static string MethodName;
        public static string ProjectName = Assembly.GetCallingAssembly().GetName().Name;
        public static string sFileName = string.Concat(@"C:\Results\", ProjectName + '\\' + DateTime.Today.ToString("MMddyyyy"), '\\', DateTime.Now.ToString("yyyyMMddHHmm"), "Result");
        public static Dictionary<string, string> ResultsKey = new Dictionary<string, string>();
        public CodedUITest1()
        {
            if (!Directory.Exists(sFileName))
            {
                Directory.CreateDirectory(sFileName);
            }
        }
     }
}

While defining the "sFileName" i am using timestamp so that i can keep the results organised in such  a way that i get all the results of test methods run at once in a single folder.

The dictionary item "ResultsKey" is used in storing the TestMethod name and also the directory in which the results will be created for that TestMethod 


Step 2

Declare the Dictionary in every TestMethod as follow

[TestMethod]

        public void CodedUITestMethod2()
        {
            ResultsKey.Add(MethodInfo.GetCurrentMethod().Name, Directory.GetCurrentDirectory());


Step 3

In the TestCleanup(), add the following code


[TestCleanup()]
        public void MyTestCleanup()
        {
            Dictionary<string, string>.KeyCollection keyColl = ResultsKey.Keys;
            foreach (KeyValuePair<string, string> kvp in ResultsKey)
            {
                UIMap.ResultsGen(kvp.Value, sFileName, kvp.Key);
            }
            ResultsKey.Clear();
         }


Step 4

Define the ResultsGen function which is used in the TestCleanup()

public static void ResultsGen(string SourceFilePath, string DestinationFilePath, string FileName)
        {
            if (FileName != null)
            {
                SourceFilePath = Regex.Split(SourceFilePath, "Out")[0] + "In";
                DirectoryInfo DirInfo = new DirectoryInfo(SourceFilePath);
                string finam = "";
                foreach (var fi in DirInfo.EnumerateFiles("*.html", SearchOption.AllDirectories))
                {
                    finam = finam + fi.Directory + '\\' + fi.Name;
                }
                File.Copy(finam, DestinationFilePath + '\\' + FileName + ".html");
                File.Delete(finam);
            }
        }


Step 5

This step has some additional information
To get the result of the Test method during runtime
use "TestContext.CurrentTestOutcome" in the TestCleanup()

To get the server name of the machine where the tests are rung
use "Dns.GetHostName()"



After following these steps, if the Tests are run, the results are seen as follow in a structured manner





Feel free to share if you got a better solution or you own way of storing results, which would help others