UiAutomator UIAutomator2.0 - - - - UiObject 简介

49875183 · 2015年11月12日 · 最后由 木月 回复于 2015年11月26日 · 2820 次阅读

如有遗漏或偏差请拍,请轻拍。

UiObject2 类的新特性:

1,boolean equals(Object object)
判断 2 个 uiobject2 是否一致

2,int hashCode()
获取 uiobject2 的 hashcode
可以是明确看出只有 UiObject2.hashcode() 得到的 hashcode 是同一个 code

UiObject2 obj1 = device.findObject(By.text("TesterHome"));
UiObject2 obj2 = device.findObject(By.text("TesterHome"));
System.out.println(obj1.hashCode());
System.out.println(obj2.hashCode());
UiObject obj3 = device.findObject(new UiSelector().text("TesterHome"));
UiObject obj4 = device.findObject(new UiSelector().text("TesterHome"));
System.out.println(obj3.hashCode());
System.out.println(obj4.hashCode());

3,UiObject2 getParent()
获取父元素

4,List getChildren()
获取子元素集合

5,UiObject2 findObject(BySelector selector)
通过查找获取 uiobject2 元素

6,List findObjects(BySelector selector)
通过查找获取 uiobject2 元素集合

7,String getApplicationPackage()
获取 package 属性

8, String getResourceName()
获取 ResourceName 属性

9,void clear()
清除元素文本

10,clickAndWait(EventCondition condition, long timeout)
点击并等待某个事件

11,swipe(Direction direction, float percent)
通过方向拖动元素

Direction.RIGHT
Direction.LEFT
Direction.DOWN
Direction.UP

12,boolean scroll(Direction direction, float percent)
通过方向滑动元素

13,boolean fling(Direction direction)
通过方向飞划元素

14,wait(UiObject2Condition condition, long timeout)
待等条件满足

15,wait(SearchCondition condition, long timeout)
等待条件满足

UiObject 基本特性、方法:

1,boolean dragTo(UiObject destObj, int steps)
拖动元素到指定 destObj,Steps 通常为 40
获取 destObj 的 bounds 信息,然后拖动元素到 destOjb 位置,执行 swipe 操作

2,boolean dragTo(int destX, int destY, int steps)
拖动元素到指定 destX,destY 坐标位置,Steps 通常为 40
扡动元素到 destX,destY 坐标点,执行的 swipe 操作

3,boolean swipeUp(int step)
向上滑动元素

4,boolean swipeDown(int step)
向下滑动元素

5,boolean swipeLeft(int step)
向左滑动元素

6,boolean swipeRight(int step)
向右滑动元素

*7,boolean click() *
模拟点击元素

8,boolean clickAndWaitForNewWindow()
点击元素,等待窗口出现
默认设置超时时长为 5500,调用 clickAndWaitForNewWindow(long timeout) 方法。

9,boolean clickAndWaitForNewWindow(long timeout)
点击元素,在自定义时间内等待窗口出现
clickAndWaitForNewWindow()、clickAndWaitForNewWindow(long timeout) 可以当成一个方法,唯一不同的就是第一个方法有默认时长 。

10,boolean clickTopLeft()
点击元素左上角

11,boolean longClickBottomRight()
点击元素右下角

12,boolean clickBottomRight()
点击元素右上角

13,boolean longClick()
长按元素

14,boolean longClickTopLeft()
长按元素左上角

15,UiObject getChild(UiSelector selector)
获取子元素生成一个新的 UIobject 对象

16,int getChildCount()
获取子元素数量

17,UiObject getFromParent(UiSelector selector)
从父元素集合中创建一个新的元素对象

18,String getText()
获取元素 text 属性

19,String getClassName()
获取元素 classname 属性

20,String getContentDescription()
获取元素 content_desc 属性

21,boolean setText(String text)
设置元素文本
先对原有文本进行了清除操作 clearTextField(),然后再写入新文本。
在对元素赋值时,不需要先执行 cleartextfield 操作,再执行 setText 操作。

22,void clearTextField()
清除元素文本

23,boolean isSelected()
获取元素 selected 属性,判断是否被选中

24,boolean isChecked()
获取元素 checked 属性,判断是否被勾选

25,boolean isCheckable()
获取元素 checkable 属性,判断是否允许被勾选

26,boolean isEnabled()
获取元素 enabled 属性,判断是否被启用

27,boolean isClickable()
获取元素 clickable 属性,判断是否允许被点击

28,isFocused()
获取元素 focused 属性,是否得到焦点

29,boolean isFocusable()
获取元素 focusable 属性,是否允许得到焦点

30,boolean isScrollable()
获取元素 scrollable 属性,判断是否可滚动

31,boolean isLongClickable()
获取元素 longclickable 属性,长点击是否有效

33,String getPackageName()
获取元素包名属性

34,Rect getVisibleBounds()
获取元素可见视力的范围坐标

35,Rect getBounds()
获取元素在屏幕中的位置坐标

36,boolean waitForExists(long timeout)
等待元素出现

37,boolean waitUntilGone(long timeout)
等待元素是消失

38,boolean exists()
检查元素是否存在,其实凋用的就是 waitForExists(long timeout) 方法, 将 timeout 设置成了 0

39,boolean pinchOut(int percent, int steps)
由中间同时向左上角和 右下角做放大动作

39,boolean pinchIn(int percent, int steps)
由左上角和右下角同时向中间做缩小动作。

40,boolean performTwoPointerGesture(Point startPoint1, Point startPoint2, Point endPoint1,Point endPoint2, int steps)
模拟两个手指执行滑动事件,是组装了二个 PointerCoords,调用了 performMultiPointerGesture 多点触控方法:

        PointerCoords[] points1 = new PointerCoords[steps + 2];
        PointerCoords[] points2 = new PointerCoords[steps + 2];
        for (int i = 0; i < steps + 1; i++) {
            PointerCoords p1 = new PointerCoords();
            p1.x = eventX1;
            p1.y = eventY1;
            p1.pressure = 1;
            p1.size = 1;
points1[i] = p1;
PointerCoords p2 = new PointerCoords();
            p2.x = eventX2;
            p2.y = eventY2;
            p2.pressure = 1;
            p2.size = 1;
            points2[i] = p2; 
            eventX1 += stepX1;
            eventY1 += stepY1;
            eventX2 += stepX2;
            eventY2 += stepY2;
        }

        // ending pointers coordinates
        PointerCoords p1 = new PointerCoords();
        p1.x = endPoint1.x;
        p1.y = endPoint1.y;
        p1.pressure = 1;
        p1.size = 1;
        points1[steps + 1] = p1; 
        PointerCoords p2 = new PointerCoords();
        p2.x = endPoint2.x;
        p2.y = endPoint2.y;
        p2.pressure = 1;
        p2.size = 1;
        points2[steps + 1] = p2; 
        return performMultiPointerGesture(points1, points2);

41,boolean performMultiPointerGesture(PointerCoords[] ...touches)
模拟多点触控手势,最少需要二次指针位置

package com.example.xushizhao.uiautomatortest;

import android.app.Instrumentation;
import android.content.SyncAdapterType;
import android.graphics.Point;
import android.graphics.Rect;
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.InstrumentationUiAutomatorBridge;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;

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

/**
 * Created by xushizhao on 15/11/10.
 */
@RunWith(AndroidJUnit4.class)
public class UiDeviceTest {

    Instrumentation instrumentation;
    UiDevice device;


    @Before
    public void setUp(){

        instrumentation = InstrumentationRegistry.getInstrumentation();
        device = UiDevice.getInstance(instrumentation);


    }

    @Test
    public void testUiObject() throws UiObjectNotFoundException {

        UiObject obj1;
        UiObject obj2;

        obj1 = device.findObject(new UiSelector().text("TesterHome"));
        obj2 = device.findObject(new UiSelector().text("设置"));
        //拖动元素到指定destObj
        obj1.dragTo(obj2,40);


        obj2 = device.findObject(new UiSelector().resourceId("com.android.launcher3:id/preview_background"));
        obj2.click();
        obj1 = device.findObject(new UiSelector().text("TesterHome"));
        //拖动元素到指定destX,destY坐标位置
        obj1.dragTo(323, 519, 40);


        //休眠3
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //进入TesterHome应用
        UiObject2 object2 = device.findObject(By.text("TesterHome"));
        //元素点击事件
        object2.click();
        //点击最热模块
        obj1 = device.findObject(new UiSelector().text("最热"));
        obj1.click();

        obj1 = device.findObject(new UiSelector().resourceId("com.testerhome.nativeandroid:id/vp_topics"));
        //判断元素是否可以滑动
        if(obj1.isScrollable()){
            //向上滑动元素
            obj1.swipeUp(100);
            //向下滑动元素
            obj1.swipeDown(100);
            //向左滑动元素
            obj1.swipeLeft(100);
            //向右滑动元素
            obj1.swipeRight(100);
        }

        obj1 = device.findObject(new UiSelector().text("话题"));
        //点击元素,等待窗口出现
        obj1.clickAndWaitForNewWindow();

        device.pressHome();

        obj1 = device.findObject(new UiSelector().text("TesterHome"));
        //点击元素左上角
        obj1.clickTopLeft();
        device.pressHome();
        //点击元素右下角
        obj1.longClickBottomRight();
        device.pressHome();
        //点击元素右上角
        obj1.clickBottomRight();
        device.pressHome();
        //长按元素
        obj1.longClick();
        device.pressHome();
        //长按元素左上角
        obj1.longClickTopLeft();
        device.pressBack();
        device.pressBack();

        obj1.click();


        obj1 = device.findObject(new UiSelector().resourceId("com.testerhome.nativeandroid:id/rl_topic_item").index(1));
        //获取所有子元素数量
        obj1.getChildCount();
        //获取子元素生成一个新的UIobject对象
        obj2 = obj1.getChild(new UiSelector().resourceId("com.testerhome.nativeandroid:id/tv_topic_title"));

        //获取元素text属性
        obj2.getText();
        //获取classname属性
        obj2.getClassName();
        //获取content_desc属性
        obj2.getContentDescription();
        //获取元素包名属性
        obj2.getPackageName();
        //获取selected属性
        obj2.isSelected();
        //获取checked属性
        obj2.isChecked();
        //获取元素checkable属性
        obj2.isCheckable();
        //获取enalbed属性
        obj2.isEnabled();
        //获取checkable属性
        obj2.isCheckable();

        //获取元素可见视力的范围坐标
        Rect rect =obj2.getVisibleBounds();
        System.out.println("x:" + rect.centerX() + "  y:" + rect.centerY());
        //获取元素的位置坐标
        Rect rect1 = obj2.getBounds();
        System.out.println("x:" + rect1.centerX() + "  y:" + rect1.centerY());


        obj1 = device.findObject(new UiSelector().text("图库"));
        obj1.click();
        int height = device.getDisplayHeight()/2;
        int width = device.getDisplayWidth()/2;
        //点击屏幕中间位置,进入图册
        device.click(width,height);

        //休眠3
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //再次点击,进入图片
        device.click(width,height);

        obj1 = device.findObject(new UiSelector().resourceId("com.android.gallery3d:id/photopage_bottom_controls"));

        obj1.pinchOut(200,50);
        //休眠3
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        obj1.pinchIn(200,50);
        //
        obj1.performTwoPointerGesture(new Point(width-50,height-50),new Point(width+50,height+50),new Point(width-150,height-150),new Point(width+150,height+150),40);



    }

    @Test
    public void testUiObject2() throws UiObjectNotFoundException {

        UiObject2 obj1 = device.findObject(By.text("TesterHome"));
        UiObject2 obj2 = device.findObject(By.text("TesterHome"));
        //判断2obj是否一致
        System.out.println( obj1.equals(obj2));

        //UiObject2uiobject,hashcode()的区别
        System.out.println(obj1.hashCode());
        System.out.println(obj2.hashCode());

        UiObject obj3 = device.findObject(new UiSelector().text("TesterHome"));
        UiObject obj4 = device.findObject(new UiSelector().text("TesterHome"));
        System.out.println(obj3.hashCode());
        System.out.println(obj4.hashCode());
        //获取package属性
        System.out.println(obj1.getApplicationPackage());
        //获取ResourceName属性
        System.out.println(obj1.getResourceName());

        UiObject2 c = device.findObject(By.text("获取的元素的text"));
        c.swipe(Direction.UP,100);
        c.fling(Direction.UP);
        c.scroll(Direction.UP,100);



    }

}

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 10 条回复 时间 点赞

顶贴~

这是传说中的全代码流吗。。。不是代码的部分可以不用代码块的。

#2 楼 @chenhengjie123 刷一遍 2.0,帮扫扫还有啥遗漏的不。。。

@xushizhao 还少了
wait(UiObject2Condition condition, long timeout)
wait(SearchCondition condition, long timeout)

@xushizhao BySelector 的 api 才重要。

#5 楼 @lihuazhang 收到~ 我看 By 全部封装的 BySelector,我马上扣过去了。。。

#4 楼 @zsx10110 我一会补进去~~谢谢!

问个问题,uiautomaot 2.0 只能通过 gradle 的方式添加依赖库了吗,不能通过之前 add jar 的方式了吗

@lihuazhang 嗯,了解了,官网上一直介绍的 gradle 配置 sdk 下载的方法,还以为不支持

11楼 已删除
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册