我认为您正在尝试更改 index.html 报告。您可以在面板类中拥有任何数据并将其打印在 index.html 中。
为此,您需要更改三个文件(类)。所有班级都是here
Main.java
TimesPanel.java(该类将取决于您要更改的内容(面板)。为了解释目的,我们将在报告信息部分下的时间面板中添加内容,因此TimesPanel.java)
和BaseMultiSuitePanel.java
在你的项目中创建一个文件customBaseMultiSuitePanel.java,复制原始文件的内容并相应地修改构造函数。
创建customTimesPanel.java 并复制TimesPanel.java 的内容并对private String js(ISuite suite) 方法进行更改,因为当您单击报告中的时间时,我们将向出现的表中添加成功百分比和测试优先级。
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++;
...
...
}
接下来,创建customIndexHtmlReport.java并将Main.java的内容复制到这个文件中。在public void generateReport() 方法中删除旧的TimesPanel 对象和下面的新对象
new customTimesPanel(m_model)
还可以像这样更改同一文件中的报告名称
Utils.writeUtf8File(m_outputDirectory, "customReport-index.html", xsb, all);
最后,将监听器添加到您的testng.xml
<listener class-name = "firsttestngpackage.customIndexHtmlReport" />
然后您将获得如下所示的自定义报告,其中添加了每个测试的成功百分比和优先级
注意:
确保与customIndexHtmlReport.java 中的getClass().getResourceAsStream 方法相关的资源已正确放置在您的项目中。我遇到了问题。