TestNG自定义报告程序-向报告添加stacktrace

时间:2019-07-04 09:24:24

标签: java selenium testng reporting

我正在为TestNG套件使用自定义报告程序。我从在线教程中借用了基本代码,并进行了一些调整。但是,我想包括任何失败测试的堆栈跟踪,就像内置报告一样(emailable-report.html)。只需将本报告中看到的内容添加到我的底部即可。

任何人都可以给我有关如何实现此目标的任何指示吗?我不知道如何访问堆栈跟踪。

这是generateReport方法(我可以显示更多但可能不相关的东西):

@Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {

        try {
            // Get content data in TestNG report template file.
            String customReportTemplateStr = this.readEmailableReportTemplate();

            // Create custom report title.
            String customReportTitle = this.getCustomReportTitle(Base.Client + " Regression Suite Report");

            // Create test suite summary data.
            String customSuiteSummary = this.getTestSuiteSummary(suites);

            // Create test methods summary data.
            String customTestMethodSummary = this.getTestMethodSummary(suites);

            // Replace report title place holder with custom title.
            customReportTemplateStr = customReportTemplateStr.replaceAll("\\$TestNG_Custom_Report_Title\\$",
                    customReportTitle);

            // Replace test suite place holder with custom test suite summary.
            customReportTemplateStr = customReportTemplateStr.replaceAll("\\$Test_Case_Summary\\$", customSuiteSummary);

            // Replace test methods place holder with custom test method summary.
            customReportTemplateStr = customReportTemplateStr.replaceAll("\\$Test_Case_Detail\\$",
                    customTestMethodSummary);

            // Write replaced test report content to custom-emailable-report.html.
            File targetFile = new File(outputDirectory + "/custom-emailable-report.html");
            FileWriter fw = new FileWriter(targetFile);
            fw.write(customReportTemplateStr);
            fw.flush();
            fw.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

...这是html模板:

<body>
    <table>
      <tr><center><font size="5" face="verdana">
        <b>$TestNG_Custom_Report_Title$</b>
      </font></center></tr>
      <p></p>
      <thead>
        <tr>
          <th># Total Method</th>
          <th># Passed</th>
          <th># Skipped</th>
          <th># Failed</th>
          <th>Start Time</th>
          <th>End Time</th>
          <th>Execute Time (hh:mm:ss)</th>
        </tr>
       </thead> 
       $Test_Case_Summary$
    </table>
    <table id="summary">
      <thead>
        <tr>
          <th>Class</th>
          <th>Method</th>
          <th>Start Time</th>
          <th>Execution Time (hh:mm:ss)</th>
          <th>Browser</th>
          <th>Screenshot</th>
        </tr>
      </thead> 
      $Test_Case_Detail$
    </table>
  </body>

1 个答案:

答案 0 :(得分:0)

import org.testng.internal.Utils;

private void generateResult(ITestResult ans){
    ITestResult ans;
    List<String> msgs = Reporter.getOutput(ans);
    Throwable exception = ans.getThrowable();

    boolean hasThrowable = exception != null;
    if (hasThrowable) {
       String stackTraceMsg = Utils.stackTrace(exception, true)[0];
    }
}