UiObject2介绍

基础动作模拟API
返回API说明
voidclear()清除编辑框中的内容
voidclick()点击一个对象
<R> RclickAndWait(EventCondition<R> condition, long timeout)点击一个对象,然后等待在超时时间内条件成立则通过,否则抛出异常
voiddrag(Point dest, int speed)自定义速度拖拽一个对象到指定位置,速度:像素/秒
voiddrag(Point dest)拖拽一个对象到指定位置
voidlongClick()长时间点击对象
booleanscroll(Direction direction, float percent)滚动操作
booleanscroll(Direction direction, float percent, int speed)自定义速度的滚动操作
voidsetText(String text)设置文本内容
手势动作API
返回API说明
voidpinchClose(float percent, int speed)自定义速度关闭手势
voidpinchOpen(float percent, int speed)自定义速度打开手势
voidpinchOpen(float percent)打开手势
booleanfling(Direction direction)滑动手势
booleanfling(Direction direction, int speed)自定义速度滑动手势
voidswipe(Direction direction, float percent, int speed)自定义速度滑动手势
voidswipe(Direction direction, float percent)自滑动手势
组件属性API
返回API说明
StringgetApplicationPackage()返回应用包名
StringgetClassName()返回对象类名
StringgetContentDescription()返回内容描述
StringgetResourceName()返回资源id
StringgetText()返回文本
RectgetVisibleBounds()返回对象可见范围内的屏幕坐标
PointgetVisibleCenter()返回可见范围的中心
booleanisCheckable()返回Checkable属性
booleanisChecked()返回Checked属性
booleanisClickable()返回Clickable属性
booleanisEnabled()返回Enabled属性
booleanisFocusable()返回Focusable属性
booleanisFocused()返回isFocused属性
booleanisLongClickable()返回LongClickable属性
booleanisScrollable()返回Scrollable属性
booleanisSelected()返回Selected属性
层级关系API
返回API说明
UiObject2findObject(BySelector selector)在该对象层级之下,返回第一个与条件匹配的对象
List<UiObject2>findObjects(BySelector selector)在该对象层级之下,返回所有匹配的对象
List<UiObject2>getChildren()返回该对象的所有子元素的集合
UiObject2getParent()返回该对象的父元素
intgetChildCount()返回该对象的直接子元素的数量
条件判断API
返回API说明
booleanequals(Object object)比较两个对象是否相等
inthashCode()获取对象的hashCode
booleanhasObject(BySelector selector)返回对象是否存在
<R> Rwait(UiObject2Condition<R> condition, long timeout)等待的条件得到满足
<R> Rwait(SearchCondition<R> condition, long timeout)等待的条件得到满足
示例代码:
package mqqzt.myuitest5;

import android.app.Instrumentation;
import android.graphics.Point;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.Direction;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

/**
 * Created by mqqzt-yang on 2015/11/22.
 */
@RunWith(AndroidJUnit4.class)
public class UiObject2Test {
    UiDevice uiDevice;
    Instrumentation instrumentation;

    @Before
    public void setUp(){
        instrumentation = InstrumentationRegistry.getInstrumentation();
        uiDevice = UiDevice.getInstance(instrumentation);
    }


    @Test
    public void testSetText(){
        UiObject2 mmsSendObject = uiDevice.findObject(By.res("com.android.mms:id/embedded_text_editor"));
        mmsSendObject.setText("我的东西在哪里?");
        sleep(1000);
        mmsSendObject.clear();
    }

    @Test
    public void testDrag(){
        UiObject2 settingsObj = uiDevice.findObject(By.text("Settings"));
        Point p = new Point();
        p.x = 116;
        p.y = 497;
        settingsObj.drag(p, 500);
        sleep(2000);
        settingsObj.clickAndWait(Until.newWindow(), 5000);
    }

    @Test
    public void testScroll(){
        UiObject2 listViewObj = uiDevice.findObject(By.clazz(ListView.class));
        listViewObj.scroll(Direction.DOWN, 0.8f, 500);
    }


    @Test
    public void testPinch(){
        UiObject2 viewObj = uiDevice.findObject(By.res("com.android.gallery3d:id/gl_root_view"));
        viewObj.pinchClose(0.8f, 500);
        sleep(2000);
        viewObj.pinchOpen(0.8f, 500);
    }

    @Test
    public void testFling(){
        UiObject2 msg = uiDevice.findObject(By.res("com.android.contacts:id/message"));
        msg.fling(Direction.LEFT, 500);
    }

    @Test
    public void testSwipe(){
        UiObject2 msg = uiDevice.findObject(By.res("com.android.contacts:id/message"));
        msg.swipe(Direction.RIGHT, 0.8f, 500);
    }

    @Test
    public void testWifiSwitch(){
        UiObject2 wifiSwitchObj = uiDevice.findObject(By.res("com.android.settings:id/switch_widget"));
        if(!wifiSwitchObj.isChecked()){
            wifiSwitchObj.click();
        }
    }

    @Test
    public void testChild(){
        UiObject2 listViewObj = uiDevice.findObject(By.clazz(ListView.class));
        UiObject2 child = listViewObj.findObject(By.clazz(TextView.class));
        Log.i("mqqzt", child.getText());

        List<UiObject2> listChild = listViewObj.findObjects(By.clazz(TextView.class));
        Log.i("mqqzt", listChild.size() + "");
        for(UiObject2 uiObject2 : listChild){
            Log.i("mqqzt", uiObject2.getText());
        }

        List<UiObject2> getChild = listViewObj.getChildren();
        Log.i("mqqzt", getChild.size() + "");
        for(UiObject2 uiObject2 : getChild){
            Log.i("mqqzt", uiObject2.getClassName());
        }

        Log.i("mqqzt", listViewObj.getParent().getClassName());
        Log.i("mqqzt", listViewObj.getChildCount() + "");

        UiObject2 childEqual = listViewObj.findObject(By.text("Alarms"));
        Log.i("mqqzt", child.equals(childEqual) + "");

        Log.i("mqqzt", child.hasObject(By.text("Alarms")) + "");
        Log.i("mqqzt", child.hasObject(By.text("Alarms12")) + "");

        childEqual.recycle();
        childEqual = listViewObj.findObject(By.text("Alarms"));
        childEqual.click();
    }


    public void sleep(int sleep){
        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值