testng自帶的html測試報告不是很好用,打開慢,内容簡單,基本不具備什麼參考價值,所有想到自己重新定義一份html格式的報告,可以參考jsp轉換成html頁面的原理+testng監聽器實作。
代碼如下:
1.實作testng listener接口:
public class testReport implements ITestListener
2.實作ItestListener 接口的onStart方法:
public void onStart(ITestContext context) {
File htmlReportDir = new File("test-output/html-report");
if (!htmlReportDir.exists()) {
htmlReportDir.mkdirs();
}
String reportName = String.valueOf(new Date().getTime())+".html";
reportPath = htmlReportDir+"/"+reportName;
File report = new File(htmlReportDir,reportName);
if(report.exists()){
try {
report.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
String msg = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><meta name=\"author\" content=\"ec-qa:xin.wang\"><meta name=\"description\" content=\"自動化測試用例結果清單\"><title>UI自動化測試報告</title></head><body STYLE=\"background-color:#00CCCC;\" align=\"center\"><br><a style=\"font-weight:bold;\">測試用例運作結果清單</a><br><br><table width=\"90%\" height=\"80\" 1\" align=\"center\" style=\"table-layout:fixed;\"><thead><tr><th>測試用例名</th><th>測試用例結果</th></tr></thead><tbody style=\"word-wrap:break-word;font-weight:bold;\">";
try {
Files.write((Paths.get(reportPath)),msg.getBytes("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
3. 實作ItestListener 接口的onTestSkipped方法:
public void onTestSkipped(ITestResult result) {
StringBuilder caseRes = new StringBuilder("<tr><td>");
caseRes.append(result.getMethod().getRealClass()+"."+result.getMethod().getMethodName());
caseRes.append("</td><td>Skipped</td></tr>");
String res = caseRes.toString();
try {
Files.write((Paths.get(reportPath)),res.getBytes("utf-8"),StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
4. 實作ItestListener 接口的onTestSuccess方法:
public void onTestSuccess(ITestResult result) {
StringBuilder caseRes = new StringBuilder("<tr><td>");
caseRes.append(result.getMethod().getRealClass()+"."+result.getMethod().getMethodName());
caseRes.append("</td><td>Passed</td></tr>");
String res = caseRes.toString();
try {
Files.write((Paths.get(reportPath)),res.getBytes("utf-8"),StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
5. 實作ItestListener 接口的onTestFailure方法:
public void onTestFailure(ITestResult result) {
String screenShotTip = null;
try {
if (PropertiesUtil.getProValue("testcase.failedcase.screenShotEnabled").equals("true")) {
ScreenShotOnFailure.takeScreentShot();
screenShotTip= result.getMethod().getMethodName() + " failed, the screenshot saved in "
+ ScreenShotOnFailure.getScreenShotPath() + " screenshot name : "
+ ScreenShotOnFailure.getScreenShotName();
System.out.println(screenShotTip);
}
} catch (IOException e) {
e.printStackTrace();
}
StringBuilder caseRes = new StringBuilder("<tr><td>");
caseRes.append(result.getMethod().getRealClass()+"."+result.getMethod().getMethodName());
caseRes.append("</td><td><br>");
caseRes.append(screenShotTip);
caseRes.append("<br><br>");
Throwable throwable = result.getThrowable();
caseRes.append("測試用例 運作失敗,原因: ");
caseRes.append(throwable.getMessage());
caseRes.append("<br><br>");
StackTraceElement[] se = throwable.getStackTrace();
caseRes.append("堆棧資訊:");
caseRes.append("<br>");
for (StackTraceElement e : se) {
caseRes.append(e.toString());
caseRes.append("<br>");
}
caseRes.append("</td></tr>");
String res = caseRes.toString();
try {
Files.write((Paths.get(reportPath)),res.getBytes("utf-8"),StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
在測試結束時把html标簽閉合(測試過程中就可以打開測試報告看已運作的測試用例的報告,雖然此時html标簽并不閉合,但是目前主流浏覽器都可以自動識别 并正确顯示頁面内容):
public void onFinish(ITestContext testContext) {
String msg = "</tbody></table></body></html>";
try {
Files.write((Paths.get(reportPath)),msg.getBytes("utf-8"),StandardOpenOption.APPEND);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
6在測試用例中添加此監聽器類:
/**
*
* @auther xin.wang
*登入背景管理系統
*/
@Listeners({ testReport.class })
public class LoginAdminPortal extends AdminPortalTestBase{
@AutoInject
private NavigationMenu ctPage;
@Test(invocationCount = 20)
public void loginAdminPortal() throws Exception{
AssertJUnit.assertEquals("歡迎您,",ctPage.getWelcomeContent());
AssertJUnit.assertEquals("超級管理者1",ctPage.getCurLoginUser());
ctPage.goToProdcutLibPage();
}
}
7.運作測試,并在目标目錄檢視測試報告:
出現失敗用例時的報告:
3. 實作ItestListener 接口的onTestSkipped方法: