Monkeyrunner

android开发免不了要用到android sdk tools下的一些开发工具

,你可以在系统环境变量里面配置一个ANDROID_HOME变量,把你的android SDK 安装路径加进去。比如我的路径是:C:\Users\curryl\adt-bundle-windows-x86_64-20140702\sdk。如果你已经配过了ANDROID_HOME变量,你现在只需要在你的系统变量的PATH变量中,把这句话加进去:%ANDROID_HOME%/tools;其实也就是相当于你把指向到android sdk tools的路径加到PATH变量中。

C:\Users\curryl\adt-bundle-windows-x86_64-20140702\sdk\tools

然后随便打开一个cmd命令行窗口,     运行:monkeyrunner 命令,回车。如果出现monkeyrunner的版本信息,就证明你monkeyrunner环境已经配成功了。(如下图)

这时候你需要打开一个模拟器或者连接你的真实设备到电脑上。

 1.模拟器:  用Elipse打开andorid的模拟器或者在CMD中用andorid命令打开模拟器,如下:

  C:\Users\curryl\adt-bundle-windows-x86_64-20140702\sdk\tools>emulator -avd AVD2.3.3

  Comments: 定位到android SDK的路径下的tools目录,运行上面这个命令。“AVD2.3.3”是我的模拟器的名字,这个命令是每个人自己命名的,用的时候需改成实际的名字。

2.实际设备:​随便打开一个cmd,运行adb devices,  就能获得设备号

C:\Users\curryl>adb devices

List of devices attached

05131138014091062F69    device

使用:

在输入 monkeyrunner之后出现shell 交互界面,

输入类似下面的 语句或(当然,我们也可以不这样麻烦得一行行地输入命令,可以将从“from...import...”开始的所有后面的语句都写到一个python文件里面,例如 monkeyrunnerTest.py,然后我们再从命令行直接通过monkeyrunner运行它即可。比如,我们还是用上面的例子,语法如下:

  C:\Users\curryl\adt-bundle-windows-x86_64-20140702\sdk\tools>monkeyrunner monkeyrunnerTest.py

  接下来monkeyrunner会自动调用monkeyrunnerTest.py,并执行其中的语句,相当方便。

[python]  view plain copy print ?
  1. Imports the monkeyrunner modules used by this program  
  2. from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice  
  3.    
  4. Connects to the current device, returning MonkeyDevice object  
  5. device MonkeyRunner.waitForConnection()  
  6.    
  7. Installs the Android package. Notice that this method returns boolean,  
  8. so you can test to see if the installation worked.  
  9. device.installPackage('myproject/bin/MyApplication.apk' 
  10.    
  11. sets variable with the package's internal name  
  12. package 'com.example.android.myapplication'  
  13.    
  14. sets variable with the name of an Activity in the package  
  15. activity 'com.example.android.myapplication.MainActivity'  
  16.    
  17. sets the name of the component to start  
  18. runComponent package '/' activity  
  19.    
  20. Runs the component  
  21. device.startActivity(component=runComponent)  
  22.    
  23. Presses the Menu button  
  24. device.press('KEYCODE_MENU','DOWN_AND_UP' 
  25.    
  26. Takes screenshot  
  27. result device.takeSnapshot()  
  28.    
  29. Writes the screenshot to file  
  30. result.writeToFile('myproject/shot1.png','png' 


如果是实际手机的话,可以输入刚刚查询到的设备号。

device=MonkeyRunner.waitForConnection(5,'05131138014091062F69')


C:\Users\curryl\adt-bundle-windows-x86_64-20140702\sdk\tools>monkeyrunner

Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:54:35)

[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_25

>>> from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

>>> device=MonkeyRunner.waitForConnection(5,'05131138014091062F69')

>>> device.press('KEYCODE_MENU','DOWN_AND_UP')



再看看录制回放功能:

详见  http://blog.csdn.net/mad1989/article/details/38087737

这个缺点是只能通过电脑上的屏幕操作,录制太慢了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值