本人在作自動化測試的過程當中,出現了一個需求。緣由是,在發出去的渠道包裏面,偶然一次有兩個渠道包微博登陸失敗的bug,因此想着利用UiAutomator寫了一個自動遍歷每一個渠道包的登陸方式的腳本。通過嘗試初版終於完成,分享代碼和思路,供你們參考。java
思路:把全部渠道包放在一個apk的文件夾裏面。寫好代碼打包成jar包,先push到手機中,而後再導出一個jar包,再這個包裏用命令執行UiAutomator腳本。輸出結果並保存日誌在當前目錄下。android
下面是放在電腦上的jar包程序入口所在的類的代碼:sql
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); } } } }
下面是本身調試類的代碼,暫且把須要運行的方法直接寫在這裏了,若是你也要寫腳本,不建議這麼作。shell
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包文件的辦法,博客裏上一篇帖子就是。app