In the process of using UiAutomator, I have been testing with quick debugging classes. I find that many of them need to make changes according to different needs. Today, I spent a little time in general revision, more flexible, and wrote a lot of Chinese annotations. Share it for your reference.
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";//Android Id, write well without reference private static String jar_name = "";//jar name private static String test_class = "";//Package name. Class name private static String test_name = "";//Use case name private static String devices = UseOften.NEXUS5DEVICESID;//Custom Device ID private static String workspace_path;// The workspace does not need to be configured to automatically get the workspace directory public UiAutomatorHelper() {//If a class has a parametric construction method, it must write out the hidden empty parameter construction method. Library.getInstance().output("Welcome to use custom debugging classes!"); } public UiAutomatorHelper(String jarName, String testClass, String testName) { workspace_path = getWorkSpase(); jar_name = jarName; test_class = testClass; test_name = testName; UseOften.getInstance().setMobileInputMethodToUtf();//Set the input method to utf7 runUiautomator(); UseOften.getInstance().setMobileInputMethodToQQ();//Set input method to QQ input method System.out.println(UseOften.LINE+"---FINISH DEBUG----"+UseOften.LINE);//End } // Operation steps private void runUiautomator() { creatBuildXml(); modfileBuild(); buildWithAnt(); pushTestJar(workspace_path + "\\bin\\" + jar_name + ".jar"); runTest(jar_name, test_class + "#" + test_name); } //Create build.xml public void creatBuildXml() { execCmd("cmd /c android create uitest-project -n " + jar_name + " -t " + android_id + " -p " + "\"" + workspace_path + "\""); } //Modify build public void modfileBuild() { StringBuffer stringBuffer = new StringBuffer();//Create and instantiate stringbuffer try { File file = new File("build.xml"); if (file.isFile() && file.exists()) { //Determine whether a file exists InputStreamReader read = new InputStreamReader(new FileInputStream(file));//Create and instantiate the output character stream (stream conversion) through the file byte input stream BufferedReader bufferedReader = new BufferedReader(read);//Create and instantiate BufferedReader to receive character streams String lineTxt = null;//Used to receive readline results while ((lineTxt = bufferedReader.readLine()) != null) {//Loop Read Processing Content if (lineTxt.matches(".*help.*")) {//Regular matching lineTxt = lineTxt.replaceAll("help", "build");//Replace help with build } stringBuffer = stringBuffer.append(lineTxt + "\t\n");//stringbuffer receives modified content } bufferedReader.close();//Close the flow, depend on it, so close first read.close();//Closed flow } else { System.out.println("The specified file could not be found"); } } catch (Exception e) { System.out.println("Error reading file contents"); e.printStackTrace(); } // Write it back after revision writerText("build.xml", new String(stringBuffer)); } //ant performs build public void buildWithAnt() { execCmd("cmd /c ant"); } //jar package push to mobile phone. public void pushTestJar(String localPath) { localPath = "\"" + localPath + "\""; String pushCmd = "adb -s "+devices+" push " + localPath + " /data/local/tmp/"; execCmd(pushCmd); } //Running Use Case Method public void runTest(String jarName, String testName) { String runCmd = "adb -s "+devices+" shell uiautomator runtest ";//Here - s denotes the nexus machine String testCmd = jarName + ".jar " + "--nohup -c " + testName; execCmd(runCmd + testCmd); } //Getting workspace public String getWorkSpase() { File directory = new File("");//Create and instantiate file objects String abPath = directory.getAbsolutePath();//Get the absolute path return abPath; } //Execute the cmd command public void execCmd(String cmd) { try { Process p = Runtime.getRuntime().exec(cmd);//Executing cmd commands through runtime classes // Correct Output Flow InputStream input = p.getInputStream();//Create and instantiate the input byte stream BufferedReader reader = new BufferedReader(new InputStreamReader(input));//First, flow transformation is done through inputstreamreader, and then bufferedreader is instantiated to receive content. String line = ""; while ((line = reader.readLine()) != null) {//Cyclic reading System.out.println(line);//output saveToFile(line, "runlog.log", false);//Save, false means uncovered } reader.close();//Here reader depends on input and should be turned off first input.close(); // Error Output Stream InputStream errorInput = p.getErrorStream();//Create and instantiate the input byte stream BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));//First, flow transformation is done through inputstreamreader, and then bufferedreader is instantiated to receive content. String eline = ""; while ((eline = errorReader.readLine()) != null) {//Cyclic reading System.out.println(eline);//output saveToFile(eline, "runlog.log", false);//Save, false means uncovered } errorReader.close();//There is a dependency here, so close errorReader first errorInput.close(); } catch (IOException e) { e.printStackTrace(); } } //Overwrite Write File public void writerText(String path, String content) { File dirFile = new File(path); if (!dirFile.exists()) {//If it does not exist, build a new one dirFile.mkdir(); } try { //Add true here, you can not overwrite the original TXT file content, continue to write BufferedWriter bw1 = new BufferedWriter(new FileWriter(path));//Receiving and writing with bufferedwrite through file output stream bw1.write(content);//Write content to a file bw1.flush();//Forced Output Buffer Content bw1.close();//Closed flow } catch (IOException e) { e.printStackTrace(); } } //Write to the document. For comments, see the writerText method 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);//Add content bf.newLine(); bf.flush(); if (isClose) { bf.close(); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Selection of previous articles
- One line of java code prints a heart
- Linux performance monitoring software netdata Chinese version
- Interface Test Code Coverage (jacoco) Scheme Sharing
- Performance testing framework
- How to Enjoy Performance Testing on Linux Command Line Interface
- Graphic HTTP Brain Map
- Writing to everyone about programming thinking
- How to Test Probabilistic Business Interface
- httpclient handles multi-user simultaneous online
- Ten Steps to Become a Great Java Developer
- Automatically convert swagger documents into test code
- Five lines of code to build static blogs
- How httpclient handles 302 redirection
- Why choose software testing as a career path?