Write a stability test script for homepage refresh using UiAutomator

Keywords: Programming Java Android Linux shell

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

  1. One line of java code prints a heart
  2. Linux performance monitoring software netdata Chinese version
  3. Interface Test Code Coverage (jacoco) Scheme Sharing
  4. Performance testing framework
  5. How to Enjoy Performance Testing on Linux Command Line Interface
  6. Graphic HTTP Brain Map
  7. Writing to everyone about programming thinking
  8. How to Test Probabilistic Business Interface
  9. httpclient handles multi-user simultaneous online
  10. Ten Steps to Become a Great Java Developer
  11. Automatically convert swagger documents into test code

Public Number Map ☢️ Together ~FunTester

Posted by squalls_dreams on Sun, 06 Oct 2019 00:29:21 -0700