现在的位置: 首页 > 综合 > 正文

android uiautomator学习(二)主要类的功能简单介绍

2018年05月26日 ⁄ 综合 ⁄ 共 5620字 ⁄ 字号 评论关闭

各个类的主要作用如下:

1.可用UiDevice表示设备
getUiDevice().pressHome();

2.UiSelector表示一个搜索条件以获取一个特定的组件。
UiObject appItem new UiObject(new UiSelector()
.className("android.widget.ListView").instance(1)
.childSelector(new UiSelector().text("Apps")));

UiObject 一个UI组件,一般和UiSelector连用

3.UiCollection一系列的UI组件
UiCollection videos new UiCollection(new UiSelector()
.className("android.widget.FrameLayout"));
int count videos.getChildCount(new UiSelector()
.className("android.widget.LinearLayout"));

4.UiScrollable代表一个可能需要滑动先用视图才会被用户看见的组件。
UiScrollable settingsItem new UiScrollable(new UiSelector()
.className("android.widget.ListView"));

UiObject about settingsItem.getChildByText(new UiSelector()
.className("android.widget.LinearLayout"), "About tablet");
about.click()

API分析:

UiScrollable
1 快速滚动
2 获取列表子元素
3 获取与设置最大滚动次数常量值
4 滑动区域校准常量设置与获取
5 向前与向后滚动
6 滚动到某个对象
7 设置滚动方向
继承关系
UiObject
UiCollection
UiScrollable
基本格式
UiScrollable 对象名 = new UiScrollable(new UiSelector().选择对象条件);;
对象名.操作函数

exists()
1 快速滚动
flingBackward() //向后滑动
flingForward() //向前滑动
flingToBeginning() //快速滑动到开始
flingToEnd() //快速滑动到结尾

2 获取列表子元素
getChildByDescription()
getChildByInstance()
getChildByText()

3 获取与设置最大滚动次数常量值
getMaxSearchSwipes()
setMaxSearchSwipes()

4 滑动区域校准常量设置与获取
getSwipeDeadZonePercentage()
setSwipeDeadZonePercentage()

5 向前与向后滚动
scrollBackward() //向后滚动
scrollDescriptionIntoView() //滚动到描述位置
scrollForward() //向前滚动

6 滚动到某个对象
scrollIntoView()
scrollTextIntoView()
scrollToBeginning()
scrollToEnd()

7 设置滚动方向
setAsHorizontalList() //水平
setAsVerticalList() //纵向

测试用例
游视榜的滑动用例

[java] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. package cn.vlang.test01;  
  2.   
  3. import android.R.integer;  
  4. import android.os.RemoteException;  
  5.   
  6. import com.android.uiautomator.core.UiObject;  
  7. import com.android.uiautomator.core.UiObjectNotFoundException;  
  8. import com.android.uiautomator.core.UiScrollable;  
  9. import com.android.uiautomator.core.UiSelector;  
  10. import com.android.uiautomator.testrunner.UiAutomatorTestCase;  
  11.   
  12. public class Uidevice extends UiAutomatorTestCase{  
  13.     public void testDemo() throws UiObjectNotFoundException, RemoteException{  
  14.           
  15.         getUiDevice().pressMenu();  
  16.         getUiDevice().pressBack();  
  17.           
  18.         UiScrollable listScrollable = new UiScrollable(new UiSelector().scrollable  
  19.   
  20. (true));  
  21.         for(int i=0; i<10; i++)  
  22.         {  
  23.         listScrollable.flingToEnd(5);  
  24.         }  
  25.           
  26.         for(int j=0; j<10; j++)  
  27.         {  
  28.         listScrollable.flingToBeginning(5);  
  29.         }  
  30.     }  
  31.   
  32.   
  33. }  


UiCollection
继承自UiObject 用于计算一个容器的用户界面元素个数 获取子元素
getChildByDescription() //寻找符合条件子元素
getChildByInstance()
getChildByText()
getChildCount() //递归计算符合条件子元素数量

需求分析
获取界面元素类的个数与5个textview的字符串 返回一个字符串数组 用于判断界面是否变化

[java] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. public String[] isUiChange() throws UiObjectNotFoundException  
  2. {  
  3.     UiCollection framCollection = new UiCollection(new UiSelector().className  
  4.   
  5. ("android.widget.FrameLayout"));  
  6.     int count = 0;  
  7.     String[] ts = {"a""a""a""a""a"};  
  8.     if(framCollection.exists());  
  9.     {  
  10.         int textcount = framCollection.getChildCount(new UiSelector().className  
  11.   
  12. ("android.widget.TextView"));  
  13.         int linearcount = framCollection.getChildCount(new UiSelector().className  
  14.   
  15. ("android.widget.LinearLayout"));  
  16.         int viewcount = framCollection.getChildCount(new UiSelector().className  
  17.   
  18. ("android.widget.View"));  
  19.         count = textcount + linearcount + viewcount;  
  20.     }  
  21.     for(int j=0; j<5; j++)  
  22.     {  
  23.         UiObject textObject = new UiObject(new UiSelector().className  
  24.   
  25. ("android.widget.TextView").instance(j));  
  26.         if(textObject.exists())  
  27.         {  
  28.             ts[j] = textObject.getText();  
  29.         }  
  30.         else  
  31.         {  
  32.             break;  
  33.         }  
  34.     }  
  35.   
  36.     String[] change = {String.valueOf(count), ts[0],ts[1],ts[2],ts[3],ts[4]};  
  37.     return change;  
  38. }  



UiWatcher
checkForCondition() //中断监听检查
UiSelector条件无法匹配对象
调用所有已经运行的监听器

UiDevice 操作设备
UiSelector 选择条件
UiWatcher 监听器
UiObject 操作对象
UiCollection 对象集合
UiScrollable 滚动对象

一个程序例子
打开文件管家—进入内置存储卡—滚动到Pictures文件夹—点击进入—HOME键
 launchApp("com.lenovo.FileBrowser", "com.lenovo.FileBrowser.activities.FileBrowserMain");
//声明内置存储卡文本对象
 UiObject build_in=new UiObject(new UiSelector().text("内置存储卡"));
 build_in.click();//点击内置存储卡
 UiScrollable list=new UiScrollable(new          
  UiSelector().scrollable(true));//根据滚动属性条件声明列表对象
 //使用正则匹配条件声明文件夹名称对象
 UiObject pictures=new UiObject(new UiSelector().textMatches("Pictures\\s\\(\\d+\\)"));
 list.scrollIntoView(pictures);//滚动到对象
 pictures.clickAndWaitForNewWindow();//点击对象等待新窗口
 UiDevice.getInstance().pressHome();//按Home键回到桌面

再来个程序例子
UiDevice.getInstance().registerWatcher("answerThePhone",
new UiWatcher() {
UiObject jietingObject = new UiObject(new UiSelector()
.text("下拉接听"));
@Override
public boolean checkForCondition() {
// TODO Auto-generated method stub
System.out.println("监听器检查函数开始运行-挂电话");
if (jietingObject.exists()) {
System.out.println("监听器条件判断成功--挂电话");
int y = UiDevice.getInstance().getDisplayHeight();
int x = UiDevice.getInstance().getDisplayWidth();
UiDevice.getInstance().swipe(x / 2, y / 2, x / 2,
10, 10);
return true;}
System.out.println("监听器条件判断失败--挂电话");
return false;}});

测试代码示例
取消监听与运行所有监听
this.watcherAlarmClock();// 闹钟监听
this.watcherAnswerThePhone();// 电话监听
this.watcherMms();// 短信监听

// 取消部分监听器
UiDevice.getInstance().removeWatcher("mms");
UiDevice.getInstance().removeWatcher("alarm");

// 运行所有的监听
// UiDevice.getInstance().runWatchers();

重置监听与检查监听运行
// 重置已经触发过的监听
// UiDevice.getInstance().resetWatcherTriggers();
// 检查监听器是否被运行过
boolean phone = UiDevice.getInstance().hasWatcherTriggered(
"answerThePhone");
boolean mms = UiDevice.getInstance().hasWatcherTriggered("mms");
boolean alarm = UiDevice.getInstance().hasWatcherTriggered("alarm");
if (phone == true) {
System.out.println("电话监听器运行过了");}
if (mms == true) {
System.out.println("短信听器运行过了");}
if (alarm == true) {
System.out.println("闹钟监听器运行过了");}

抱歉!评论已关闭.