UIAutomator2.0详解(UIDevice篇----触屏操作2)
来源:互联网 发布:移动硬盘数据同步软件 编辑:程序博客网 时间:2024/06/06 05:10
如前文所述,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 Test11-06 18:46:03.458 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:46:03.923 I/com.breakloop.u2demo.uidevice.FingerTest: open APP11-06 18:46:04.020 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true11-06 18:46:05.882 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:46:06.847 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:46:08.790 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:46:08.828 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Left, result = true11-06 18:46:09.860 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:46:09.874 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Right, result = true11-06 18:46:10.905 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:46:10.935 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Down, result = true11-06 18:46:11.959 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:46:11.979 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Up, result = true11-06 18:46:13.002 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:46:13.287 I/com.breakloop.u2demo.uidevice.FingerTest: press DPad Down, result = true11-06 18:46:14.888 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-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 APP11-06 18:56:42.456 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true11-06 18:56:44.302 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:56:44.330 I/com.breakloop.u2demo.uidevice.FingerTest: Write Big S, result = true11-06 18:56:45.331 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true11-06 18:56:45.370 I/com.breakloop.u2demo.uidevice.FingerTest: Write Small S, result = true11-06 18:56:46.371 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = true11-06 18:56:46.395 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_LEFT, result = true11-06 18:56:47.900 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:56:47.913 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_RIGHT, result = true11-06 18:56:48.941 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:56:48.961 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_DOWN, result = true11-06 18:56:49.982 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:56:50.001 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_UP, result = true11-06 18:56:51.027 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:56:51.045 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_DOWN, result = true11-06 18:56:52.071 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false11-06 18:56:52.094 I/com.breakloop.u2demo.uidevice.FingerTest: press KEYCODE_DPAD_CENTER, result = true11-06 18:56:53.104 I/com.breakloop.u2demo.uidevice.FingerTest: wait For Window Update, result = false
运行效果如下:
- UIAutomator2.0详解(UIDevice篇----触屏操作2)
- UIAutomator2.0详解(UIDevice篇----触屏操作1)
- UIAutomator2.0详解(UIDevice篇----触屏操作3)
- UIAutomator2.0详解(UIDevice篇----Screen操作)
- UIAutomator2.0详解(UIDevice篇---- 序)
- UIAutomator2.0详解(UIDevice篇----Wait)
- UIAutomator2.0详解(UIDevice篇----waitForIdle)
- UIAutomator2.0详解(UIDevice篇----waitForWindowUpdate)
- UIAutomator2.0详解(UIDevice篇----Hierarchy)
- UIAutomator2.0详解(UIDevice篇----截屏)
- UIAutomator2.0详解(UIDevice篇----performActionAndWait)
- UIAutomator2.0详解(UIDevice篇----UIWatcher)
- UIAutomator2.0详解(UIDevice篇----获取UIDevice对象)
- UIAutomator2.0详解(UIDevice篇----获取控件)
- UIAutomator2.0详解(UIDevice篇----LastTraversedText质疑)(未解)
- UIAutomator2.0详解(UIDevice篇----获取设备名称和Package名称)
- UIAutomator2.0详解(入门篇)
- UIAutomator2.0详解(JUnit Annotation篇)
- DFA应用——判断合法数字
- Java静态方法为什么不能访问非静态方法
- ElasticSearch5.x踩坑记录(二):linux下es5.6.3、head、ik安装
- 软件工程(C编码实践篇)学习总结
- sql中查询一张表显示多列自己需要的数据
- UIAutomator2.0详解(UIDevice篇----触屏操作2)
- Linux中使用update-alternatives配置多个jdk
- Postman使用官方文档翻译--7设置介绍
- wmd的改进算法
- 【2017年cs231n学习笔记2】Lecture3 损失函数 最优化
- 自定义View之Paint
- Java实现同步的几种方式
- expected '=', ',', ';', 'asm' or '__attribute__' before xxx
- CCF认证201703-2--学生排队