Write a script to automatically traverse the key functions of channel packages using UiAutomator

In the process of automated testing, there is a need. The reason is that in the outgoing channel package, there are occasionally two channel packages that failed to log on to Weibo, so I want to use UiAutomator to write a script to automatically traverse the login mode of each channel package. After experimenting with the first edition, we finally finished it, sharing the code and ideas for your reference.

Idea: Put all channels in an apk folder. Write the code and pack it into a jar package. push it into the phone first, then export a jar package, and execute the UiAutomator script with commands in the package. Output the results and save the log in the current directory.

The following is the code of the class where the jar package program entry is placed on the computer:

package happyjuzi;
 
import java.io.File;
import source.Common;
 
public class Script extends Common {
	public static void main(String[] args) {
		Script script = new Script();
		script.testDemo();
	}
 
	public static Script getInstance() {
		return new Script();
	}
 
	public void testDemo() {
		String home = getWorkSpase();//Get the current path
		output(home);
		File file = new File(home + "/apk");
		// File file = new File("/Users/dahaohaozai/Desktop" + "/apk");
		File[] file2 = file.listFiles();
		for (int i = 0; i < file2.length; i++) {
			File apk = file2[i];
			String path = apk.getAbsolutePath();
			for (int k = 0; k < 4; k++) {
				output(apk.getName(), i + 1);
				execCmd(ADB_PATH + "adb uninstall com.happyjuzi.apps.juzi");
				execCmd(ADB_PATH + "adb install " + path);
				execCmd(ADB_PATH + "adb shell uiautomator runtest demo.jar --nohup -c happyjuzi.AppTest#testTest" + k);
			}
		}
	}
}

Here's the code for debugging your own class. For the time being, write the methods you need to run directly here. If you want to write scripts, it's not recommended to do so.

package happyjuzi;
 
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import com.android.uiautomator.core.UiObjectNotFoundException;
import android.os.RemoteException;
import source.UiAutomatorHelper;
 
@SuppressWarnings("deprecation")
public class AppTest extends AppCase {
	public static String jarName, testClass, testName, androidId;
 
	public static void main(String[] args)
			throws ClassNotFoundException, SQLException, InterruptedException, IOException, ParseException {
		jarName = "demo";
		testClass = "happyjuzi.AppTest";
		testName = "testTest0";
		// Script.getInstance().testDemo();
		// Performance Thread. getInstance (). start ();// Start Thread
		new UiAutomatorHelper(jarName, testClass, testName);// Debug use case
		// Performance Thread. key = false; // End Thread
	}
 
	public void testTest0() throws InterruptedException, IOException, UiObjectNotFoundException, RemoteException {
		startJuziApp();
		skipGuideage();
		login(0);
		checkIsLogin();
	}
 
	public void testTest1() throws InterruptedException, IOException, UiObjectNotFoundException, RemoteException {
		startJuziApp();
		skipGuideage();
		login(1);
		checkIsLogin();
	}
 
	public void testTest2() throws InterruptedException, IOException, UiObjectNotFoundException, RemoteException {
		startJuziApp();
		skipGuideage();
		login(2);
		checkIsLogin();
	}
 
	public void testTest3() throws InterruptedException, IOException, UiObjectNotFoundException, RemoteException {
		startJuziApp();
		skipGuideage();
		login(3);
		checkIsLogin();
	}
}

Here is the code for these three encapsulation methods:

/**
	 * Skip the boot page
	 * 
	 * @throws IOException
	 * @throws InterruptedException
	 * @throws UiObjectNotFoundException
	 */
	public void skipGuideage() throws IOException, InterruptedException, UiObjectNotFoundException {
		startJuziApp();
		waitForUiObjectByResourceId("com.happyjuzi.apps.juzi:id/btn_skip");
		swipeLeft();
		swipeLeft();
		waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/btn_start");
		sleep(5000);
		if (getUiObjectByResourceId("com.happyjuzi.apps.juzi:id/close").exists()) {
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/close");
		}
	}
 
	/**
	 * Sign in
	 * 
	 * @param key
	 *            Select login mode
	 * @throws UiObjectNotFoundException
	 */
	public void login(int key) throws UiObjectNotFoundException {
		switch (key) {
		case 0:
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/btn_profile");
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/avatar_default_view");
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/wx_view");
			waitForUiObjectByResourceId("com.happyjuzi.apps.juzi:id/protrait_item_main");
			break;
		case 1:
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/btn_profile");
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/avatar_default_view");
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/qq_view");
			sleep(5000);
			if (getUiObjectByResourceId("com.tencent.mobileqq:id/name").exists()) {
				clickPiont(500, 1820);
			}
			waitForUiObjectByResourceId("com.happyjuzi.apps.juzi:id/protrait_item_main");
			break;
		case 2:
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/btn_profile");
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/avatar_default_view");
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/sina_view");
			sleep(5000);
			if (getUiObjectByResourceId("com.sina.weibo:id/bnLogin").exists()) {
				waitForResourceIdAndClick("com.sina.weibo:id/bnLogin");
			}
			waitForUiObjectByResourceId("com.happyjuzi.apps.juzi:id/protrait_item_main");
			break;
		case 3:
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/btn_profile");
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/avatar_default_view");
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/phone_view");
			writeTextByResourceId("com.happyjuzi.apps.juzi:id/phone_num_view", "******");
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/next_view");
			writeTextByResourceId("com.happyjuzi.apps.juzi:id/pwd_view", "*****");
			waitForResourceIdAndClick("com.happyjuzi.apps.juzi:id/next_view");
			waitForUiObjectByResourceId("com.happyjuzi.apps.juzi:id/protrait_item_main");
			break;
		default:
			break;
		}
	}
 
	/**
	 * Check whether the login is successful
	 */
	public void checkIsLogin() {
		if (getUiObjectByText("Click on the avatar to log in").exists()) {
			outpu("Logon failed!");
		} else {
			output("Log in successfully!");
		}
	}

When exporting jar packages, if it is a Mac running jar packages, be sure to modify the ADB_PATH address inside, otherwise it will report an error. The way to export the jar package file is the last post in the blog.

Selection of Technical Articles

  1. One line of java code prints a heart
  2. Chinese Language Version of Linux Performance Monitoring Software netdata
  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. How to Test Probabilistic Business Interface
  8. httpclient handles multi-user simultaneous online
  9. Automatically convert swagger documents into test code
  10. Five lines of code to build static blogs
  11. How httpclient handles 302 redirection
  12. A preliminary study on the testing framework of linear interface based on java
  13. Tcloud Cloud Measurement Platform

Selection of non-technical articles

  1. Why choose software testing as a career path?
  2. Ten Steps to Become a Great Java Developer
  3. Writing to everyone about programming thinking
  4. Obstacles to automated testing
  5. The Problems of Automated Testing
  6. Tested "Code Immortality" Brain Map

Click on the Public Number Map

Keywords: Programming Java Android Linux shell

Added by sv4rog on Wed, 11 Sep 2019 10:58:55 +0300