UIAutomator2.0详解(UIDevice篇----触屏操作1)

UIDevice中有20个方法,是关于触屏操作的,占方法总数的三分之一。数量看似很多,仔细分析一下,也就几类。

(1)功能键型,7个,HOME,RECENT,BACK,DELETE,ENTER,MENU,SEARCH
(2)开启固定界面型,2个,Notification,Quick Setting
(3)按键型,7个,包括方向型(上下左右中),以及KeyCode事件型
(4)单击、拖拽型,4个

由于不想将博文变得冗长,该部分我们将分为三个章节,本文将讲述前两项,即功能键和固定界面。

先列一下,需要调用的接口方法。

功能键型(具体按键可对应下图,未给出回车键):

(1)public boolean pressSearch() ,点击查找功能键
(2)public boolean pressBack() ,点击后退键
(3)public boolean pressDelete() ,点击删除键
(4)public boolean pressEnter() ,点击回车键
(5)public boolean pressHome(),点击Home键
(6)public boolean pressMenu(),点击菜单键
(7)public boolean pressRecentApps(),点击在运行的APP键

固定界面型:

(1)public boolean openNotification(),展开通知栏
(2)public boolean openQuickSettings(),展开快速设置栏

图1

图2

需要指出的是,由于各厂家对于功能键的显示方式不同,导致某些功能键不存在,或者满足一定条件才显示。因此,本文所提供的样例,在不同手机上的测试效果可能存在差异。

注:关于Search键的显示,需要在EditView中设置两项属性

这里写图片描述

测试案例代码如下:

package com.breakloop.u2demo.uidevice;

import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.util.Log;

import com.breakloop.u2demo.common.Utils;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Created by user on 2017/11/3.
 */

public class FingerTest extends UIDeviceTest {
    @Test
    public void FunctionKeyTest(){
        String packageName="com.breakloop.test";
        String activityName=".MainActivity";
        boolean result;
        int timeOut=5000;

        Log.i(TAG, "Start Test");
        result=mDevice.waitForWindowUpdate(null,timeOut);
        Log.i(TAG, "wait For Window Update, result = "+result);

        //(1)Open APP
        Utils.startAPP(mDevice,packageName,activityName);
        Log.i(TAG, "open APP");
        result=mDevice.waitForWindowUpdate(null,timeOut);
        Log.i(TAG, "wait For Window Update, result = "+result);

        //(2)Click Edit View1, then Search Button appear
        UiObject2 mInput1=mDevice.findObject(By.res("com.breakloop.test:id/et_input1"));
        if(mInput1==null){
            Log.i(TAG, "Do not find Input1");
        }else {
            if (mInput1.isEnabled()) {
                mInput1.click();
                result = mDevice.waitForWindowUpdate(null, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                mInput1.setText("SSS");
                result=mDevice.pressSearch();
                Log.i(TAG, "Press Search Button, result = "+result);
                mDevice.waitForWindowUpdate(null,timeOut);
                Log.i(TAG, "wait For Window Update, result = "+result);
            }
        }

        //(3)Click Edit View2, then Menu Button appear
        UiObject2 mInput2=mDevice.findObject(By.res("com.breakloop.test:id/et_input2"));
        if(mInput2==null){
            Log.i(TAG, "Do not find Input2");
        }else {
            if(mInput2.isEnabled()){
                mInput2.click();
                result=mDevice.waitForWindowUpdate(null,timeOut);
                Log.i(TAG, "wait For Window Update, result = "+result);

                mInput2.setText("AAA");
                result=mDevice.pressEnter();
                Log.i(TAG, "Press Enter Button, result = "+result);
                mDevice.waitForWindowUpdate(null,timeOut);
                Log.i(TAG, "wait For Window Update, result = "+result);

                result=mDevice.pressMenu();
                Log.i(TAG, "Press Menu Button, result = "+result);
                mDevice.waitForWindowUpdate(packageName,timeOut);
                Log.i(TAG, "wait For Window Update, result = "+result);
            }
        }

        //(4)Change Activity and Press Back Button
        UiObject2 button=mDevice.findObject(By.res("com.breakloop.test:id/btn_goto2"));
        if(button==null){
            Log.i(TAG, "Do not find Button");
        }else {
            if (button.isEnabled()) {
                button.click();
                mDevice.waitForWindowUpdate(packageName, timeOut);
                result=mDevice.pressBack();
                Log.i(TAG, "Press Back Button, result = "+result);
                mDevice.waitForWindowUpdate(null,timeOut);
                Log.i(TAG, "wait For Window Update, result = "+result);
            }
        }

        //(5)Press Home Button
        result=mDevice.pressHome();
        Log.i(TAG, "Press Home Button, result = "+result);
        mDevice.waitForWindowUpdate(null,timeOut);
        Log.i(TAG, "wait For Window Update, result = "+result);

        //(6)Press Recent Button
        try {
            result=mDevice.pressRecentApps();
            Log.i(TAG, "Press Recet Apps Button, result = "+result);
            result=mDevice.waitForWindowUpdate(null,timeOut);
            Log.i(TAG, "wait For Window Update, result = "+result);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        //(7)Press Delete
        result=mDevice.pressDelete();
        Log.i(TAG, "Press Delete Button, result = "+result);
        result=mDevice.waitForWindowUpdate(null,timeOut);
        Log.i(TAG, "wait For Window Update, result = "+result);

        //(8)Press Home Button
        result=mDevice.pressHome();
        Log.i(TAG, "Press Home Button, result = "+result);
        result=mDevice.waitForWindowUpdate(null,timeOut);
        Log.i(TAG, "wait For Window Update, result = "+result);

        //(9)Open Notification
        result=mDevice.openNotification();
        Log.i(TAG, "Press Notification Button, result = "+result);
        result=mDevice.waitForWindowUpdate(null,timeOut);
        Log.i(TAG, "wait For Window Update, result = "+result);

        //(10)Press Home Button
        result=mDevice.pressHome();
        Log.i(TAG, "Press Home Button, result = "+result);
        result=mDevice.waitForWindowUpdate(null,timeOut);
        Log.i(TAG, "wait For Window Update, result = "+result);

        //(11)Open Quick Setting
        result=mDevice.openQuickSettings();
        Log.i(TAG, "Press Quick Setting Button, result = "+result);
        result=mDevice.waitForWindowUpdate(null,timeOut);
        Log.i(TAG, "wait For Window Update, result = "+result);

        //(12)Press Home Button
        result=mDevice.pressHome();
        Log.i(TAG, "Press Home Button, result = "+result);
        result=mDevice.waitForWindowUpdate(null,timeOut);
        Log.i(TAG, "wait For Window Update, result = "+result);

        Log.i(TAG, "End Test");
    }
}

执行结果如下:

11-03 16:13:24.876 I/com.breakloop.u2demo.uidevice.FingerTest: Start Test
11-03 16:13:25.015 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:25.470 I/com.breakloop.u2demo.uidevice.FingerTest: open APP
11-03 16:13:25.516 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:32.610 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:33.655 I/com.breakloop.u2demo.uidevice.FingerTest: Press Search Button, result = true
11-03 16:13:34.043 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:36.584 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:37.639 I/com.breakloop.u2demo.uidevice.FingerTest: Press Enter Button, result = true
11-03 16:13:37.680 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:39.600 I/com.breakloop.u2demo.uidevice.FingerTest: Press Menu Button, result = true
11-03 16:13:47.575 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:50.646 I/com.breakloop.u2demo.uidevice.FingerTest: Press Back Button, result = true
11-03 16:13:51.095 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:52.650 I/com.breakloop.u2demo.uidevice.FingerTest: Press Home Button, result = true
11-03 16:13:52.698 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:54.603 I/com.breakloop.u2demo.uidevice.FingerTest: Press Recet Apps Button, result = true
11-03 16:13:54.657 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:56.620 I/com.breakloop.u2demo.uidevice.FingerTest: Press Delete Button, result = true
11-03 16:13:56.661 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:13:59.647 I/com.breakloop.u2demo.uidevice.FingerTest: Press Home Button, result = true
11-03 16:13:59.648 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:14:01.619 I/com.breakloop.u2demo.uidevice.FingerTest: Press Notification Button, result = true
11-03 16:14:01.629 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:14:03.666 I/com.breakloop.u2demo.uidevice.FingerTest: Press Home Button, result = true
11-03 16:14:03.670 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:14:05.633 I/com.breakloop.u2demo.uidevice.FingerTest: Press Quick Setting Button, result = true
11-03 16:14:05.642 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:14:07.752 I/com.breakloop.u2demo.uidevice.FingerTest: Press Home Button, result = true
11-03 16:14:07.772 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-03 16:14:07.772 I/com.breakloop.u2demo.uidevice.FingerTest: End Test

执行效果如下图:

这里写图片描述

比对执行效果和LOG,发现pressMenu触发成功,但并未显示。原因未查到。
另,Android7.0基本上导航键上已经没有菜单键了。

使用测试手机为华为Mate8,Android版本为7.0。

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值