android UiAutomator自定義快速調試類

本人在使用UiAutomator的過程中,一直用快速調試類來做測試,發現其中很多地方都需要根據不同的需求做修改,今天特意花了點時間總體修改一遍,更加靈活了,又寫了很多中文註釋。分享出來,供大家參考。

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();
		}

	}



}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章