1. 程式人生 > >利用UiAutomator寫一個自動遍歷渠道包關鍵功能的指令碼

利用UiAutomator寫一個自動遍歷渠道包關鍵功能的指令碼

本人在做自動化測試的過程中,出現了一個需求。原因是,在發出去的渠道包裡面,偶然一次有兩個渠道包微博登入失敗的bug,所以想著利用UiAutomator寫了一個自動遍歷每個渠道包的登入方式的指令碼。經過嘗試第一版終於完成,分享程式碼和思路,供大家參考。

思路:把所有渠道包放在一個apk的資料夾裡面。寫好程式碼打包成jar包,先push到手機中,然後再匯出一個jar包,再這個包裡用命令執行UiAutomator指令碼。輸出結果並儲存日誌在當前目錄下。

下面是放在電腦上的jar包程式入口所在的類的程式碼:

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();//獲取當前路徑
		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);
			}
		}
	}
}
下面是自己除錯類的程式碼,暫且把需要執行的方法直接寫在這裡了,如果你也要寫指令碼,不建議這麼做。
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();
		// PerformanceThread.getInstance().start();//啟動執行緒
		new UiAutomatorHelper(jarName, testClass, testName);// 除錯用例
		// PerformanceThread.key = false;//結束執行緒
	}

	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();
	}
}
下面是這三個封裝方法的程式碼:
/**
	 * 跳過引導頁
	 * 
	 * @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");
		}
	}

	/**
	 * 登入
	 * 
	 * @param key
	 *            選擇登入方式
	 * @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;
		}
	}

	/**
	 * 檢查是否登入成功
	 */
	public void checkIsLogin() {
		if (getUiObjectByText("點選頭像登入").exists()) {
			outpu("登入失敗!");
		} else {
			output("登入成功!");
		}
	}

由於時間比較緊,所以封裝做得不夠好,註釋也少。先看看實際效果後期再改進,畢竟需求是會不斷變化的。

匯出jar包的時候,如果是Mac執行jar包,一定要修改裡面ADB_PATH地址,不然會報錯。具體匯出jar包檔案的辦法,部落格裡上一篇帖子就是。