Android自动化测试之monkeyrunner
jopen
13年前
1. #导入模块; <br /> from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage <br /> <br /> 2. #连接当前设备,并返回一个MonkeyDevice对象; <br /> device = MonkeyRunner.waitForConnection() <br /> if not device: <br /> print "Please connect a device to start!" <br /> else: <br /> print "Start " <br /> <br /> 3. #安装Android包,注意,此方法返回的返回值为boolean,由此可以判断安装过程是否正常 ; <br /> device.installPackage('myproject/bin/MyApplication.apk') <br /> <br /> device.removePackage ('com.example.android.notepad') <br /> print ('卸载成功') <br /> device.installPackage('ApiDemos.apk') <br /> print ('安装成功') <br /> <br /> 4. #启动一个Activity; <br /> device.startActivity <br /> <br /> (component='com.android.htccontacts/com.android.htccontacts.ContactsTabActivity') <br /> <br /> 5. #截图; <br /> result = device.takeSnapshot() <br /> result.writeToFile('C:\\Users\\Martin\\Desktop\\test.png','png') <br /> <br /> 6. #时延(秒); <br /> MonkeyRunner.sleep(3) <br /> <br /> 7. #滑动屏幕; <br /> for i in range(1,70): <br /> <p> device.drag((250,850),(250,110),0.1,10)</p> <p> //开始,结束,持续时间,步骤 </p> for i in range(1,70): <br /> device.drag((250,110),(250,850),0.1,10) <br /> MonkeyRunner.sleep(1) <br /> <br /> 8. #触击屏幕; <br /> device.touch(507,72,"DOWN_AND_UP") <br /> <br /> 9. #执行adb shell命令; <br /> device.shell("input text goup01") <br /> <br /> 按下HOME键 device.press('KEYCODE_HOME','DOWN_AND_UP') <br /> 按下BACK键 device.press('KEYCODE_BACK','DOWN_AND_UP') <br /> 按下下导航键 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP') <br /> 按下上导航键 device.press('KEYCODE_DPAD_UP','DOWN_AND_UP') <br /> 按下OK键 <br /> <br /> device.press('KEYCODE_DPAD_CENTER','DOWN_AND_UP') <br /> <br /> <br /> 相应的按键对应的名称如下: <br /> <br /> home键:KEYCODE_HOME <br /> back键:KEYCODE_BACK <br /> send键:KEYCODE_CALL <br /> end键:KEYCODE_ENDCALL <br /> 上导航键:KEYCODE_DPAD_UP <br /> 下导航键:KEYCODE_DPAD_DOWN <br /> 左导航:KEYCODE_DPAD_LEFT <br /> 右导航键:KEYCODE_DPAD_RIGHT <br /> ok键:KEYCODE_DPAD_CENTER <br /> 上音量键:KEYCODE_VOLUME_UP <br /> 下音量键:KEYCODE_VOLUME_DOWN <br /> power键:KEYCODE_POWER <br /> camera键:KEYCODE_CAMERA <br /> menu键:KEYCODE_MENU