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

如前文所述,UIDevice有20个接口方法用于触屏操作。前文已记录了两个类型,功能键和开启固定界面,本文将记录按键型的7个接口方法。

先列举一下本文涉及的方法。
(1)public boolean pressDPadLeft(),方向键,向左
(2)public boolean pressDPadRight(),方向键,向右
(3)public boolean pressDPadDown(),方向键,向下
(4)public boolean pressDPadUp() ,方向键,向上
(5)public boolean pressDPadCenter(),方向键,中心点,并非pressEnter()
(6)public boolean pressKeyCode(int keyCode) ,发送KeyEvent
(7)public boolean pressKeyCode(int keyCode, int metaState),发送配有组合键(ALT,SHIFT)的KeyEvent。

先说一下DPad。老一点的android机型都有方向键(硬件),现有机型基本已无配备。我们可以从启动的虚拟设备中,看到其原有形态。如下图:

这里写图片描述

注:虚拟设备中,默认不启用DPAD,可通过修改ini文件启动,具体可查找Baidu。这里给出一个参考。
http://jingyan.baidu.com/article/e3c78d644d22ec3c4c85f5f5.html

需要指出的是,新设备无DPad,并不代表pressDPadXXX方法不能被使用。
下面通过一个例子来记录5个方向键方法的使用。

测试需求:打开测试APP,使用方向键移动EditView上的光标。

测试案例代码如下:

 @Test
    public void FunctionKeyTest2(){
        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(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                mInput1.setText("SSS");
                mDevice.waitForWindowUpdate(null,timeOut);
                Log.i(TAG, "wait For Window Update, result = "+result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressDPadLeft();
                Log.i(TAG, "press DPad Left, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressDPadRight();
                Log.i(TAG, "press DPad Right, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressDPadDown();
                Log.i(TAG, "press DPad Down, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressDPadUp();
                Log.i(TAG, "press DPad Up, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressDPadDown();
                Log.i(TAG, "press DPad Down, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressDPadCenter();
                Log.i(TAG, "press DPad Center, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);
            }
        }

运行结果如下:

11-06 18:46:02.457 I/com.breakloop.u2demo.uidevice.FingerTest: Start Test
11-06 18:46:03.458 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:46:03.923 I/com.breakloop.u2demo.uidevice.FingerTest: open APP
11-06 18:46:04.020 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-06 18:46:05.882 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:46:06.847 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:46:08.790 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:46:08.828 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Left, result = true
11-06 18:46:09.860 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:46:09.874 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Right, result = true
11-06 18:46:10.905 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:46:10.935 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Down, result = true
11-06 18:46:11.959 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:46:11.979 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Up, result = true
11-06 18:46:13.002 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:46:13.287 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Down, result = true
11-06 18:46:14.888 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:46:14.912 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Center, result = true

运行效果如下:
这里写图片描述

从效果图可见,pressDPadCenter()并非回车换行。

通过查看pressDPadXXX方法的源码,可以发现,其本质是对public boolean pressKeyCode(int keyCode) 方法的调用。

这里写图片描述

而public boolean pressKeyCode(int keyCode, int metaState)方法,则是在public boolean pressKeyCode(int keyCode) 基础上,添加了Alt/Shift辅助键的调用。其meteState仅有四个值(0,1,2,3)。0代表meta_key未被激活;1代表shift或caps_lock被激活;2代表alt被激活
3代表alt,shift或caps_lock同时被激活。

因此,上面的例子,完全可以使用presskeyCode进行重写。

重写后,代码如下:

@Test
    public void FunctionKeyTest3(){
        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(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result=mDevice.pressKeyCode(KeyEvent.KEYCODE_S,1);//此处与之前代码相比,做了修改,使用pressKeyCode方法,代替实现mInput1.setText("SSS");的原有功能
                Log.i(TAG, "Write Big S, result = " + result);
                mDevice.waitForWindowUpdate(null,timeOut);
                Log.i(TAG, "wait For Window Update, result = "+result);

                result=mDevice.pressKeyCode(KeyEvent.KEYCODE_S);//此处为新添加,跟mInput1.setText("s");操作类似,只是小写罢了
                Log.i(TAG, "Write Small S, result = " + result);
                mDevice.waitForWindowUpdate(null,timeOut);
                Log.i(TAG, "wait For Window Update, result = "+result);

                result = mDevice.pressKeyCode(KeyEvent.KEYCODE_DPAD_LEFT);
                Log.i(TAG, "press KEYCODE_DPAD_LEFT, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressKeyCode(KeyEvent.KEYCODE_DPAD_RIGHT);
                Log.i(TAG, "press KEYCODE_DPAD_RIGHT, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressKeyCode(KeyEvent.KEYCODE_DPAD_DOWN);
                Log.i(TAG, "press KEYCODE_DPAD_DOWN, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressKeyCode(KeyEvent.KEYCODE_DPAD_UP);
                Log.i(TAG, "press KEYCODE_DPAD_UP, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressKeyCode(KeyEvent.KEYCODE_DPAD_DOWN);
                Log.i(TAG, "press KEYCODE_DPAD_DOWN, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);

                result = mDevice.pressKeyCode(KeyEvent.KEYCODE_DPAD_CENTER);
                Log.i(TAG, "press KEYCODE_DPAD_CENTER, result = " + result);
                result = mDevice.waitForWindowUpdate(packageName, timeOut);
                Log.i(TAG, "wait For Window Update, result = " + result);
            }
        }
    }

运行结果如下:

11-06 18:56:42.365 I/com.breakloop.u2demo.uidevice.FingerTest: open APP
11-06 18:56:42.456 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-06 18:56:44.302 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:56:44.330 I/com.breakloop.u2demo.uidevice.FingerTest: Write Big S, result = true
11-06 18:56:45.331 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-06 18:56:45.370 I/com.breakloop.u2demo.uidevice.FingerTest: Write Small S, result = true
11-06 18:56:46.371 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true
11-06 18:56:46.395 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_LEFT, result = true
11-06 18:56:47.900 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:56:47.913 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_RIGHT, result = true
11-06 18:56:48.941 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:56:48.961 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_DOWN, result = true
11-06 18:56:49.982 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:56:50.001 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_UP, result = true
11-06 18:56:51.027 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:56:51.045 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_DOWN, result = true
11-06 18:56:52.071 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
11-06 18:56:52.094 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_CENTER, result = true
11-06 18:56:53.104 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false

运行效果如下:

这里写图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值