本人在學習UiAutomator的時候,發現UiAutomatorHelper快速調試類非常好用,最近想了一下利用UiAutomatorHelper類來生成一個測試報告,好得比那些亂七八糟的runlog好看多了。原理很簡單,我就把我增加的代碼貼出來好了。供大家參考,後期肯定好得做一些優化。
首先修改的是execCmd方法,其中在輸出正確流的地方增加了一些判斷。
while ((line = reader.readLine()) != null) {
if (line.startsWith("INSTRUMENTATION_STATUS: test=")) {
saveToFile("運行用例名稱:"+getTest(line), "report.log", false);
}
if (line.startsWith("INSTRUMENTATION_STATUS: current")) {
saveToFile("正在運行第"+getCurrent(line)+"個用例!", "report.log", false);
}
if (line.startsWith("INSTRUMENTATION_STATUS_CODE:")) {
if (getCode(line).equalsIgnoreCase("-1")) {
saveToFile("\n"+"---------------運行狀態:運行錯誤!"+"\n", "report.log", false);
}else if (getCode(line).equalsIgnoreCase("-2")) {
saveToFile("\n"+"---------------運行狀態:斷言錯誤!"+"\n", "report.log", false);
}else {
saveToFile("運行狀態:運行OK!", "report.log", false);
}
}
System.out.println(line);
saveToFile(line, "runlog.log", false);
}
下面是一些提取有用信息的方法:
public String getTest(String text) {
return text.substring(29, text.length());
}
public String getCode(String text) {
return text.substring(29, text.length());
}
public String getCurrent(String text) {
return text.substring(32, text.length());
}
public String getNow() {//獲取當前時間
Date time = new Date();
SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String c = now.format(time);
return c;
}
生成報告的樣子有點粗糙:
用例運行開始2017-04-12 17:01:02
運行用例名稱:testAddAndDeleteAdress
正在運行第1個用例!
運行狀態:運行OK!
運行用例名稱:testAddAndDeleteAdress
正在運行第1個用例!
---------------運行狀態:運行錯誤!
運行用例名稱:testBuyCourseByWechat
正在運行第2個用例!
運行狀態:運行OK!
運行用例名稱:testBuyCourseByWechat
正在運行第2個用例!
---------------運行狀態:斷言錯誤!
運行用例名稱:testChatroom
正在運行第3個用例!
運行狀態:運行OK!
運行用例名稱:testChatroom
正在運行第3個用例!
---------------運行狀態:運行錯誤!
運行用例名稱:testLearnCornerAddQuestion
正在運行第4個用例!
運行狀態:運行OK!
運行用例名稱:testLearnCornerAddQuestion
正在運行第4個用例!
運行狀態:運行OK!
運行用例名稱:testVerifyGradeInScreenAndMyInfo
正在運行第5個用例!
運行狀態:運行OK!
運行用例名稱:testVerifyGradeInScreenAndMyInfo
正在運行第5個用例!
運行狀態:運行OK!