android UiAutomator運行用例並獲取運行信息的方法

本人在使用調試類做UiAutomator測試的時候,一直想着把UiAutomator自己的測試報告裏的內容進行過濾,篩選出自己需要的信息,經過不斷嘗試和改進,終於完成。分享出來供大家參考。我是用string數組來存放用例信息的,也可以用map或許更好一點,暫時還沒嘗試。

public static String[] execCmdAndReturnResult(String jarName,String testclass, String testName, int orderno) {
		int status = 0;//此參數用於提取運行code
		String runCmd = "adb shell uiautomator runtest ";//調試命令的前半部分
		String testCmd = jarName + ".jar " + "--nohup -c " + testclass + "#" + testName;//調試命令的後半部分
		System.out.println("----runTest:  " + runCmd + testCmd);//輸出調試命令
		String runresult = "";//運行結果
		String runinfo = "";//運行信息
		String errorlineinliabrary = "";//在library類的錯誤行
		String errorlineinspecial = "";//在special類的錯誤行
		String errorlineincase = "";//在case類的錯誤行
		Date time1 = new Date();//開始時間
		SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置時間格式
		String starttime = now.format(time1);//轉換時間格式
		String cmd = runCmd + testCmd;//拼接調試命令
		System.out.println("----execCmd:  " + cmd);//輸出調試命令
		try {
			Process p = Runtime.getRuntime().exec("cmd /c " + cmd);//藉助runtime類執行調試命令
			// 正確輸出流
			InputStream input = p.getInputStream();//正確輸出流,新建輸入流
			BufferedReader reader = new BufferedReader(new InputStreamReader(input));//將字節輸入流轉化爲reader字符輸入流,並用bufferedreader接收數據
			String line = "";//用來接收輸入流字符流的每一行
			while ((line = reader.readLine()) != null) {//每次讀取一行賦值給line
				System.out.println(line);//輸出此行
				saveToFile(line, "reportlog.log", false);//保存此行到log裏面
				if (line.startsWith("INSTRUMENTATION_STATUS_CODE:")) {//獲取運行報告的code碼
					status ++;//此處因爲每一個報告都有不只一個code,我們只要最後一個
					if (status == 2) {//當status=2時,就是我們要的code
						System.out.println(getCode(line));//輸出此行的code
						if (getCode(line).equalsIgnoreCase("-1")) {//如果-1,則運行錯誤
							runresult = "運行錯誤";
							} else if (getCode(line).equalsIgnoreCase("-2")) {//如果-2則是斷言錯誤
								runresult = "斷言錯誤";
							} else {
								runresult = "運行成功";//此處就不判斷其他情況了,直接表示運行成功
								}
						}
					}
				if (line.startsWith("INSTRUMENTATION_STATUS: stack=")) {//獲取錯誤和異常
					runinfo = line.substring(30, line.length());//截取我們需要的錯誤和異常
					}
				if (line.startsWith("	at student.Special.")) {//獲取錯誤在special類發生的行
					errorlineinspecial = line.substring(line.indexOf(".")+1, line.length());//截取錯誤發生行
					}
				if (line.startsWith("	at student.Case.")) {//獲取錯誤在case類發生的行
					errorlineincase = line.substring(line.indexOf(".")+1, line.length());//截取錯誤發生行
					}
				if (line.startsWith("	at student.Library.")) {//獲取錯誤在library類發生的行
					errorlineinliabrary = line.substring(line.indexOf(".")+1, line.length());//截取錯誤發生行
					}
				}
			reader.close();//此處有依賴關係,先關閉reader
			input.close();//關閉字節輸入流
			// 錯誤輸出流
			InputStream errorInput = p.getErrorStream();//獲取錯誤流,新建輸出流
			BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));////將字節輸入流轉化爲reader字符輸入流,並用bufferedreader接收數據
			String eline = "";////用來接收輸入流字符流的每一行
			while ((eline = errorReader.readLine()) != null) {//每次讀取一行賦值給line
				System.out.println(eline);//輸出此行
				saveToFile(line, "runlog.log", false);//保存此行到log裏面
			}
			errorReader.close();//此處有依賴關係,先關閉errorReader
			errorInput.close();//關閉輸入字節流
			} catch (IOException e) {
				e.printStackTrace();//拋出異常
				}
		Date time = new Date();//獲取結束時間
		String endtime = now.format(time);//轉化時間格式
		String[] result = new String[9];//新建數組存放運行信息
		result[0] = (orderno + 1) + "";//表示運行的用例的順序
		result[1] = testName;//用例名
		result[2] = runresult;//運行結果
		result[3] = runinfo;//運行信息
		result[4] = errorlineinliabrary;//在library類的錯誤行
		result[5] = errorlineinspecial;//在special類的錯誤行
		result[6] = errorlineincase;//在case類的錯誤行
		result[7] = starttime;//開始時間
		result[8] = endtime;//結束時間
		return result;//返回運行信息
	}


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