How to override the index.html report in testNG

感情迁移 提交于 2019-12-20 03:14:46

问题


I have a scenario where I need to add some custom messages into index.html testNG report. Is there any way to do that?

I just created a custom annotation which I want to publish into index.html testNG report like DataProvider did. I have been tried the below code so far.

The below class will create annotation:

     @Retention(RetentionPolicy.RUNTIME)
     @Target({ ElementType.METHOD })
     public @interface Greet {
        /**
         * @return - The name of the person to greet.
         */
        String name() default "";
     }

I just googled but didn't get any idea about that how dataprovider publish the test data into default TestNG report. If anybody expert about the internal logic of dataprovider please let me know. It would be appreciate if there is any document to understand this better.

I just created a custom annotation which I want to publish into index.html testNG report like DataProvider did. I have been tried the below code so far.

The below class will create annotation:

 @Retention(RetentionPolicy.RUNTIME)
 @Target({ ElementType.METHOD })
 public @interface Greet {
    /**
     * @return - The name of the person to greet.
     */
    String name() default "";

}

The below class will get data from user:

  public class TestCase1 {
    @Test
    @DataPublish(name="First Test method_1")
    public static void test1() throws Exception {
       try {
            Assert.assertTrue(true);
           } 
       catch (Exception ex) {
            ex.printStackTrace();
        }
     }

I would like to print that annotation value in testNG index.html report.


回答1:


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.



来源:https://stackoverflow.com/questions/47546020/how-to-override-the-index-html-report-in-testng

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