昨天研究了一下如何生成html文件的測試報告,但沒有發出來寫html文件的代碼,經過整理之後,覺得差不多了。發出來供大家參考。
public static void createWebReport(List<String[]> runresult) throws IOException {
//這個是頁面前面固定的信息
String starttext = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>"
+"</head><body><h1 style='text-align:center'>android UiAutomator測試報告</h1><p style='text-align:center'>"+getNow()+"測試報告"+"</p>"
+"<table border='1' style='table-layout:fixed;font-size:14px;'>"
+"<thead>"
+"<tr><td width='30px'>編號</td>"
+"<td width='120px'>用例名</td>"
+"<td width='70px'>運行狀態</td>"
+"<td width='250px'>錯誤信息</td>"
+"<td width='160px'>錯誤行Library</td>"
+"<td width='160px'>錯誤行Special</td>"
+"<td width='160px'>錯誤行Case</td><td width='100px'>開始時間</td>"
+"<td width='100px'>結束時間</td><td width='100px'>備用列</td></tr>"
+"</thead><tbody>";
//這裏是頁面後面固定的信息
String endtext = "</tbody></table></body></html>";
File file = new File("C:\\Users\\fankaiqiang\\Desktop\\888\\"+getNow()+".html");//新建一個html文檔
if (!file.exists()) {//判斷是否存在,不存在先創建
file.createNewFile();
}
//將運行信息輸出到html文檔中
boolean isClose = false;//寫入時用到
BufferedWriter bf;
FileOutputStream outputStream = new FileOutputStream(file, true);
OutputStreamWriter outWriter = new OutputStreamWriter(outputStream);
String sheet = "";
for (int i = 0; i < runresult.size(); i++) {
sheet = sheet + "<tr>";//添加行
String[] result = runresult.get(i);//獲取用例運行信息
for (int j = 0; j < result.length; j++) {
if (result[2] == "運行成功") {//運行成功,就用綠色
String addtext = "<td style='word-wrap:break-word;word-break:break-all;background-color:paleturquoise'>" + result[j]+ "</td>";
sheet = sheet + addtext;
} else if (result[2] == "斷言錯誤") {//斷言失敗,就用黃色
String addtext = "<td style='word-wrap:break-word;word-break:break-all;background-color:yellow;'>" + result[j]+ "</td>";
sheet = sheet + addtext;
} else if (result[2] == "運行錯誤") {//運行失敗就用紅色
String addtext = "<td style='word-wrap:break-word;word-break:break-all;background-color:red;'>" + result[j]+ "</td>";
sheet = sheet + addtext;
} else {
output("運行信息錯誤!");
}
}
sheet = sheet + "</tr>";//結束行
}
bf = new BufferedWriter(outWriter);//寫入
bf.append(starttext);//寫入頁面前面的信息
bf.append(sheet);//寫入報告信息
bf.append(endtext);//寫入頁面後面信息
bf.flush();
if (isClose) {
bf.close();
}
}
裏面用到了一些自己自定義的方法,大家看方法名就知道做什麼用了,可以忽略。