android UiAutomator如何把log、截圖和測試報告放在一起

本人在使用android UiAutomator做測試的時候,在斷言之前都會截圖留證,方便以後查找。隨着執行的次數增多,截圖也越來越多,log文件也變成了上萬行,如果找起來非常不方便。故而想了一個把每一次運行的相關信息放到一個文件夾中,這樣方便查找截圖和log。經過嘗試終於成功了。分享代碼如下,供大家參考。

package student;  
  
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;

public class ClassReport extends Library{
	//VERSION_KEY爲false時,測試環境;爲true時,正式環境
	public static final boolean version = false;
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException, SQLException, ParseException {
    	String jarname = "Dream22";//設置生成jar包的名字
    	String logPath = "C:\\Users\\fankaiqiang\\workspace\\superclass-app\\reportlog.log";//運行log地址
    	String htmlPath = "C:\\Users\\fankaiqiang\\Desktop\\888\\UiAutomator.html";//測試報告
    	String backgroundPath = "C:\\Users\\fankaiqiang\\Desktop\\888\\222.jpg";//背景圖地址
    	String time = Excel.getNow();//獲取時間,也是測試報告文件夾的名稱
    	String reportPath = creatNewFile(time);
    	/*刪除已有的log文件和測試報告文件
    	 * 之所以放在開始因爲java寫入文件後,這些文件是被佔用的無法刪除
    	 */
    	File oldfile1 = new File(htmlPath);
        File oldfile2 = new File(logPath);
		oldfile1.delete();
		oldfile2.delete();
    	boolean key = false;//爲true時生成excel測試報告,爲false生成html測試報告
        //這裏把所有的測試方法放到了一個list集合中,可以靈活控制需要運行的用例  
        List<String> MethodList = new ArrayList<String>();  
        MethodList.add("test001LearnCornerAddPost");  
        MethodList.add("test002RechargeByWechat");  
        MethodList.add("test003RechargeByAlipay");  
        MethodList.add("test004BuyCourseByWechat");  
        MethodList.add("test005AddAndDeleteAdress");  
        MethodList.add("test006Chatroom");  
        MethodList.add("test007ShareCourseToWechat");  
        MethodList.add("test008ShareCourseToFriendCircle");  
        MethodList.add("test009ShareCourseToQQ");  
        MethodList.add("test010ShareCourseToQzone");  
        MethodList.add("test011AlterPassword");  
        MethodList.add("test012AlterUserName");  
        MethodList.add("test013BuyCourseByAlipay");
        MethodList.add("test014SubmitLectureExercise");
        MethodList.add("test015ConsultGaotuTeacher");
        MethodList.add("test016ChatroomByCourse");
        MethodList.add("test017PlayCourseVideo");
        MethodList.add("test018PlayLectureVideo");
        MethodList.add("test019SalePrice");
        MethodList.add("test020LectureLive");
        
        //準備必要數據
        if (version) {
        	output();
        	} else {
        		MySql.updateLiveTime();//更新直播公開課直播時間
        		MySql.DeleteUserLectureExercise();//刪除作業提交記錄
//        		MySql.UpdateLectureByUserid();//更新作業狀態可提交
        		}
//        fail();//用語單獨執行準備數據的方法
        List<String[]> sheet = new ArrayList<String[]>();//新建list,用於存放每個測試用例的測試結果
        if (key) {
        	String[] title = {"編號", "用例名", "運行狀態", "錯誤信息", "錯誤行Library", "錯誤行Special", "錯誤行Case", "開始時間", "結束時間"};
        	sheet.add(title);//把標題行先加入表信息
             }
        new RunHelper(jarname, "1");//新編譯jar包並push到手機上
        setMobileInputMethodToUtf();//設置手機輸入法爲UTF-7
        for(int i = 0;i < MethodList.size(); i++){//遍歷運行所有方法 
        	String[] result = execCmdAndReturnResult(jarname, "student.Case", MethodList.get(i), i);//運行測試用例  
            sheet.add(result);//將此次用例的測試結果放入list中  
            }
        if (key) {
        	Map<Integer, List<String[]>> report = new HashMap<Integer, List<String[]>>();//新建map,用於存放多張表格數據
        	 report.put(1, sheet);//把第一個表格的測試數據放入要寫入到map裏  
        	 Excel.writeXlsx(report);//把測試報告寫入excel表格中
        	 } else {
        		 WriteHtml.createWebReport(sheet);
        		 }
        copyFile(htmlPath, reportPath+"\\UiAutomator.html");//複製測試報告
        copyFile(logPath, reportPath+"\\reportlog.log");//複製運行log
        copyFile(backgroundPath, reportPath+"\\222.jpg");//複製背景圖
        execCmd("adb -s 06dbd10c0ae4e3af pull /mnt/sdcard/123/ C:\\Users\\fankaiqiang\\Desktop\\888\\testreport"+time);//獲取運行截圖
        execCmdAndReturnResult(jarname, "student.Case", "lastCase", 100);//刪除手機運行截圖
        
    	
        
        
        setMobileInputMethodToQQ();//設置手機輸入法爲QQ輸入法  
        }
    
}  

lastcase用例是用來清除手機上面的截圖的代碼如下:

public void deleteScreenShot() {//刪除文件
		File file = new File("/mnt/sdcard/123/");
		if (file.exists()) {
			File[] files = file.listFiles();  
		    for (int i = 0; i < files.length; i++) {  
		    	files[i].delete();
		    }
		    file.delete();
		} else {
			System.out.println("文件夾不存在!");
		}
		
	}

中間有一個坑,嘗試了很多次沒有解決,本來打算刪除log和測試報告放在結束,可是用delete()和renameto(),都不好用,而且不報錯,經過查資料,覺得可能是文件被佔用了,故在一開始就把原來的log文件和測試報告情況。避免產生混淆。效果如下,關於文件名還在優化。123是測試截圖。222是背景圖,UiAutomator是html的測試報告。因爲使用html做的,所以excel的測試報告沒有生成,所以這裏看不到。


發佈了102 篇原創文章 · 獲贊 40 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章