范围报告版本 3.0.2 - AppendExisting

问题描述 投票:0回答:3

下面是我尝试用来将所有测试附加到单个报告的代码。然而,最新的测试正在取代所有旧的测试报告。因此,出于某种原因,它不会附加到单个报告中。你能帮我一下吗?

var htmlReporter = new ExtentHtmlReporter(ResourcesConfig.ReportPath);
            extent = new ExtentReports();
            extent.AttachReporter(htmlReporter);
            htmlReporter.LoadConfig(ResourcesConfig.ReportXMLPath);
            **htmlReporter.AppendExisting = true;**
c# .net append extentreports
3个回答
1
投票

我在这方面遇到了很多麻烦,而且文档没有解释太多。我有一种名为 ReportCreation 的方法,它针对每个测试用例运行,在该方法中我有以下内容:

public static ExtentReports ReportCreation(){
    System.out.println(extent);
    if (extent == null) {
        extent = new ExtentReports();
        htmlReports = new ExtentHtmlReporter(fileName+ n + "\\extentReportFile.html");
        htmlReports.config().setReportName("Pre release Smoke test");
        htmlReports.config().setTheme(Theme.STANDARD);
        htmlReports.config().setTestViewChartLocation(ChartLocation.BOTTOM);
        extent.attachReporter(htmlReports);
    }
    else {
        htmlReports = new ExtentHtmlReporter(fileName+ n+ "\\extentReportFile.html");
        htmlReports.setAppendExisting(true);
        extent.attachReporter(htmlReports);
    }
    return extent;
}

因此,当运行第一个单元测试时,它将创建 html 报告,但第二个单元测试将看到报告已经生成,因此使用现有的报告。

我创建了一个随机数生成器,以便每次运行都会生成不同的报告

public static Random rand = new Random();
    public static int n = rand.nextInt(10000)+1;

0
投票

我也面临着同样的问题。我的解决方案是使用 .NET Core,因此不支持 ExtentReports 3 和 4。

相反,我编写了代码将以前的 html 文件的结果合并到新的 html 文件中。

这是我使用的代码:

public static void GenerateReport()
        {
            // Publish test results to extentnew.html file
            extent.Flush();

            if (!File.Exists(extentConsolidated))
            {
                // Rename extentnew.html to extentconsolidated.html after execution of 1st batch
                File.Move(extentLatest, extentConsolidated);
            }
            else
            {
                // Append test results to extentconsolidated.html from 2nd batch onwards
                _ = AppendExtentHtml();
            }
        }

public static async Task AppendExtentHtml()
        {

            var htmlconsolidated = File.ReadAllText(extentConsolidated);
            var htmlnew = File.ReadAllText(extentLatest);

            var config = Configuration.Default;
            var context = BrowsingContext.New(config);

            var newdoc = await context.OpenAsync(req => req.Content(htmlnew));
            var newlis = newdoc.QuerySelector(@"ul.test-list-item");

            var consolidateddoc = await context.OpenAsync(req => req.Content(htmlconsolidated));
            var consolidatedlis = consolidateddoc.QuerySelector(@"ul.test-list-item");

            foreach (var li in newlis.Children)
            {
                li.RemoveFromParent();
                consolidatedlis.AppendElement(li);
            }

            File.WriteAllText(extentConsolidated, consolidateddoc.DocumentElement.OuterHtml);
        }

此逻辑绕过任何范围报告引用,并将结果文件视为任何其他 html。

希望这有帮助。


0
投票

Jaideep Dhumal 的答案根据 ExtentReport 4.1.0 更新:

/// <summary>Appends test results to consolidated report.</summary>
/// <param name="consolidatedReportPath">Full pathname of the consolidated report file.</param>
/// <param name="reportPath">The Path to the Report.</param>
/// <returns>A Task.</returns>
/// <example>.</example>
private static async Task AppendToConsolidatedReport(string consolidatedReportPath, string reportPath)
{
    var htmlConsolidated = File.ReadAllText(consolidatedReportPath);
    var htmlNew = File.ReadAllText(reportPath);
    var config = Configuration.Default;
    var context = BrowsingContext.New(config);
    var newDoc = await context.OpenAsync(req => req.Content(htmlNew));
    var newLis = newDoc.QuerySelector(@"ul.test-collection");
    var consolidatedDoc = await context.OpenAsync(req => req.Content(htmlConsolidated));
    var consolidatedLis = consolidatedDoc.QuerySelector(@"ul.test-collection");
    var consolidatedTests = new List<string>();
    if (consolidatedLis != null)
    {
        foreach (var li in consolidatedLis.Children)
        {
            // record what we already have in the consolidated list to avoid duplication
            var existingTestName = li.QuerySelector(@"span.test-name");
            var existingTestTime = li.QuerySelector(@"span.test-time");
            if (existingTestName != null && existingTestTime != null)
            {
                consolidatedTests.Add(existingTestName.InnerHtml + existingTestTime.InnerHtml);
            }
        }
    }
    if (newLis != null && consolidatedLis != null)
    {
        foreach (var li in newLis.Children)
        {
            // check we don't already have it in the consolidated list (this can happen if this method is called twice)
            var newTestName = li.QuerySelector(@"span.test-name");
            var newTestTime = li.QuerySelector(@"span.test-time");
            if (newTestName == null || newTestTime == null ||
                consolidatedTests.Contains(newTestName.InnerHtml + newTestTime.InnerHtml)) continue;
            li.RemoveFromParent();
            consolidatedLis.AppendElement(li);
        }
    }
    await File.WriteAllTextAsync(consolidatedReportPath, consolidatedDoc.DocumentElement.OuterHtml);
}
© www.soinside.com 2019 - 2024. All rights reserved.