在monkeyrunner里使用Java做为脚本语言

本文介绍如何使用Java语言编写MonkeyRunner脚本,实现自动化测试。通过示例代码展示了连接设备、触摸操作、拖拽及输入文本等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

monkeyrunner官网和很多地方都是使用的python做为脚本语言的,但是实际上monkeyrunner是支持Java做为脚本语言的,下面是对在monkeyrunner中使用Java的一些尝试,已经全部使用过是可行的,需要引入的jar包包括:
ddmlib.jar;guavalib.jar;sdklib.jar和monkeyrunner.jar
package com.test

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import com.aliyun.smoking.monkeyrunner.extend.ImageProcess;
import com.android.monkeyrunner.adb.AdbBackend;
import com.android.monkeyrunner.core.IMonkeyDevice;
import com.android.monkeyrunner.core.IMonkeyImage;
import com.android.monkeyrunner.core.TouchPressType;

/**
* this class provide java code to call monkeyrunner.jar to execute test case
*
* @author cx
*
*/
public class RunnerProxy {
private IMonkeyDevice device;
private AdbBackend adb;
private static RunnerProxy instance;

public static RunnerProxy getInstance() {
if (instance == null) {
instance = new RunnerProxy();
}
return instance;
}

private RunnerProxy() {
adb = new AdbBackend();
}

/**
* this function will connect to a device, emulator or phone
*/
public void connect() {
assert (adb != null);
device = adb.waitForConnection();
}

/**
* this function clear device connect
*/
public void dispose() {
if (device != null) {
device.dispose();
device = null;
}
}

private String imageDir;

public void setImageDir(String imageDir) {
this.imageDir = imageDir;
}

public String getImageDir() {
return imageDir;
}

private String logDir;

public void setLogDir(String logDir) {
this.logDir = logDir;
}

public String getLogDir() {
return logDir;
}

/**
* this function finish touch operation
*
* @param x
* : x coordinate
* @param y
* : y coordinate
*/
public void touch(int x, int y) {
assert (device != null);
device.shell("sendevent /dev/input/event6 3 48 1");
device.shell("sendevent /dev/input/event6 3 53 " + x);
device.shell("sendevent /dev/input/event6 3 54 " + y);
device.shell("sendevent /dev/input/event6 0 2 0");
device.shell("sendevent /dev/input/event6 0 0 0");
device.shell("sendevent /dev/input/event6 3 48 0");
device.shell("sendevent /dev/input/event6 0 2 0");
device.shell("sendevent /dev/input/event6 0 0 0");
}

/**
* this function finish long touch operation
*
* @param x
* : x coordinate
* @param y
* : y coordinate
*/
public void longTouch(int x, int y) {
assert (device != null);
device.drag(x, y, x, y, 10, 2);
}

/**
* this function finish drag from one point to another point
*
* @param x
* : x coordinate of start point
* @param y
* : y coordinate of start point
* @param endX
* : x coordinate of end point
* @param endY
* : Y coordinate of end point
*
*/
public void drag(int x, int y, int endX, int endY) {
assert (device != null);
device.drag(x, y, endX, endY, 10, 2);
}

/**
* this function finish type a text to view operation
*
* @param value
* : text to type in
*/
public void type(String value) {
assert (device != null);
device.type(value);
}

/**
* this function finish click a key operation
*
* @param keyCode
* : key code
*/
public void press(String keyCode) {
assert (device != null);
device.press(keyCode, TouchPressType.DOWN_AND_UP);
}

/**
* this function finish start an activity operation
*
* @param component
* : activity what to start
*/
public void startActivity(String component) {
assert (device != null);
String action = "android.intent.action.MAIN";
Collection<String> categories = new ArrayList<String>();
categories.add("android.intent.category.LAUNCHER");
device.startActivity(null, action, null, null, categories,
new HashMap<String, Object>(), component, 0);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值