2个Uiautomator测试代码实例


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 }

 

网上收集并修改。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM