UiAutomator之UiCollection UiScrollable UiWatcher

直接吧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() //纵向


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

package cn.vlang.test01;

import android.R.integer;
import android.os.RemoteException;

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class Uidevice extends UiAutomatorTestCase{
	public void testDemo() throws UiObjectNotFoundException, RemoteException{
		
		getUiDevice().pressMenu();
		getUiDevice().pressBack();
		
		UiScrollable listScrollable = new UiScrollable(new UiSelector().scrollable

(true));
		for(int i=0; i<10; i++)
		{
		listScrollable.flingToEnd(5);
		}
		
		for(int j=0; j<10; j++)
		{
		listScrollable.flingToBeginning(5);
		}
	}


}

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


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

public String[] isUiChange() throws UiObjectNotFoundException
{
	UiCollection framCollection = new UiCollection(new UiSelector().className

("android.widget.FrameLayout"));
	int count = 0;
	String[] ts = {"a", "a", "a", "a", "a"};
	if(framCollection.exists());
	{
		int textcount = framCollection.getChildCount(new UiSelector().className

("android.widget.TextView"));
		int linearcount = framCollection.getChildCount(new UiSelector().className

("android.widget.LinearLayout"));
		int viewcount = framCollection.getChildCount(new UiSelector().className

("android.widget.View"));
		count = textcount + linearcount + viewcount;
	}
	for(int j=0; j<5; j++)
	{
		UiObject textObject = new UiObject(new UiSelector().className

("android.widget.TextView").instance(j));
		if(textObject.exists())
		{
			ts[j] = textObject.getText();
		}
		else
		{
			break;
		}
	}

	String[] change = {String.valueOf(count), ts[0],ts[1],ts[2],ts[3],ts[4]};
	return change;
}


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("闹钟监听器运行过了");}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值