Kishor Gaur
Kishor Gaur

Reputation: 43

In CodedUI tests with multiple iterations, UITestActionLog.html file is generated only for the last iteration

On executing tests for CodedUI in Visual Studio, why is the uiTestActionlog.html file generated only for the last iteration? Can this be due to the overriding of the previously generated reports?

Upvotes: 1

Views: 571

Answers (2)

KR Akhil
KR Akhil

Reputation: 1017

This is happening because every time a new iteration runs the previous UITestActionLog file gets overwritten. Resulting in preserving only the final copy.

The path where this file is - inside your project under folder TestReuslts

Path for your understanding in my case - D:\Project**TestResults**\krakhil\In\0eee82c0-3cb9-42fd-96fe-7314ae4b5fd5\KRAKHIL-LT\UITestAction.html

So, what i did is - I added a line of code in my CleanUp (as my every iteration goes through this method at the end. You can use according to your need)

string FileSourcePath = (TestContext.TestResultsDirectory + "\\UITestActionLog.html").Replace(@"\\", @"\");
            File.Copy(FileSourcePath, FileSourcePath.Replace("UITestActionLog.html", "UITestActionLog" + ReportFileNo++ + ".html"));

Here at the end i will get all the UITestActionLog*.html where * will be the iteration no - i used a static variable for this. If you want u can write a line to delete the UITestActionLog.html - as u will already have a copy of it with iteration no.

File.Delete(FileSourcePath); // use this at the end of all iterations only

Upvotes: 0

Kishor Gaur
Kishor Gaur

Reputation: 43

I have been struggling a lot with this issue. There were some pathetic answers on the microsoft blog like you should re-install visual studio to resolve this issue. Finally managed to resolve this issue with below solution. Hope it would be helpful for others. The below method not only generates reports for all the iterations, but also helps us in getting the logs in the folder as per our requirement for all the iterations on a particular day for a particular method at a particular time.

Step 1

Define the directory or the share location and few variables

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

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;
                }
               currentIterationValue = currentIteration + 1;
                File.Copy(finam, DestinationFilePath + '\\' + FileName + "_Iteration"+ currentIterationValue + ".html");
                File.Delete(finam);
            }

Upvotes: 1

Related Questions