我是靠谱客的博主 感性咖啡,最近开发中收集的这篇文章主要介绍uiautomator 之uiautomator 2.0 UiObject2学习收藏学习收藏http://blog.csdn.net/swordgirl2011/article/details/50993157,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

学习收藏

参考链接:

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

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 简单示例

[java] view plain copy
  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 简单示例

[java] view plain copy
  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 简单示例

[java] view plain copy
  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/mqqzt/article/details/49981207


基础动作模拟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)等待的条件得到满足

示例代码:

<code class="hljs avrasm has-numbering">package mqqzt<span class="hljs-preprocessor">.myuitest</span>5<span class="hljs-comment">;</span>

import android<span class="hljs-preprocessor">.app</span><span class="hljs-preprocessor">.Instrumentation</span><span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.graphics</span><span class="hljs-preprocessor">.Point</span><span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.InstrumentationRegistry</span><span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.runner</span><span class="hljs-preprocessor">.AndroidJUnit</span>4<span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.uiautomator</span><span class="hljs-preprocessor">.By</span><span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.uiautomator</span><span class="hljs-preprocessor">.Direction</span><span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.uiautomator</span><span class="hljs-preprocessor">.UiDevice</span><span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.uiautomator</span><span class="hljs-preprocessor">.UiObject</span>2<span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.uiautomator</span><span class="hljs-preprocessor">.Until</span><span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.util</span><span class="hljs-preprocessor">.Log</span><span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.widget</span><span class="hljs-preprocessor">.ListView</span><span class="hljs-comment">;</span>
import android<span class="hljs-preprocessor">.widget</span><span class="hljs-preprocessor">.TextView</span><span class="hljs-comment">;</span>

import org<span class="hljs-preprocessor">.junit</span><span class="hljs-preprocessor">.Before</span><span class="hljs-comment">;</span>
import org<span class="hljs-preprocessor">.junit</span><span class="hljs-preprocessor">.Test</span><span class="hljs-comment">;</span>
import org<span class="hljs-preprocessor">.junit</span><span class="hljs-preprocessor">.runner</span><span class="hljs-preprocessor">.RunWith</span><span class="hljs-comment">;</span>

import java<span class="hljs-preprocessor">.util</span><span class="hljs-preprocessor">.List</span><span class="hljs-comment">;</span>

<span class="hljs-comment">/**
 * Created by mqqzt-yang on 2015/11/22.
 */</span>
@RunWith(AndroidJUnit4<span class="hljs-preprocessor">.class</span>)
public class UiObject2Test {
    UiDevice uiDevice<span class="hljs-comment">;</span>
    Instrumentation instrumentation<span class="hljs-comment">;</span>

    @Before
    public void setUp(){
        instrumentation = InstrumentationRegistry<span class="hljs-preprocessor">.getInstrumentation</span>()<span class="hljs-comment">;</span>
        uiDevice = UiDevice<span class="hljs-preprocessor">.getInstance</span>(instrumentation)<span class="hljs-comment">;</span>
    }


    @Test
    public void testSetText(){
        UiObject2 mmsSendObject = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.res</span>(<span class="hljs-string">"com.android.mms:id/embedded_text_editor"</span>))<span class="hljs-comment">;</span>
        mmsSendObject<span class="hljs-preprocessor">.setText</span>(<span class="hljs-string">"我的东西在哪里?"</span>)<span class="hljs-comment">;</span>
        <span class="hljs-keyword">sleep</span>(<span class="hljs-number">1000</span>)<span class="hljs-comment">;</span>
        mmsSendObject<span class="hljs-preprocessor">.clear</span>()<span class="hljs-comment">;</span>
    }

    @Test
    public void testDrag(){
        UiObject2 settingsObj = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.text</span>(<span class="hljs-string">"Settings"</span>))<span class="hljs-comment">;</span>
        Point p = new Point()<span class="hljs-comment">;</span>
        p<span class="hljs-preprocessor">.x</span> = <span class="hljs-number">116</span><span class="hljs-comment">;</span>
        p<span class="hljs-preprocessor">.y</span> = <span class="hljs-number">497</span><span class="hljs-comment">;</span>
        settingsObj<span class="hljs-preprocessor">.drag</span>(p, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span>
        <span class="hljs-keyword">sleep</span>(<span class="hljs-number">2000</span>)<span class="hljs-comment">;</span>
        settingsObj<span class="hljs-preprocessor">.clickAndWait</span>(Until<span class="hljs-preprocessor">.newWindow</span>(), <span class="hljs-number">5000</span>)<span class="hljs-comment">;</span>
    }

    @Test
    public void testScroll(){
        UiObject2 listViewObj = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.clazz</span>(ListView<span class="hljs-preprocessor">.class</span>))<span class="hljs-comment">;</span>
        listViewObj<span class="hljs-preprocessor">.scroll</span>(Direction<span class="hljs-preprocessor">.DOWN</span>, <span class="hljs-number">0.8</span>f, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span>
    }


    @Test
    public void testPinch(){
        UiObject2 viewObj = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.res</span>(<span class="hljs-string">"com.android.gallery3d:id/gl_root_view"</span>))<span class="hljs-comment">;</span>
        viewObj<span class="hljs-preprocessor">.pinchClose</span>(<span class="hljs-number">0.8</span>f, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span>
        <span class="hljs-keyword">sleep</span>(<span class="hljs-number">2000</span>)<span class="hljs-comment">;</span>
        viewObj<span class="hljs-preprocessor">.pinchOpen</span>(<span class="hljs-number">0.8</span>f, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span>
    }

    @Test
    public void testFling(){
        UiObject2 msg = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.res</span>(<span class="hljs-string">"com.android.contacts:id/message"</span>))<span class="hljs-comment">;</span>
        msg<span class="hljs-preprocessor">.fling</span>(Direction<span class="hljs-preprocessor">.LEFT</span>, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span>
    }

    @Test
    public void testSwipe(){
        UiObject2 msg = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.res</span>(<span class="hljs-string">"com.android.contacts:id/message"</span>))<span class="hljs-comment">;</span>
        msg<span class="hljs-preprocessor">.swipe</span>(Direction<span class="hljs-preprocessor">.RIGHT</span>, <span class="hljs-number">0.8</span>f, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span>
    }

    @Test
    public void testWifiSwitch(){
        UiObject2 wifiSwitchObj = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.res</span>(<span class="hljs-string">"com.android.settings:id/switch_widget"</span>))<span class="hljs-comment">;</span>
        if(!wifiSwitchObj<span class="hljs-preprocessor">.isChecked</span>()){
            wifiSwitchObj<span class="hljs-preprocessor">.click</span>()<span class="hljs-comment">;</span>
        }
    }

    @Test
    public void testChild(){
        UiObject2 listViewObj = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.clazz</span>(ListView<span class="hljs-preprocessor">.class</span>))<span class="hljs-comment">;</span>
        UiObject2 child = listViewObj<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.clazz</span>(TextView<span class="hljs-preprocessor">.class</span>))<span class="hljs-comment">;</span>
        Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, child<span class="hljs-preprocessor">.getText</span>())<span class="hljs-comment">;</span>

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

        List<UiObject2> getChild = listViewObj<span class="hljs-preprocessor">.getChildren</span>()<span class="hljs-comment">;</span>
        Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, getChild<span class="hljs-preprocessor">.size</span>() + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span>
        for(UiObject2 uiObject2 : getChild){
            Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, uiObject2<span class="hljs-preprocessor">.getClassName</span>())<span class="hljs-comment">;</span>
        }

        Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, listViewObj<span class="hljs-preprocessor">.getParent</span>()<span class="hljs-preprocessor">.getClassName</span>())<span class="hljs-comment">;</span>
        Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, listViewObj<span class="hljs-preprocessor">.getChildCount</span>() + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span>

        UiObject2 childEqual = listViewObj<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.text</span>(<span class="hljs-string">"Alarms"</span>))<span class="hljs-comment">;</span>
        Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, child<span class="hljs-preprocessor">.equals</span>(childEqual) + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span>

        Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, child<span class="hljs-preprocessor">.hasObject</span>(By<span class="hljs-preprocessor">.text</span>(<span class="hljs-string">"Alarms"</span>)) + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span>
        Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, child<span class="hljs-preprocessor">.hasObject</span>(By<span class="hljs-preprocessor">.text</span>(<span class="hljs-string">"Alarms12"</span>)) + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span>

        childEqual<span class="hljs-preprocessor">.recycle</span>()<span class="hljs-comment">;</span>
        childEqual = listViewObj<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.text</span>(<span class="hljs-string">"Alarms"</span>))<span class="hljs-comment">;</span>
        childEqual<span class="hljs-preprocessor">.click</span>()<span class="hljs-comment">;</span>
    }


    public void <span class="hljs-keyword">sleep</span>(int <span class="hljs-keyword">sleep</span>){
        try {
            Thread<span class="hljs-preprocessor">.sleep</span>(<span class="hljs-keyword">sleep</span>)<span class="hljs-comment">;</span>
        } catch (InterruptedException e) {
            e<span class="hljs-preprocessor">.printStackTrace</span>()<span class="hljs-comment">;</span>
        }
    }
}</code>

最后

以上就是感性咖啡为你收集整理的uiautomator 之uiautomator 2.0 UiObject2学习收藏学习收藏http://blog.csdn.net/swordgirl2011/article/details/50993157的全部内容,希望文章能够帮你解决uiautomator 之uiautomator 2.0 UiObject2学习收藏学习收藏http://blog.csdn.net/swordgirl2011/article/details/50993157所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部