Android自动化学习笔记之MonkeyRunner:官方介绍和简单实例

----------------------------------------------------------------------------------------------------------------------------

小记: 一直都是用公司自己研发的自动化工具,对市面开源的自动化工具知之甚少,所以开始自学开源的自动化工具。

初步学习中,难免会有疏漏和想不到的地方,随着不断深入的了解,可能会觉得现在的笔记很可笑,不过有新的体会,我会及时修改,不断进步。

2014-10-13:初版

2014-10-14:更新模拟器实例和常用命令

2014-10-16:更新2个真机实例

2014-10-17:添加Q&A

2014-10-23: 更新两种编写monkeyrunner脚本的第三方插件,另开一篇文章:Android自动化学习笔记:编写MonkeyRunner脚本的几种方式

------------------------------------------------------------------------------------------------------------------------------

下面来学习一下官方文档: (简介翻译来自网络)

一、什么是monkeyrunner

monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner,您可以写出一个Python程序去安装一个Android应用程序或测试包,运行它,向它发送模拟击键,截取它的用户界面图 片,并将截图存储于工作站上。monkeyrunner工具的主要设计目的是用于测试功能/框架水平上的应用程序和设备,或用于运行单元测试套件,但您当然也可以将其用于其它目的。

二、Monkey和MonkeyRunner

Monkey工具直接运行在设备或模拟器的adb shell中,生成用户或系统的伪随机事件流。

相比较之下,MonkeyRunner工具则是在工作站上通过API定义的特定命令和事件来控制设备或模拟器

三、MonkeyRunner的特色

  1. 多设备控制:monkeyrunner API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器(或统统一起),依据程序依次连接到每一个,然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器,运行一个或多个测试,然后关闭模拟器。

  2. 功能测试: monkeyrunner可以为一个应用自动贯彻一次功能测试。您提供按键或触摸事件的输入数值,然后观察输出结果的截屏。

  3. 回归测试:monkeyrunner可以运行某个应用,并将其结果截屏与既定已知正确的结果截屏相比较,以此测试应用的稳定性。

  4. 可扩展的自动化:由于monkeyrunner是一个API工具包,您可以基于Python模块和程序开发一整套系统,以此来控制Android设备。除 了使用monkeyrunner API之外,您还可以使用标准的Python os和subprocess模块来调用Android Debug Bridge这样的Android工具。

四、Monkeyrunner运行环境

无论怎么运行,都需要调用 SDK目录的tools子目录下的monkeyrunner命令。如果您提供一个文件名作为运行参数,则monkeyrunner将视文件内容为 Python程序,并加以运行;否则,它将提供一个交互对话环境。

monkeyrunner的命令语法为:

monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>

五、官网实例(网上的资料都没有提及,运行脚本之前,必须先把连接到电脑上或者启动模拟器)
官网的例子,不解释。

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice # Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection() # Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
device.installPackage('myproject/bin/MyApplication.apk') # sets a variable with the package's internal name
package = 'com.example.android.myapplication' # sets a variable with the name of an Activity in the package
activity = 'com.example.android.myapplication.MainActivity' # sets the name of the component to start
runComponent = package + '/' + activity # Runs the component
device.startActivity(component=runComponent) # Presses the Menu button
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP) # Takes a screenshot
result = device.takeSnapshot() # Writes the screenshot to a file
result.writeToFile('myproject/shot1.png','png')

六、实例二(模拟器4.4.2测试通过):

步骤:截屏-->滑屏-->截屏
目的:学习截屏方法的使用

'''
Created on Oct 14, 2014 @author: deldong
'''
print 'Before execution' # Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner # Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection() # Takes a screenshot
result = device.takeSnapshot() # Writes the screenshot to a file
result.writeToFile('./beforeDrag.png','png')
#多嘴一句,这里的截图,其实是存放在..sdk/tools下面,找了1天有木有······
#当然这里也可以写绝对路径,比如c:\screenshot\beforeDrag.png #Drag screen from right to left
for i in range(0,3):
device.drag((600,200),(100,200),0.1,10)
MonkeyRunner.sleep(1) #Takes a screenshot
result = device.takeSnapshot()
#Writes the screenshot to a file
result.writeToFile('./afterDrag.png','png') print 'After execution'

七、实例三(诺基亚X2手机测试通过):

步骤安装APK-->打开APK-->关闭APK-->重复打开关闭操作N次-->卸载APK
目的
写个压力测试的脚本玩玩

'''
Created on Oct 16, 2014 @author: deldong
'''
# -*- coding: UTF-8 -*-
import sys#因为系统是英文系统,打汉字的话,必须用添加# -*- coding: UTF-8 -*-
reload(sys)
sys.setdefaultencoding("utf-8") # 导入所需的方法
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice #连接我的Nokia手机
print ("Connecting device.....")
NokiaDevice = MonkeyRunner.waitForConnection()
if not NokiaDevice:
print ("Please connect a device to start!")
else:
print ("Device Connected successfully!") #安装apk
try:
NokiaDevice.installPackage('C:/apps/MyFirstApp.apk')
print ("Package installed successfully! ")
except:
print ("Package installation failed!") #设置包名
package = 'com.example.myfirstapp' #设置activity名
activity = 'com.example.myfirstapp.MainActivity' #循环执行四次
for i in range(1,5):
#打开已安装的应用
print ("Open installed package for the %d time"%i)
NokiaDevice.startActivity(component=package + '/' + activity)
#等待5秒
print ("wait 5 sec")
MonkeyRunner.sleep(5)
#关闭应用
print("Quit the application")
NokiaDevice.press("KEYCODE_BACK",NokiaDevice.DOWN_AND_UP)
print ("wait 5 sec")
MonkeyRunner.sleep(5) #卸载APK
try:
print("Uninstall package!")
NokiaDevice.removePackage("com.example.myfirstapp")
print("Uninstall package successfully")
except:
print ("Package unintall failed!")

cmd提示:

aaarticlea/png;base64,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cmpicuvr1x5MH31t7YbXDi6O2fiLo/WD0LE8st754ODLGycmp558Jxz26Gtr55K5dvYXG5wIi7c+mL724PVn3FBuO/uLDXPDpLYQajjvB09ueO3gtbO/2LD19cVtEVfq3T1/wsIUjV1q5Na89eDLG9e+fHbuv74Bwa0L35ve/VzsOfjyxolnPgjuXsIs/kfdQvMEfDD9ztbF/fEOoNFoI2zP7n7r/I0Hh8/d9rZVq9ddDVu1el3oxAs3Hjy7+63hFBduPFiolM0WnSJ04vnaFHMl+diFu0cv3D1Wa0cv3D124c7W3fvqs9SPq08+eyj8v+R6hZ472Lln7yefPVxs72yb2PDa+4s953ZsmHrqnYeffPbBU5NzBw/ff3njxOTGHcfmOucOQsfuQahzPvLal88Fww5zO/baWn/Y+Qw/Ofba2sltr4dOb6x3eNZnDz859tqOd5yRx15b+8wHixlumEvy4fsvb3zqmW1zx4qVenfPTViYorFLzdyatx57be2GbU9tWLLD4U2eWjpL9J51HjDe7V2S4bkdG6yzyI86b9rbXp//r3cAjUYbaXt291uXbj46trSSDduq1esuhq1avS504qWbj57d/daIpzh+afb4pc+PX/z840ufH784e/zi7MeXZj++NFufYtXqddte2DfsqWauP7L+xfXM9UfD9vufTq399bl6zwe//snET/86c/3RzDvb5p43r/31609N/mTHsUcz1/+6cBA6dg98nQuRJya3/X4hjYnJqYmf/nXpied2bJiamJya2LDtqQ3znbWRj2aOvb52Po4vq3e2TWx4/YPa0ubb4llT8wOaI8/tWJhu5tjrayd9x5GVenfPm3B4CmeXluTmu/X3P13YGd/pga1zt909XrgjFqPVe6YmJqeeese3e9f/+tSSAdHHT+xRtyTt13dsmH/0fvDrn8zfg811Ofc+jUbrsj2358Clm4+OXfjc27bu2Sf8v+Ste/aFTrx089Fzew6MeIoTn96rtdkTn86e+PTex5/e2/7C/mHAbS/sP1Hrqc7c+MJaks/c+IIWaf+2fe2vz/efhjG3//3Tqaf+re/0Ct89Go3WZXtuz4HLt7888en9vO3y7S+f23NgxFOcvHx/oT2oHd8/efn+qSU9D+YHXLlfnb35pfUzrs/e/JK2AtvHv1+74fd/6z0NGo02xm3LC3+8cvfr01cf5m1X7n695YU/jniKU1cenLry8NSVh6euPFhoD09dfXB6rufqw7kfa7c+qM7d+oo29u3irv85NTH5k10nes+ERqONddvz23ePTF+7fu/bvO3I9LU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1bAAAAAElFTkSuQmCC" alt="" width="624" height="380" />

八、实例四(诺基亚X2手机测试通过):

步骤重启手机-->唤醒手机-->输入密码解锁
目的
使用重启和唤醒的方法

# -*- coding: UTF-8 -*-
'''
Created on Oct 16, 2014 @author: deldong
'''
import sys
reload(sys)
sys.setdefaultencoding("utf-8") # 导入所需的方法
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice #连接我的Nokia手机
print ("Connecting device.....")
NokiaDevice = MonkeyRunner.waitForConnection()
if not NokiaDevice:
print ("Please connect a device to start!")
else:
print ("Device Connected successfully!") #重启设备
print("Reboot device!")
NokiaDevice.reboot()
MonkeyRunner.sleep(60) #唤醒设备
print("Wake device")
NokiaDevice.wake()
print("wait 2s")
MonkeyRunner.sleep(2) #解锁 输入密码
print("unlock device....")
NokiaDevice.drag((400,700),(400,300),0.1,10)
MonkeyRunner.sleep(5)
NokiaDevice.type('')
print("unlock successfully")

Q&A:

Q:在cmd中执行monkeyrunner c:\test.py脚本时报错
提示:141017 14:35:59.101:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]SyntaxError: Non-ASCII character in file 'C:\calculator.py', but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
解决办法:在脚本的第一行添加中文的注释语句代码:# -*- coding: UTF-8 -*-
如果还不行的话,多添加下面三行代码即可。
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

九、常用命令:

1.安装Android包,此方法返回的返回值为boolean,由此可以判断安装过程是否正常
device.installPackage('文件名')
#参数是相对或绝对APK路径
路径级别用“/”,不能用“\”,比如d:\www\a.apk,而应该写成d:/www/a.apk
安装成功返回true,此时查看模拟器我们可以看到安装的APK的图标了。

2.从设备中删除指定的软件包,此方法返回的返回值为boolean,删除其相关的数据和调整缓存
device.removePackage ('包名')

3.启动一个Activity,两种方式
device.startActivity (component='包名/包名.Activity名')
device.startActivity (component='包名/.Activity名')

4.暂停(秒);
MonkeyRunner.sleep(5)

5.截图
result=device.takeSnapshot()
#此方法返回一个MonkeyImage对象(点阵图包装),我们可以用以下命令将图保存到文件
#result.writeToFile('takeSnapshot\\result1.png','png')
#MonkeyImage.writeToFile(参数1:输出文件名,也可以包括路径,参数2:目标格式)
#写成功返回true,否则返回false。
6.触击屏幕

在指定位置发送触摸事件(x,y的单位为像素)

device.touch(x,y,TouchPressType-触摸事件类型)
device.touch(100,100,"DOWN_AND_UP")

发送到指定键的一个关键事件
device.press(参数1:键码,参数2:触摸事件类型)
参数1:见android.view.KeyEvent URL:http://developer.android.com/reference/android/view/KeyEvent.html
参数2,如有TouchPressType()返回的类型-触摸事件类型,有三种。
a、DOWN 发送一个DOWN事件。指定DOWN事件类型发送到设备,对应的按一个键或触摸屏幕上。
b、UP 发送一个UP事件。指定UP事件类型发送到设备,对应释放一个键或从屏幕上抬起。
c、DOWN_AND_UP 发送一个DOWN事件,然后一个UP事件。对应于输入键或点击屏幕。

7.发送按键指令
按下HOME键 device.press('KEYCODE_HOME','DOWN_AND_UP')
按下BACK键 device.press('KEYCODE_BACK','DOWN_AND_UP')
按下下导航键 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
按下上导航键 device.press('KEYCODE_DPAD_UP','DOWN_AND_UP')
按下OK键 device.press('KEYCODE_DPAD_CENTER','DOWN_AND_UP')

相应的按键对应的名称如下:

home键:KEYCODE_HOME
back键:KEYCODE_BACK
send键:KEYCODE_CALL
end键:KEYCODE_ENDCALL
上导航键:KEYCODE_DPAD_UP
下导航键:KEYCODE_DPAD_DOWN
左导航:KEYCODE_DPAD_LEFT
右导航键:KEYCODE_DPAD_RIGHT
ok键:KEYCODE_DPAD_CENTER
上音量键:KEYCODE_VOLUME_UP
下音量键:KEYCODE_VOLUME_DOWN
power键:KEYCODE_POWER
camera键:KEYCODE_CAMERA
menu键:KEYCODE_MENU

八、参考资料:

Google官方文档:
http://developer.android.com/tools/help/monkeyrunner_concepts.html

网络资料:
http://www.cnblogs.com/yyangblog/archive/2011/03/10/1980086.html
http://blog.csdn.net/shy871265996/article/details/9716723

上一篇:innodb对update的处理


下一篇:转 TCP中的序号和确认号