要使用java编写monkeyrunner脚本,目前本人使用的SDK版本(22.6)。Eclipse里面需要导入sdk/toos/lib目录下的一些jar包,导入chimpchat.jar和sdklib.jar,jython-standalone-2.5.3.jar即可,其余一些jar包会自动导入。下面给出示例代码:
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Collection; import java.util.HashMap; import com.android.chimpchat.adb.AdbBackend; import com.android.chimpchat.adb.AdbChimpDevice; import com.android.chimpchat.core.ChimpImageBase; import com.android.chimpchat.core.IChimpImage; import com.android.chimpchat.core.TouchPressType; public class Test { public static void main(String[] args) throws Exception { AdbBackend adb = new AdbBackend(); AdbChimpDevice device = (AdbChimpDevice) adb.waitForConnection(5000, "788a6ab5"); //后面的参数为设备id号 Thread.sleep(5000); device.drag(530, 1590, 530, 130, 10, 500); //使用的真机为上滑解锁屏幕 Thread.sleep(2000); //设置截图的文件名,以当前系统时间命名 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");//设置日期格式 String time = dateFormat.format(new Date());// new Date()为获取当前系统时间 String fileName = time + ".png"; //将截图存放在设备里面 //device.shell("screencap -p /sdcard/screenshot/" + fileName); //将截图存放于本地D盘下 device.takeSnapshot().writeToFile("D:\\screen\\picture\\" + fileName, "png"); Thread.sleep(2000); device.press("KEYCODE_MENU", TouchPressType.DOWN_AND_UP);//按下菜单键 device.takeSnapshot().getSubImage(0, 1353, 1080, 1920-1353).writeToFile("D:\\screen\\picture\\" + fileName, "png"); //按规格截取图片存放于本地 Thread.sleep(2000); //截取图片与本地图片进行对比 IChimpImage picture = ChimpImageBase.loadImageFromFile("D:\\screen\\picture\\menu.png"); System.out.println(device.takeSnapshot().getSubImage(0, 1353, 1080, 1920-1353).sameAs(picture, 1.0));//图片相同打印true,否则打印false device.drag(900, 1100, 200, 1100, 10, 500); Thread.sleep(2000); //长按图标进行拖动 device.touch(412, 1084, TouchPressType.DOWN); Thread.sleep(1500); device.drag(412, 1084, 668, 1084, 1, 2000); Thread.sleep(1500); device.touch(668, 1084, TouchPressType.UP); //启动系统设置 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="">(), "com.android.settings/.Settings", 0); adb.shutdown(); } }