Android Monkeyrunner Test
2012-07-12       0  个评论      
收藏     我要投稿

    关于Android自动化测试,研究了Monkey,Robotium 这次来看下 Monkeyrunner.
 
这里简单记录下实践过程,Monkeyrunner 需要用Python来编写,对于曾未学过Python的童鞋来说也没关系,因为Python属于比较好学的一门脚本语言.笔者也未曾学过Python,但有其他编程基础如:PHP,Java,Peal,还是能够很好理解Python的。
 
一、monkeyrunner 介绍
monkeyrunner 提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.
 
二、monkeyrunner 测试类型
多设备控制、功能测试、回归测试
 
三、实例(测试MyAndroid.apk)
1. 新建一个 monkeyrunnerTest.py
# Import the monkey runner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
 
# Connects to current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
 
# Installs the Android package
device.installPackage("./MyAndroid.apk")
 
# Runs the component
device.startActivity(component = 'com.luwenjie.android/.MyAndroidActivity')
 
#Presses the Menu button
device.press('KEYCODE_MENU','DOWN_AND_UP')
 
#Takes a screenshot
result = device.takeSnapshot()
 
# Writes the screenshot to a file
result.writeToFile('./shot1.png','png')
2. 运行在 %Android_HOME%\tools 目录下运行一下命令
monkeyrunner  monkeyrunnerTest.py

 
四、API参考
MonkeyRunner:http://developer.android.com/tools/help/MonkeyRunner.html
MonkeyDevice:http://developer.android.com/tools/help/MonkeyDevice.html
MonkeyImage:http://developer.android.com/tools/help/MonkeyImage.html
 
五、录制模式
#Usage: monkeyrunner recorder.py  
  
#recorder.py    
from com.android.monkeyrunner import MonkeyRunner as mr  
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder  
  
device = mr.waitForConnection()  
recorder.start(device)  
#END recorder.py  
#Press ExportAction to save recorded scrip to a file  
#Example of result:  
#PRESS|{'name':'MENU','type':'downAndUp',}  
#TOUCH|{'x':190,'y':195,'type':'downAndUp',}  
#TYPE|{'message':'',}  
  
 
#Usage: monkeyrunner playback.py "myscript"  
#playback.py   http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_playback.py  
import sys  
from com.android.monkeyrunner import MonkeyRunner  
  
# The format of the file we are parsing is very carfeully constructed.  
# Each line corresponds to a single command.  The line is split into 2  
# parts with a | character.  Text to the left of the pipe denotes  
# which command to run.  The text to the right of the pipe is a python  
# dictionary (it can be evaled into existence) that specifies the  
# arguments for the command.  In most cases, this directly maps to the  
# keyword argument dictionary that could be passed to the underlying  
# command.   
  
# Lookup table to map command strings to functions that implement that  
# command.  
CMD_MAP = {  
    'TOUCH': lambda dev, arg: dev.touch(**arg),  
    'DRAG': lambda dev, arg: dev.drag(**arg),  
    'PRESS': lambda dev, arg: dev.press(**arg),  
    'TYPE': lambda dev, arg: dev.type(**arg),  
    'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)  
    }  
 
 
# Process a single file for the specified device.  
def process_file(fp, device):  
    for line in fp:  
        (cmd, rest) = line.split('|')  
        try:  
            # Parse the pydict  
            rest = eval(rest)  
        except:  
            print 'unable to parse options'  
            continue  
  
        if cmd not in CMD_MAP:  
            print 'unknown command: ' + cmd  
            continue  
  
        CMD_MAP[cmd](device, rest)  
  
  
def main():  
    file = sys.argv[1]  
    fp = open(file, 'r')  
  
    device = MonkeyRunner.waitForConnection()  
      
    process_file(fp, device)  
    fp.close();  
      
  
if __name__ == '__main__':  
    main() 
 作者:luckyapplelwj

更多相关文章

  1. tcping测试服务器TCP端口
  2. Android(安卓)Fragment 申请权限 onRequestPermissionsResult 不
  3. Android(安卓)studio3.0上运行opencv3.2.0自带人脸识别实例Demo
  4. Android(安卓)Kotlin入门-控制流
  5. DELPHI XE5/6/7 android 无线真机调试
  6. Android让手机主动亮屏的代码
  7. service与Thread的区别
  8. Android(安卓)studio 下的robotium自动化测试和持续集成
  9. appium(一) 自动测试环境搭建

随机推荐

  1. XML加密和XML签名简介的具体介绍
  2. 详细介绍XML特殊字符的问题
  3. 详解根据xsd生成xml文档的示例代码分析
  4. 自定义XML动态配置程序的示例代码分享
  5. Schema验证XML概念的实例代码详解
  6. 详细介绍XML与JSON相互转换(图文)
  7. 详解XML命名空间(XML Namespaces)介绍以及
  8. 详解读写XML文件的代码案例
  9. xml约束技术之dtd的详解
  10. 使用CSS样式表格式化XML文档的详情介绍