Android自动化测试之使用java调用monkeyrunner(补充篇)
上一篇使用java调用monkeyrunner(http://fengbohaishang.blog.51cto.com/5106297/1065647)中遗留了一个问题,就是上次用的是低版本的4个包解决的问题,使用高版本的jar包怎么调用monkeyrunner呢? 经过一位朋友的提示说,现在高版本的方法已经变了,我就按照他的提示,上网搜了一下需要的类,测试通过后,特写此补充篇总结一下。 上次使用的是android sdktools路径下的lib里面的4个包:ddmlib.jar,guavalib.jar,monkeyrunner.jar,sdklib.jar.而更新后的版本需要添加另外一个包就是:chimpchat.jar,monkerunner.jar这个包倒不是必须的了。另外,低版本中是用AdbMonkeyDevice实现IMonkeyDevice,高版本中没有这两个类了,用的AdbChimpDevice和IchimpDevice。 而通过查看AdbChimpDevice( http://code.google.com/p/aster/source/browse/src/com/android/chimpchat/adb/AdbChimpDevice.java?r=967f7f8cd6249c69e00c6de7ff1b55bd0f51d311)和IchimpDevice( http://code.google.com/p/aster/source/browse/src/com/android/chimpchat/core/IChimpDevice.java?r=967f7f8cd6249c69e00c6de7ff1b55bd0f51d311)这两个类在官方的源码,就不难发现,AdbChimpDevice实现了IchimpDevice这个接口,不过连接方法还是通过AdbBackend,通过adb方式连接模拟器,或者真机。只是Device的父类发生了变化。 下面还是用以前的测试类,进行稍微改变一下,就可以看出两者的不同: importjava.util.ArrayList; importjava.util.Collection; importjava.util.HashMap; importcom.android.chimpchat.adb.AdbBackend; importcom.android.chimpchat.adb.AdbChimpDevice; publicclassTestNewMonkeyrunner{ /** *@paramargs */ //这里有变化 privatestaticAdbChimpDevicedevice; privatestaticAdbBackendadb; publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub if(adb==null){ adb=newAdbBackend(); //参数分别为自己定义的等待连接时间和设备id //这里需要注意一下adb的类型 device=(AdbChimpDevice)adb.waitForConnection(8000,"MSM8225QRD5"); } //添加启动权限 Stringaction="android.intent.action.MAIN"; Collection<String>categories=newArrayList<String>(); categories.add("android.intent.category.LAUNCHER"); //启动要测试的主界面 device.startActivity(null,action,null,null,categories, newHashMap<String,Object>(),"cn.com.fetion/.android.ui.activities.StartActivity",0); //点击某一个坐标 //touch方法略有变化 device.touch(202,258,com.android.chimpchat.core.TouchPressType.DOWN_AND_UP); } } 从上面可以看出,高版本与低版本的变化,并不是很多。只要连接上设备,一些需要用到的操作方法,自己可以去源码里面看,也可以自己重写一些常用的方法。 源码里的注释是非常详细,比如IchimpDevice接口类中的startActivity方法: voidstartActivity(@NullableStringuri,@NullableStringaction, @NullableStringdata,@NullableStringmimeType, Collection<String>categories,Map<String,Object>extras,@NullableStringcomponent, intflags); /** *Sendabroadcastintenttothedevice. * *@paramuritheURIfortheIntent *@paramactiontheactionfortheIntent *@paramdatathedataURIfortheIntent *@parammimeTypethemimetypefortheIntent *@paramcategoriesthecategorynamesfortheIntent *@paramextrastheextrastoaddtotheIntent *@paramcomponentthecomponentoftheIntent *@paramflagstheflagsfortheIntent */ 该方法里对重要参数解释的都很清楚。所以,建议正在研究java调用monkeyrunner问题的朋友们,不要忘了源码这个最好的资源。 本文转自 风泊海上 51CTO博客,原文链接:http://blog.51cto.com/fengbohaishang/1071155