How to override the index.html report in testNG

被刻印的时光 ゝ 提交于 2019-12-02 02:34:55

I think you are trying to alter index.html report. You can have any data in the panel classes and print it in index.html.

To achieve this, you need to alter three files (classes). All the classes are here

Main.java

TimesPanel.java (this class will depend on the content (panel) you want to alter. For explaining purpose, we will add content to times panel under info section of report and hence TimesPanel.java )

and BaseMultiSuitePanel.java

create a file customBaseMultiSuitePanel.java in your project , copy the content of original file and make change to constructor accordingly.

create customTimesPanel.java and copy the content of TimesPanel.java and make changes to private String js(ISuite suite) method as we are going to add successPercentage and priority of tests to the table that comes up , when you click on times in report.

public class customTimesPanel extends customBaseMultiSuitePanel {

...
...

      private String js(ISuite suite) {
        String functionName = "tableData_" + suiteToTag(suite);
        StringBuilder result = new StringBuilder(
            "suiteTableInitFunctions.push('" + functionName + "');\n"
              + "function " + functionName + "() {\n"
              + "var data = new google.visualization.DataTable();\n"
              + "data.addColumn('number', 'Number');\n"
              + "data.addColumn('string', 'Method');\n"
              + "data.addColumn('string', 'Class');\n"
              + "data.addColumn('number', 'Time (ms)');\n"
              + "data.addColumn('string', 'SuccessPercentage');\n"
              + "data.addColumn('string', 'Priority');\n");

        List<ITestResult> allTestResults = getModel().getAllTestResults(suite);
        result.append(
          "data.addRows(" + allTestResults.size() + ");\n");

        Collections.sort(allTestResults, new Comparator<ITestResult>() {
          @Override
          public int compare(ITestResult o1, ITestResult o2) {
            long t1 = o1.getEndMillis() - o1.getStartMillis();
            long t2 = o2.getEndMillis() - o2.getStartMillis();
            return (int) (t2 - t1);
          }
        });

        int index = 0;
        for (ITestResult tr : allTestResults) {
          ITestNGMethod m = tr.getMethod();
          long time = tr.getEndMillis() - tr.getStartMillis();
          result
              .append("data.setCell(" + index + ", "
                  + "0, " + index + ")\n")
              .append("data.setCell(" + index + ", "
                  + "1, '" + m.getMethodName() + "')\n")
              .append("data.setCell(" + index + ", "
                  + "2, '" + m.getTestClass().getName() + "')\n")
              .append("data.setCell(" + index + ", "
                  + "3, " + time + ")\n")
              .append("data.setCell(" + index + ", "
                  + "4, '" + m.getSuccessPercentage() + "')\n")
              .append("data.setCell(" + index + ", "
                  + "5, '" + m.getPriority() + "');\n"); 
          Long total = m_totalTime.get(suite.getName());
          if (total == null) {
            total = 0L;
          }
          m_totalTime.put(suite.getName(), total + time);
          index++;
          ...
          ...
        }

Next, create customIndexHtmlReport.java and copy the contentof Main.java in this file. Remove the old TimesPanel object and the new like below in public void generateReport() method

 new customTimesPanel(m_model) 

Also change the report name in the same file like this

Utils.writeUtf8File(m_outputDirectory, "customReport-index.html", xsb, all); 

And finally, add listener to your testng.xml

<listener class-name = "firsttestngpackage.customIndexHtmlReport" />

then you will get your custom report like below with added successPercentage and priority for each test

Note:

make sure resources pertaining to getClass().getResourceAsStream method in customIndexHtmlReport.java is properly placed in your project. I had issues with it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!