【问题标题】:In CodedUI tests with multiple iterations, UITestActionLog.html file is generated only for the last iteration在具有多次迭代的 CodedUI 测试中,仅为最后一次迭代生成 UITestActionLog.html 文件
【发布时间】:2016-03-02 14:02:47
【问题描述】:

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

【问题讨论】:

    标签: c# visual-studio coded-ui-tests ui-testing


    【解决方案1】:

    我一直在努力解决这个问题。微软博客上有一些可悲的答案,比如你应该重新安装 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中声明字典如下

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

    【讨论】:

      【解决方案2】:

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

      此文件所在的路径 - 在您的项目中的文件夹 TestReuslts

      在我的情况下您的理解路径 - D:\Project**TestResults**\krakhil\In\0eee82c0-3cb9-42fd-96fe-7314ae4b5fd5\KRAKHIL-LT\UITestAction.html

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2023-02-07
        • 2021-12-21
        • 2011-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-13
        相关资源
        最近更新 更多