In the process of Android APP stability test, I need to test the stable operation and performance data collection under the scene of constantly refreshing the content of the home page. UiAutomator + multithreading finally solves this problem. The idea is as follows: First write the running script with UiAutomator, then output the debugging command when using fast debugging, and then run the debugging command in the test script. Of course, it also needs multi-threading to help record log and performance data.
The multithreaded class code is as follows:
package monkeytest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.SimpleDateFormat; import java.util.Date; import source.Common; import source.PerformanceThread; public class HomeRefresh { public static String ADB_PATH = Common.ADB_PATH;; public static String package_name = Common.HEAD_PAGEKAGE; public static void main(String[] args) { // String timess = args[0]; // int times = Common.getInstance().changeStringToInt(timess); for (int i = 0; i < 3; i++) { Date start = Common.getInstance().getDate(); Logcat logcat = new Logcat();// New log Thread PerformanceThread performanceThread = new PerformanceThread("homerefresh", package_name);// Performance monitoring performanceThread.start(); logcat.start(); String command = "adb shell uiautomator runtest demo.jar --nohup -c happyjuzi.AppCase#testHomeRefresh"; execCmdAdb(command, "homerefresh" + getNow() + ".log"); logcat.stopLoacat(); performanceThread.stopRecord(); Date end = Common.getInstance().getDate(); Common.getInstance().outputTimeDiffer(start, end); } } /** * Execute the adb command * * @param cmd * Command content * @param fileName * Input file path */ public static void execCmdAdb(String cmd, String fileName) { System.out.println("Execute orders:" + cmd); String OSname = System.getProperty("os.name"); Common.getInstance().saveToFile(Common.getInstance().getNow() + "Start!" + Common.LINE, fileName);// Preservation try { Process p = null; if (OSname.contains("Mac")) { p = Runtime.getRuntime().exec(ADB_PATH + cmd); } else { p = Runtime.getRuntime().exec("cmd /c " + cmd); } // 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 if (line.startsWith("// ")) { continue; } if (line.startsWith("//")) { Common.getInstance().saveToFile(line, fileName);// Preservation } if (line.contains("Exception")) { Common.getInstance().saveToFile("error-homerefresh" + getNow(), "error-homerefresh"+getNow()+".log"); } } 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 Common.getInstance().saveToFile(eline, fileName);// Preservation } errorReader.close();// There is a dependency here, so close errorReader first errorInput.close(); } catch (IOException e) { Common.getInstance().output("implement" + cmd + "Failure!"); e.printStackTrace(); } Common.getInstance().saveToFile(Common.LINE + Common.getInstance().getNow() + "End!", fileName);// Preservation } /** * Get the current time * * @return Returns the current time, only dates and hours and scores, no years and seconds */ private static String getNow() { Date time = new Date(); SimpleDateFormat now = new SimpleDateFormat("MM-dd-HH-mm"); String c = now.format(time); return c; } public void output(String text) { System.out.println(text); } public void output(Object... object) { if (object.length == 1) { output(object[0].toString()); return; } for (int i = 0; i < object.length; i++) { System.out.println("The first" + (i + 1) + "One:" + object[i]); } } }
Running code is as follows, because it is relatively simple, only write the method part of the code.
public void testHeadHomeRefresh() { for (int i = 0; i < 20; i++) { startHead(); waitForUiObjectByResourceId("com.ss.android.article.news:id/b_y"); sleep(1000); for (int k = 0; k < 15; k++) { swipeDown(); sleep(1500); } stopHead(); } }
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