自动化测试中不同uid 之UI touch操作

来源:互联网 发布:michael angelo知乎 编辑:程序博客网 时间:2024/06/11 16:56

在自动化测试中touch组件的操作,一般有以下几个方法

x,y代表横纵坐标

1.MonkeyRunner

在其Python脚本或者monkeyrunner命令行中调用

device.touch(x,y,'')

2.Instrumentation

try {                               Instrumentation inst=new Instrumentation();                               inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),                                       SystemClock.uptimeMillis(),                                        MotionEvent.ACTION_DOWN, x, y, 0));                               inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),                                       SystemClock.uptimeMillis(),                                        MotionEvent.ACTION_UP, x, y, 0));                            } catch(Exception e) {                              Log.e("Exception when sendPointerSync", e.toString());                         }  

3.Solo

需导入robotium库,robotium是一套黑盒测试工具

solo.clickOnScreen(x,y);

 

4.利用runtime运行shell命令

Runtime.getRuntime.exc("sendevent /dev/input/event0 3 0 x")Runtime.getRuntime.exc("sendevent /dev/input/event0 3 1 y")Runtime.getRuntime.exc("sendevent /dev/input/event0 1 330 1")Runtime.getRuntime.exc("sendevent /dev/input/event0 0 0 0")       Runtime.getRuntime.exc("sendevent /dev/input/event0 1 330 0")Runtime.getRuntime.exc("sendevent /dev/input/event0 0 0 0")以上六句缺一不可
 
以上方法中,2、3只能touch当前的线程的UI,如果超越界限将拒绝访问,比如你在当前Activity发送对statusBar的点击事件,那么将报错。虽然二者同时存在于同一界面,但属于不同UID线程。1、4方法有点像第三者的操作,所以屏幕任何地方均可用于。但是MonkeyRunner仅适用于黑盒测试,在大部分基于白盒的自动化测试,都是采用创建Instrumentation的APK的方法,所以个人觉得4方法比较巧妙
 

原创粉丝点击