zoukankan      html  css  js  c++  java
  • Uiautomator 2.0之UiObject2类学习小记

    1. 基础动作

    1.1. 相关API介绍

    API 说明
    clear() 清楚编辑框内的内容
    click() 点击一个对象
    clickAndWait(EventCondition<R> condition, long timeout) 点击一个对象然后等待在超时的时间内条件满足则通过,否则抛出异常
    drag(Point dest, int speed) 自定义速度拖拽这个对象到指定位置
    drag(Point dest) 拖拽这个对象到指定位置
    longClick() 长按某个对象
    scroll(Direction direction, float percent) 对该对象执行一个滚动操作
    scroll(Direction direction, float percent, int speed) 自定义速度,对该对象执行一个滚动操作
    setText(String text) 设置文本内容
    legacySetText(String text) 通过发送keycode,设置文本内容

    1.2 简单示例

     1  @Test
     2     public void testCase05() throws InterruptedException {
     3         /**
     4          * 发现UiObject2中的setText()方法还是无法直接输入中文,需要借助外部方法库.
     5          */
     6         UiObject2 editText = mDevice.findObject(By.clazz(EditText.class));
     7         editText.setText(Utf7ImeHelper.e("测试"));
     8 
     9         UiObject2 appIcon = mDevice.findObject(By.text("联系人"));
    10         Point desPoint = new Point();
    11         desPoint.x = 654;
    12         desPoint.y = 1066;
    13         appIcon.drag(desPoint, 2000);
    14 
    15         UiObject2 appBtn = mDevice.findObject(By.text("联系人"));
    16         appBtn.longClick();
    17 
    18 
    19         UiObject2 listView = mDevice.findObject(By.res("android:id/list"));
    20         listView.scroll(Direction.DOWN, 0.8f, 3000);
    21 
    22         UiObject2 smsBtn = mDevice.findObject(By.text("短信"));
    23         smsBtn.clickAndWait(Until.newWindow(), 2000);
    24     }

    2. 手势动作模拟

    2.1 相关API

    API 说明
    pinchClose(float percent, int speed) 自定义速度执行收缩手势
    pinchClose(float percent) 执行收缩手势
    pinchOpen(float percent, int speed) 自定义速度执行展开手势
    pinchOpen(float percent) 执行展开手势
    fling(Direction direction) 执行一个扫动手势,Direction代表为起点方向
    fling(Direction direction, int speed) 自定义速度,执行一个扫动手势
    swipe(Direction direction, float percent, int speed) 执行一个滑动操作,可自定义滑动距离和速度
    swipe(Direction direction, float percent) 执行一个滑动操作
    setGestureMargin(int margin) 以像素为单位,设置手势边缘
    setGestureMargins(int left, int top, int right, int bottom) 以像素为单位,设置手势边缘

    2.2 简单示例

     1 @Test
     2     public void testCase06() throws InterruptedException {
     3         UiObject2 pic = mDevice.findObject(By.res("com.miui.gallery:id/single_view_other"));
     4         pic.pinchOpen(0.8f, 2000);
     5         Thread.sleep(1000);
     6         pic.pinchClose(0.8f, 2000);
     7 
     8         UiObject2 contactList = mDevice.findObject(By.res("android:id/list"));
     9         contactList.fling(Direction.DOWN);
    10         Thread.sleep(1000);
    11         contactList.fling(Direction.UP,3000);
    12 
    13         UiObject2 listView = mDevice.findObject(By.res("android:id/list"));
    14         listView.swipe(Direction.UP,0.5f, 3000);
    15         Thread.sleep(1500);
    16         listView.setGestureMargin(100);
    17         listView.swipe(Direction.DOWN,0.5f, 3000);
    18     }

    3. 获取层级与条件判断

    3.1相关API

    API 说明
    findObject(BySelector selector) 搜索在这个对象之下的所有元素,并返回第一个与搜索条件匹配的
    findObjects(BySelector selector) 搜索在这个对象之下的所有元素,并返回所有与搜索条件匹配的
    getChildCount() 返回这个对象直属子元素的数量
    getChildren() 返回这个对象下的直接子元素的集合
    getParent() 返回该对象的父类
    equals(Object object) 比较两个对象是否相等
    hashCode() 获取对象的哈希码
    hasObject(BySelector selector) 返回该对象是否存在
    recycle() 回收该对象
    wait(UiObject2Condition<R> condition, long timeout) 等待条件被满足
    wait(SearchCondition<R> condition, long timeout) 等待条件被满足

    3.2 简单示例

     1 @Test
     2     public void testCase07(){
     3         UiObject2 list = mDevice.findObject(By.res("android:id/list"));
     4         UiObject2 child = list.findObject(By.clazz(TextView.class));
     5         Log.i("testCase07", child.getText());
     6 
     7         List<UiObject2> lisCollect = mDevice.findObjects(By.clazz(TextView.class));
     8         int count = lisCollect.size();
     9         Log.i("testCase07", String.valueOf(count));
    10         for (UiObject2 obj:lisCollect) {
    11             Log.i("testCase07", obj.getText());
    12         }
    13 
    14         List<UiObject2> childList = list.getChildren();
    15         int childCount = childList.size();
    16         Log.i("testCase07", String.valueOf(childCount));
    17         for (UiObject2 obj:childList) {
    18             Log.i("testCase07", obj.getText());
    19         }
    20         
    21         UiObject2 childElement = mDevice.findObject(By.text("联系人"));
    22         childElement.getParent().click();
    23     }

    原创:http://blog.csdn.net/swordgirl2011/article/details/50993157

  • 相关阅读:
    B站崩溃的背后,b站高可用架构到底是怎么样的?
    批量查询注册表键值函数 RegQueryMultipleValues 应用一例
    windows服务程序的安装和卸载函数
    API 获得GetLastError()错误代码对应的文字信息
    API 在屏幕上简单显示字符串
    API 实现类似于 C# DateTime 的类
    Windows API ReportEvent 写系统日志
    .net core 新增对DOCKER后报 ERR_EMPTY_RESPONSE
    Windows docker 安装报 WSL 2 installation is incomplete.
    SSD固态硬盘装系统无法进入引导
  • 原文地址:https://www.cnblogs.com/successcai/p/6102240.html
Copyright © 2011-2022 走看看