脚本举例:
  用文本编辑器编辑一个名为test.py的python脚本,内容如下:
  from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
  #等待主机与android设备连接
  device = MonkeyRunner.waitForConnection()
  MonkeyRunner.sleep(3)
  #杀掉待测程序,通常用于排除干扰
  device.shell('am force-stop com.alpha.lenovo.launcher')
  MonkeyRunner.sleep(3)
  #启动待测程序的主activity
  device.startActivity(component='com.alpha.lenovo.launcher/com.alpha.lenovo.launcher.AlphaLauncher')
  #开始发送按键
  MonkeyRunner.sleep(10)
  device.press('KEYCODE_DPAD_UP','DOWN_AND_UP')
  MonkeyRunner.sleep(2)
  device.press('KEYCODE_DPAD_RIGHT','DOWN_AND_UP')
  MonkeyRunner.sleep(5)
  device.press("KEYCODE_DPAD_CENTER","DOWN_AND_UP")
  MonkeyRunner.sleep(10)
  device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
  MonkeyRunner.sleep(3)
  device.press('KEYCODE_BACK ','DOWN_AND_UP')
  MonkeyRunner.sleep(3)
  #屏幕抓图
  result = device.takeSnapshot()
  #文件将写入主机上,脚本所在目录
  result.writeToFile('./pic001.png','png')
  运行脚本
  1. 安装python2.7,在系统的环境变量Path上添加python的安装路径
  2. 用adb连接测试设备
  3. 进入android sdk的tools目录,执行monkeyrunner test.py