java monkeyrunner_Android自动化测试之使用java调用monkeyrunner(补充篇)

上一篇使用java调用monkeyrunner(https://fengbohaishang.blog.51cto.com/5106297/1065647)中遗留了一个问题,就是上次用的是低版本的4个包解决的问题,使用高版本的jar包怎么调用monkeyrunner呢?

经过一位朋友的提示说,现在高版本的方法已经变了,我就按照他的提示,上网搜了一下需要的类,测试通过后,特写此补充篇总结一下。

上次使用的是android sdk tools路径下的lib里面的4个包:ddmlib.jar,guavalib.jar,monkeyrunner.jar,sdklib.jar.而更新后的版本需要添加另外一个包就是:chimpchat.jar,monkerunner.jar这个包倒不是必须的了。另外,低版本中是用AdbMonkeyDevice实现IMonkeyDevice,高版本中没有这两个类了,用的AdbChimpDevice和IchimpDevice。

而通过查看AdbChimpDevice   www.2cto.com(https://code.google.com/p/aster/source/browse/src/com/android/chimpchat/adb/AdbChimpDevice.java?r=967f7f8cd6249c69e00c6de7ff1b55bd0f51d311)和IchimpDevice(https://code.google.com/p/aster/source/browse/src/com/android/chimpchat/core/IChimpDevice.java?r=967f7f8cd6249c69e00c6de7ff1b55bd0f51d311)这两个类在官方的源码,就不难发现,AdbChimpDevice实现了IchimpDevice这个接口,不过连接方法还是通过AdbBackend,通过adb方式连接模拟器,或者真机。只是Device的父类发生了变化。

下面还是用以前的测试类,进行稍微改变一下,就可以看出两者的不同:

import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

import com.android.chimpchat.adb.AdbBackend;

import com.android.chimpchat.adb.AdbChimpDevice;

public class TestNewMonkeyrunner {

/**

* @param args

*/

//这里有变化

private static AdbChimpDevice device;

private static AdbBackend adb;

public static void main(String[] args) {

// TODO Auto-generated method stub

if (adb==null){

adb = new AdbBackend();

//      参数分别为自己定义的等待连接时间和设备id

//这里需要注意一下adb的类型

device = (AdbChimpDevice) adb.waitForConnection(8000,"MSM8225QRD5");

}

//添加启动权限

String action = "android.intent.action.MAIN";

Collection categories = new ArrayList();

categories.add("android.intent.category.LAUNCHER");

//              启动要测试的主界面

device.startActivity(null, action, null, null, categories,

new HashMap(),"cn.com.fetion/.android.ui.activities.StartActivity", 0);

//           点击某一个坐标

//touch方法略有变化

device.touch(202,258,com.android.chimpchat.core.TouchPressType.DOWN_AND_UP);

}

}

从上面可以看出,高版本与低版本的变化,并不是很多。只要连接上设备,一些需要用到的操作方法,自己可以去源码里面看,也可以自己重写一些常用的方法。

源码里的注释是非常详细,比如IchimpDevice接口类中的startActivity方法:

void startActivity(@Nullable String uri, @Nullable String action,

@Nullable String data, @Nullable String mimeType,

Collection categories, Map extras, @Nullable String component,

int flags);

/**

* Send a broadcast intent to the device.

*

* @param uri the URI for the Intent

* @param action the action for the Intent

* @param data the data URI for the Intent

* @param mimeType the mime type for the Intent

* @param categories the category names for the Intent

* @param extras the extras to add to the Intent

* @param component the component of the Intent

* @param flags the flags for the Intent

*/

该方法里对重要参数解释的都很清楚。所以,建议正在研究java调用monkeyrunner问题的朋友们,不要忘了源码这个最好的资源。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值