1.如下是一個簡單的測試案例代碼,模擬了點擊Home鍵回到主屏,然后點擊所以應用按鈕打開所有應用列表,並滾動到時鍾應用。打開時鍾應用 並選擇鬧鈴界面的第一個鬧鍾設置,修改該設置的開關。然后返回到時鍾界面再進入倒計時界面。
1 package com.uiauto.test; 2 3 import android.widget.ListView; 4 5 import android.widget.Switch; 6 7 import com.android.uiautomator.core.UiObject; 8 9 import com.android.uiautomator.core.UiObjectNotFoundException; 10 11 import com.android.uiautomator.core.UiScrollable; 12 13 import com.android.uiautomator.core.UiSelector; 14 15 import com.android.uiautomator.testrunner.UiAutomatorTestCase; 16 17 18 public class LaunchSettings extends UiAutomatorTestCase { 19 // TODO 重要注意: 在運行該測試代碼的時候 需要先把手機語言環境設置為英文。 20 21 public void testDemo() throws UiObjectNotFoundException { 22 23 // 模擬 HOME 鍵點擊事件 24 getUiDevice().pressHome(); 25 26 // 現在打開了主屏應用,模擬點擊所有應用按鈕操作來啟動所有應用界面。 27 // 如果你使用了uiautomatorviewer來查看主屏,則可以發現“所有應用”按鈕的 28 // content-description 屬性為“Apps”。可以使用該屬性來找到該按鈕。 29 UiObject allAppsButton = new UiObject(new UiSelector().description("Apps")); 30 31 // 模擬點擊所有應用按鈕,並等待所有應用界面起來 32 allAppsButton.clickAndWaitForNewWindow(); 33 34 // 在所有應用界面,時鍾應用位於Apps tab界面中。下面模擬用戶點擊Apps tab操作。 35 // 找到 Apps tab 按鈕 36 UiObject appsTab = new UiObject(new UiSelector().text("Apps")); 37 38 // 模擬點擊 Apps tab. 39 appsTab.click(); 40 41 // 然后在 Apps tab界面,模擬用戶滑動到時鍾應用的操作。 42 // 由於Apps界面是可以滾動的,所有用 43 // UiScrollable 對象. 44 UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true)); 45 46 // 設置滾動模式為水平滾動(默認為垂直滾動) 47 appViews.setAsHorizontalList(); 48 if(allAppsButton.exists() && allAppsButton.isEnabled()) { 49 50 // allAppsButton在當前界面已經不可見了 所以這里不會執行 51 allAppsButton.click(); 52 } 53 54 // 查找時鍾應用並點擊 55 UiObject settingsApp = appViews.getChildByText(new UiSelector().className( 56 android.widget.TextView.class.getName()),"Clock"); 57 settingsApp.clickAndWaitForNewWindow(); 58 59 // 驗證當前顯示 的應用包名為時鍾 60 UiObject settingsValidation = new UiObject(new UiSelector().packageName("com.android.deskclock")); 61 62 // 如果不存在則出錯提示 63 assertTrue("Unable to detect Clock",settingsValidation.exists()); 64 65 // 模擬點擊時間tab 66 UiObject clock = new UiObject(new UiSelector().description("Clock")); 67 clock.clickAndWaitForNewWindow(); 68 69 // 模擬點擊下方的鬧鍾圖標 70 71 UiObject alarms = new UiObject(new UiSelector().description("Alarms")); 72 alarms.clickAndWaitForNewWindow(); 73 UiScrollable list = new UiScrollable(new UiSelector().className(ListView.class.getName())); 74 if(list.getChildCount() > 0) 75 { 76 UiObject listIndex0 = list.getChild(new UiSelector().index(0)); 77 UiObject switchBtn = listIndex0.getChild(new UiSelector().className(Switch.class.getName())); 78 boolean isChecked = switchBtn.isChecked(); 79 switchBtn.click(); 80 } 81 // 模擬點擊返回鍵 82 getUiDevice().pressBack(); 83 UiObject timer = new UiObject(new UiSelector().description("Timer")); 84 timer.clickAndWaitForNewWindow(); 85 } 86 }
2.一個包含所有類使用的實例。
1 package com.example.testapp.test; 2 3 import android.app.LauncherActivity; 4 import com.android.uiautomator.core.*; 5 import com.android.uiautomator.testrunner.UiAutomatorTestCase; 6 import junit.framework.Assert; 7 import java.io.*; 8 9 /** 10 * Created by zile on 15/5/23 下午5:08. 11 */ 12 public class TestPlayMusic extends UiAutomatorTestCase { 13 14 15 @Override 16 protected void setUp() throws Exception { 17 super.setUp(); 18 } 19 20 @Override 21 protected void tearDown() throws Exception { 22 super.tearDown(); 23 } 24 25 public void testPlayMusic() throws Exception { 26 27 ///////////////////////////////////////////////////////////////////////////////////// 28 /////////// 29 System.out.println("====== 演示1:判斷手機是否處於喚醒狀態"); 30 31 UiDevice device = getUiDevice(); 32 33 //喚醒手機 34 device.wakeUp(); 35 36 assertTrue("手機處於喚醒狀態", device.isScreenOn()); 37 38 /////////////////////////////////////////////////////////////////////////////////////// 39 ///////// 40 System.out.println("====== 演示2:初始化手機和應用"); 41 42 //清空被測應用緩存 43 execCommand("adb shell pm clear com.tencent.qqmusic"); 44 45 //回到桌面 46 device.pressHome(); 47 48 //////////////////////////////////////////////////////////////////////////////////////// 49 //////// 50 System.out.println("====== 演示3:啟動應用,排除一切干擾進入首頁"); 51 52 //啟動應用 53 execCommand("adb shell am start -n com.tencent.qqmusic/com.tencent.qqmusic.activity.AppStarterActivity"); 54 55 sleep(3000); 56 57 MainWatch mainWatch = new MainWatch(); 58 device.registerWatcher("進入首頁異常", mainWatch); 59 60 // //如果顯示啟動頁,啟動頁為webview 61 // if (new UiObject(new UiSelector().resourceId("com.tencent.qqmusic:id/webView")).exists()){ 62 // 63 // //點擊坐標/ 64 // device.click(241, 548); 65 // } 66 67 sleep(5000); 68 69 // //如果提示升級, 去掉升級提醒 70 // if (new UiObject(new UiSelector().text("QQ音樂全新升級")).exists()){ 71 // new UiObject(new UiSelector().resourceId("com.tencent.qqmusic:id/upgrade_close")).click(); 72 // } 73 74 ////////////////////////////////////////////////////////////////////////////////////// 75 ////////// 76 System.out.println("====== 演示4: 獲取一些關鍵信息"); 77 78 //獲取包名 79 System.out.println(">> package name: " + device.getCurrentPackageName()); 80 81 //獲取寬高 82 System.out.println(">> width: " + device.getDisplayWidth()); 83 System.out.println(">> hight: " + device.getDisplayHeight()); 84 85 //獲取手機品牌名稱 86 System.out.println(device.getProductName()); 87 88 ///////////////////////////////////////////////////////////////////////////////////// 89 /////////// 90 System.out.println("====== 演示5: 關於設備的一些操作"); 91 92 //打開通知欄(從andrid4.3開始支持) 93 System.out.println(">> 打開通知欄"); 94 device.openNotification(); 95 96 Thread.sleep(5000); 97 98 //打開快速設置 (從andrid4.3開始支持) 99 System.out.println(">> 打開快速設置"); 100 device.openQuickSettings(); 101 102 //截圖! 103 File f = new File("data/local/tmp/screenshot.png"); 104 device.takeScreenshot(f); 105 106 //將圖片pull到PC! 107 execCommand("adb pull data/local/tmp/screenshot.png /Volumes/warehouse/workspace/uiautomator-demo2/test_music.png"); 108 109 //在設備上托動 110 111 System.out.println(">> 收起快速設置"); 112 device.drag(223, 721, 223, 42, 1); 113 114 sleep(3000); 115 116 ///////////////////////////////////////////////////////////////////////////////// 117 /////////////// 118 System.out.println("====== 演示6: 通過resouceId和text查找控件"); 119 120 //通過resourceId查找控件 121 System.out.println(">> 點擊 音樂館"); 122 UiSelector myMusic = new UiSelector().resourceId("com.tencent.qqmusic:id/main_desk_title_tab_musichall"); 123 new UiObject(myMusic).clickAndWaitForNewWindow(); 124 125 device.removeWatcher("進入首頁異常"); 126 127 128 //通過text查找控件 129 System.out.println(">> 點擊 分類"); 130 UiSelector radio = new UiSelector().text("分類"); 131 new UiObject(radio).click(); 132 133 int count = 0; 134 while (!new UiObject(new UiSelector().text("輕音樂")).exists() & count<20){ 135 sleep(1000); 136 count ++; 137 System.out.println("sleep 1 seconds"); 138 139 } 140 141 142 //////////////////////////////////////////////////////////////////////////////////// 143 //////////// 144 145 System.out.println("====== 演示7: 如何滑動界面"); 146 147 //如何使用UiScrollable 148 System.out.println(">> 滑動 直到 下午茶 顯示出來"); 149 new UiScrollable(new UiSelector().resourceId("com.tencent.qqmusic:id/common_tabs_pager")). 150 scrollIntoView(new UiSelector().text("下午茶")); 151 152 System.out.println(">> 點擊 下午茶"); 153 new UiObject(new UiSelector().textMatches("下午茶")).clickAndWaitForNewWindow(); 154 155 ///////////////////////////////////////////////////////////////////////////////////////// 156 /////// 157 System.out.println("====== 演示8: 如何使用控件集合UiCollection"); 158 159 UiSelector listView = new UiSelector().resourceId("com.tencent.qqmusic:id/common_list_musicList"); 160 161 System.out.println(">> listView下的子控件數量: " + new UiCollection(listView).getChildCount()); 162 163 System.out.println(">> 點擊 歌曲 玻璃的操作菜單"); 164 new UiCollection(listView).getChildByInstance(new UiSelector().resourceId("com.tencent.qqmusic:id/action_sheet"),0).click(); 165 166 sleep(3000); 167 168 assertTrue(new UiObject(new UiSelector().text("查看歌手")).exists()); 169 170 ///////////////////////////////////////////////////////////////////////////////////// 171 /////////// 172 System.out.println("====== 演示9: 如何使用UiSelector的childSelector方法"); 173 174 UiSelector cancel= new UiSelector().className("android.widget.RelativeLayout"). 175 childSelector(new UiSelector().text("取消")); 176 177 System.out.println(">> 點擊取消"); 178 new UiObject(cancel).click(); 179 180 System.out.println(">> 再次點擊 歌曲 玻璃的操作菜單");//注意listView的復用 181 new UiCollection(listView).getChildByInstance(new UiSelector().resourceId("com.tencent.qqmusic:id/action_sheet"),0).click(); 182 183 //////////////////////////////////////////////////////////////////////////////////////////////// 184 System.out.println("====== 演示10: 如何使用類名一級一級的來查找控件"); 185 186 UiSelector text = new UiSelector().resourceId("com.tencent.qqmusic:id/popMenuGridView"). 187 childSelector(new UiSelector().className("android.widget.RelativeLayout").index(4)). 188 childSelector(new UiSelector().className("android.widget.TextView")); 189 190 System.out.println(new UiObject(text).getText()); 191 192 ///////////////////////////////////////////////////////////////////////////////// 193 /////////////// 194 System.out.println("====== 演示11: 演示index和instance的區別"); 195 196 UiSelector relativelayout = new UiSelector().resourceId("com.tencent.qqmusic:id/popMenuGridView"). 197 childSelector(new UiSelector().className("android.widget.RelativeLayout").index(4)); 198 199 200 System.out.println(new UiObject(relativelayout).getChildCount()); 201 202 System.out.println(new UiObject(relativelayout).getChild(new UiSelector().className("android.widget.TextView").index(1)).getText()); 203 204 System.out.println(new UiObject(relativelayout).getChild(new UiSelector().className("android.widget.TextView").instance(0)).getText()); 205 206 } 207 208 209 class MainWatch implements UiWatcher{ 210 211 @Override 212 public boolean checkForCondition() { 213 boolean check = false; 214 System.out.println(">> 進入首頁初始化監聽"); 215 //如果顯示啟動頁,啟動頁為webview7 216 if (new UiObject(new UiSelector().resourceId("com.tencent.qqmusic:id/webView")).exists()){ 217 System.out.println(">>>>>>>>> 看來是有啟動頁顯示,點掉你"); 218 //點擊坐標 219 getUiDevice().click(241, 548); 220 sleep(2000); 221 check = true; 222 }else if (new UiObject(new UiSelector().text("QQ音樂全新升級")).exists()){ 223 System.out.println(">>>>>>>>> 看來是有升級提醒,點掉你"); 224 try 225 { 226 new UiObject(new UiSelector().resourceId("com.tencent.qqmusic:id/upgrade_close")).click(); 227 } 228 catch (UiObjectNotFoundException e) { 229 e.printStackTrace(); 230 } 231 sleep(2000); 232 check = false; 233 } 234 235 return check; 236 } 237 } 238 239 /** 240 * 執行命令 241 *@param command 242 */ 243 public void execCommand(String command){ 244 String ls_str; 245 Process ls_proc = null; 246 try 247 { 248 System.out.println(">> 執行命令: " + command); 249 ls_proc = Runtime.getRuntime().exec(command); 250 DataInputStream ls_in = new DataInputStream(ls_proc.getInputStream()); 251 BufferedReader br = new BufferedReader(new InputStreamReader(ls_in)); 252 while ((ls_str = br.readLine()) != null) { 253 System.out.println(ls_str); 254 } 255 } 256 catch (IOException e) { 257 e.printStackTrace(); 258 } 259 260 } 261 262 }
網上收集並修改。
