您的位置:首页 > 产品设计 > UI/UE

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

2014-10-30 16:16 483 查看
各个类的主要作用如下:

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
plaincopy





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的字符串 返回一个字符串数组 用于判断界面是否变化

[java] view
plaincopy





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("闹钟监听器运行过了");}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: