android UiAutomator自定義快速調試類

本人在使用UiAutomator的過程當中,一直用快速調試類來作測試,發現其中不少地方都須要根據不一樣的需求作修改,今天特地花了點時間整體修改一遍,更加靈活了,又寫了不少中文註釋。分享出來,供你們參考。java

package student;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
public class UiAutomatorHelper {
 
	private static String android_id = "1";//androidId,寫好不用傳參
	private static String jar_name = "";//jar名字
	private static String test_class = "";//包名.類名
	private static String test_name = "";//用例名
	private static String devices = UseOften.NEXUS5DEVICESID;//自定義設備ID
	private static String workspace_path;// 工做空間不須要配置,自動獲取工做空間目錄
	public UiAutomatorHelper() {//若是類有帶參構造方法,必須把隱藏的空參構造方法寫出來
		Library.getInstance().output("歡迎使用自定義調試類!");
	}
	public UiAutomatorHelper(String jarName, String testClass, String testName) {
		workspace_path = getWorkSpase();
		jar_name = jarName;
		test_class = testClass;
		test_name = testName;
		UseOften.getInstance().setMobileInputMethodToUtf();//設置輸入法爲utf7
		runUiautomator();
		UseOften.getInstance().setMobileInputMethodToQQ();//設置輸入法爲QQ輸入法
		System.out.println(UseOften.LINE+"---FINISH DEBUG----"+UseOften.LINE);//結束
	}
	// 運行步驟
	private void runUiautomator() {
		creatBuildXml();
		modfileBuild();
		buildWithAnt();
		pushTestJar(workspace_path + "\\bin\\" + jar_name + ".jar");
		runTest(jar_name, test_class + "#" + test_name);
	}
 
	//建立build.xml
	public void creatBuildXml() {
		execCmd("cmd /c android create uitest-project -n " + jar_name + " -t " + android_id + " -p " + "\""
				+ workspace_path + "\"");
	}
	//修改build
	public void modfileBuild() {
		StringBuffer stringBuffer = new StringBuffer();//建立並實例化stringbuffer
		try {
			File file = new File("build.xml");
			if (file.isFile() && file.exists()) { //判斷文件是否存在
				InputStreamReader read = new InputStreamReader(new FileInputStream(file));//經過文件字節輸入流建立並實例化輸出字符流(流轉換)
				BufferedReader bufferedReader = new BufferedReader(read);//建立並實例化BufferedReader,用來接收字符流
				String lineTxt = null;//用來接收readline的結果
				while ((lineTxt = bufferedReader.readLine()) != null) {//循環讀取處理內容
					if (lineTxt.matches(".*help.*")) {//正則匹配
						lineTxt = lineTxt.replaceAll("help", "build");//替換help爲build
					}
					stringBuffer = stringBuffer.append(lineTxt + "\t\n");//stringbuffer接收修改後的內容
				}
				bufferedReader.close();//關閉流,有依賴關係因此先關閉
				read.close();//關閉流
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("讀取文件內容出錯");
			e.printStackTrace();
		}
		// 修改後寫回去
		writerText("build.xml", new String(stringBuffer));
	}
	//ant 執行build
	public void buildWithAnt() {
		execCmd("cmd /c ant");
	}
	//把jar包push到手機上
	public void pushTestJar(String localPath) {
		localPath = "\"" + localPath + "\"";
		String pushCmd = "adb -s "+devices+" push " + localPath + " /data/local/tmp/";
		execCmd(pushCmd);
	}
	//運行用例方法
	public void runTest(String jarName, String testName) {
		String runCmd = "adb -s "+devices+" shell uiautomator runtest ";//此處-s表示nexus機器
		String testCmd = jarName + ".jar " + "--nohup -c " + testName;
		execCmd(runCmd + testCmd);
	}
	//獲取工做空間
	public String getWorkSpase() {
		File directory = new File("");//建立並實例化file對象
		String abPath = directory.getAbsolutePath();//獲取絕對路徑
		return abPath;
	}
	//執行cmd命令
	public void execCmd(String cmd) {
		try {
			Process p = Runtime.getRuntime().exec(cmd);//經過runtime類執行cmd命令
			// 正確輸出流
			InputStream input = p.getInputStream();//建立並實例化輸入字節流
			BufferedReader reader = new BufferedReader(new InputStreamReader(input));//先經過inputstreamreader進行流轉化,在實例化bufferedreader,接收內容
			String line = "";
			while ((line = reader.readLine()) != null) {//循環讀取
					System.out.println(line);//輸出
					saveToFile(line, "runlog.log", false);//保存,false表示不覆蓋
					}
			reader.close();//此處reader依賴於input,應先關閉
			input.close();
			// 錯誤輸出流
			InputStream errorInput = p.getErrorStream();//建立並實例化輸入字節流
			BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));//先經過inputstreamreader進行流轉化,在實例化bufferedreader,接收內容
			String eline = "";
			while ((eline = errorReader.readLine()) != null) {//循環讀取
				System.out.println(eline);//輸出
				saveToFile(eline, "runlog.log", false);//保存,false表示不覆蓋
			}
			errorReader.close();//此處有依賴關係,先關閉errorReader
			errorInput.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//覆蓋寫入文件
	public void writerText(String path, String content) {
		File dirFile = new File(path);
		if (!dirFile.exists()) {//若是不存在,新建
			dirFile.mkdir();
		}
		try {
			//這裏加入true 能夠不覆蓋原有TXT文件內容,續寫
			BufferedWriter bw1 = new BufferedWriter(new FileWriter(path));//經過文件輸出流來用bufferedwrite接收寫入
			bw1.write(content);//將內容寫到文件中
			bw1.flush();//強制輸出緩衝區內容
			bw1.close();//關閉流
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//寫入文檔,註釋見writerText方法
	public void saveToFile(String text, String path, boolean isClose) {
		File file = new File("runlog.log");
		BufferedWriter bf = null;
		try {
			FileOutputStream outputStream = new FileOutputStream(file, true);
			OutputStreamWriter outWriter = new OutputStreamWriter(outputStream);
			bf = new BufferedWriter(outWriter);
			bf.append(text);//添加內容
			bf.newLine();
			bf.flush();
			if (isClose) {
				bf.close();
			}
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
 
	} 
}

往期文章精選

  1. java一行代碼打印心形
  2. Linux性能監控軟件netdata中文漢化版
  3. 接口測試代碼覆蓋率(jacoco)方案分享
  4. 性能測試框架
  5. 如何在Linux命令行界面愉快進行性能測試
  6. 圖解HTTP腦圖
  7. 寫給全部人的編程思惟
  8. 如何測試機率型業務接口
  9. httpclient處理多用戶同時在線
  10. 成爲傑出Java開發人員的10個步驟
  11. 將swagger文檔自動變成測試代碼
  12. 五行代碼構建靜態博客
  13. httpclient如何處理302重定向
  14. 爲何選擇軟件測試做爲職業道路?

公衆號地圖 ☢️ 一塊兒來~FunTester

相關文章
相關標籤/搜索