在具有多次迭代的 CodedUI 测试中,UITestActionLog.html 文件仅针对上次迭代生成

本文关键字:迭代 文件 html UITestActionLog CodedUI 测试 | 更新日期: 2023-09-27 18:37:11

在Visual Studio中执行CodedUI测试时,为什么uiTestActionlog.html文件只为上次迭代生成? 这可能是由于覆盖了以前生成的报告吗?

在具有多次迭代的 CodedUI 测试中,UITestActionLog.html 文件仅针对上次迭代生成

我一直在

为这个问题而苦苦挣扎。微软博客上有一些可悲的答案,比如你应该重新安装Visual Studio来解决这个问题。终于设法使用以下解决方案解决了这个问题。希望对其他人有帮助。下面的方法不仅可以生成所有迭代的报告,还可以帮助我们根据特定日期特定方法在特定时间进行的所有迭代的要求,将日志获取文件夹中的日志。

步骤 1

定义目录或共享位置以及几个变量

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);
            }
        }
     }
}

步骤 2

在每个测试方法中声明字典,如下所示

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

步骤 3

在 TestCleanup() 中,添加以下代码

[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();
         }

步骤 4

定义在 TestCleanup() 中使用的 ResultsGen 函数

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);
            }

发生这种情况是因为每次运行新的迭代时,以前的 UITestActionLog 文件都会被覆盖。导致仅保留最终副本。

此文件所在的路径 - 在项目内的 TestReuslts 文件夹下

就我而言,您的理解路径 -D:''Project**TestResults**''krakhil''In''0eee82c0-3cb9-42fd-96fe-7314ae4b5fd5''KRAKHIL-LT''UITestAction.html

所以,我所做的是 - 我在清理中添加了一行代码(因为我的每次迭代最后都会通过此方法。您可以根据需要使用)

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

最后,我将获得所有 UITestActionLog*.html其中 * 将是迭代 no - 我为此使用了一个静态变量。如果你愿意,你可以写一行来删除UITestActionLog.html因为你已经有一个迭代号的副本。

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