標籤:
用uiautomator做安卓的app端的UI testing的環境搭建及編jar包和運行case的步驟如下:
1、建立java工程
2、右鍵properties,
添加junit4的library,並添加android.jar和uiautomator的jar包依賴
3、在工程下建立package,之後建立類,代碼如下:
(備忘:extends的類需要寫清楚是UiAutomatorTestCase ,具體的方法需要以test開頭,並且throws UiObjectNotFoundException )
1 package com.ganji.loginhelper; 2 3 4 5 import com.android.uiautomator.core.UiObject; 6 7 import com.android.uiautomator.core.UiObjectNotFoundException; 8 9 import com.android.uiautomator.core.UiScrollable; 10 11 import com.android.uiautomator.core.UiSelector; 12 13 import com.android.uiautomator.testrunner.UiAutomatorTestCase; 14 15 public class Runner extends UiAutomatorTestCase { 16 17 public void testDemo() throws UiObjectNotFoundException { 18 19 // Simulate a short press on the HOME button. 20 21 getUiDevice().pressHome(); 22 23 // We’re now in the home screen. Next, we want to simulate 24 25 // a user bringing up the All Apps screen. 26 27 // If you use the uiautomatorviewer tool to capture a snapshot 28 29 // of the Home screen, notice that the All Apps button’s 30 31 // content-description property has the value “Apps”. We can 32 33 // use this property to create a UiSelector to find the button. 34 35 UiObject allAppsButton = new UiObject(new UiSelector() 36 37 .description("Apps")); 38 39 // Simulate a click to bring up the All Apps screen. 40 41 allAppsButton.clickAndWaitForNewWindow(); 42 43 // In the All Apps screen, the Settings app is located in 44 45 // the Apps tab. To simulate the user bringing up the Apps tab, 46 47 // we create a UiSelector to find a tab with the text 48 49 // label “Apps”. 50 51 UiObject appsTab = new UiObject(new UiSelector() 52 53 .text("Apps")); 54 55 // Simulate a click to enter the Apps tab. 56 57 appsTab.click(); 58 59 // Next, in the apps tabs, we can simulate a user swiping until 60 61 // they come to the Settings app icon. Since the container view 62 63 // is scrollable, we can use a UiScrollable object. 64 65 UiScrollable appViews = new UiScrollable(new UiSelector() 66 67 .scrollable(true)); 68 69 // Set the swiping mode to horizontal (the default is vertical) 70 71 appViews.setAsHorizontalList(); 72 73 // Create a UiSelector to find the Settings app and simulate 74 75 // a user click to launch the app. 76 77 UiObject settingsApp = appViews.getChildByText(new UiSelector() 78 79 .className(android.widget.TextView.class.getName()), 80 81 "Settings"); 82 83 settingsApp.clickAndWaitForNewWindow(); 84 85 // Validate that the package name is the expected one 86 87 UiObject settingsValidation = new UiObject(new UiSelector() 88 89 .packageName("com.android.settings")); 90 91 assertTrue("Unable to detect Settings", 92 93 settingsValidation.exists()); 94 95 UiObject reportBug = new UiObject(new UiSelector().text("Sound")); 96 97 reportBug.clickAndWaitForNewWindow(); 98 99 UiObject soundValidation = new UiObject(new UiSelector()100 101 .text("Volumes"));102 103 assertTrue("Unable to detect Sound",104 105 soundValidation.exists()); 106 107 getUiDevice().pressHome();108 109 } 110 111 }
4、之後編譯和發布uiautomator測試:
建立一個編譯檔案:
C:\Users\58>android create uitest-project -n autoLogin -t 15 -p E:\Debin2\autoLogin
Added file E:\Debin2\autoLogin\build.xml
以上是建立成功了,叫做build.xml
這個命令是:
Android create uitest-project -n xxx -t xxx -p xxx
其中-n 後面的xxx代表要project的name
-t 的15是這樣擷取的:通過android list,能夠得到一個列表,因為我用的android.jar和uiautomator.jar是19的,所以尋找到對應的是:
所以我的-t 就寫成了15
然後就是-p 後面的參數,是工程所在的位置,因為這個build.xml的組建目錄就是這個工程目錄
命令列建立提示建立成功之後,即added file xxx這句話出現之後,在工程目錄下能夠看到這個檔案,通過eclipse目錄重新整理也可以看到:(多了三個檔案)
之後如果eclipse中直接有ant的這個打包設定,就可以通過在build.xml上右鍵Run As選擇ant build,然而我的eclipse中沒有配置成功,但是沒關係,只要在命令列裡面能運行成功就可以。
build.xml的檔案開啟後,能看到default後面本來是"help"的,改成build
然後我的機器中沒有安裝ant,去官網下載ant的檔案,之後安裝成功,需要進行環境變數的配置,主要包含三個地方:
ANT_HOME D:\Program Files\apache-ant-1.9.7-bin\apache-ant-1.9.7
path D:\Program Files\apache-ant-1.9.7-bin\apache-ant-1.9.7\bin
classpath D:\Program Files\apache-ant-1.9.7-bin\apache-ant-1.9.7\lib
然後在cmd下直接運行ant 和ant -version,如果提示正確,不是出現ant不是可用的命令這句話,就說明環境變數配置成功了
接下來是比較容易出現問題的地方:
進入到工程目錄下,
命令列輸入:ant + build.xml
應該是沒有後面的尾碼名,直接ant build
然後能看到已經build成功了,並且在工程目錄的bin檔案夾下,檔案名稱叫做:autoLogin.jar
5、接下來就是將jar包push到手機中,然後運行程式了
push進入的手機的目錄是/data/local/tmp,
執行case的命令是:adb shelle uiautomator runtest autoLogin.jar -c com.ganji.loginhelper.Runner
runtest後面是之前push進去的jar包,-c後面是要執行的class(class前面要加包名的,即xx.xx.package.classname)
然後就會有跑case之後的資訊打出來,完成,後面就是怎麼寫case實現自己想要的測試效果啦~~~
uiautomator跑安卓端UI testing