Android测试-- MonkeyRunner

来源:互联网 发布:淘宝代购链接怎么做 编辑:程序博客网 时间:2024/06/11 07:19

monkeyRunner

功能:安装应用,测试安装包,运行应用,发送指令,截图并保存

在功能/框架层面 测试应用、设备,运行单元测试,但是并不限制你应用于其他方面

可以通过一套或多套测试,同时控制一个或多个设备。

monkeyrunner是一个API工具,使用Jython(使用Java语言实现的Python,可以轻松对接Android框架,又可以使用python的语法)支持。所以你可以拓展基于Python的module和程序来更好的操作设备。通过使用monkeyrunner API,可以使用 Python os 和subprocess modules来调用ADB
(Python table of content os在16.1,subprocess在17.5 )

使用示例

下文中有提到传入x,y值,请配合uiautomatorviewer(参看总览)使用。

官方示例

//使用示例from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice# 需要先连接设备,然后此方法会自己获取实例device = MonkeyRunner.waitForConnection()# 会返回一个boolean值device.installPackage('myproject/bin/MyApplication.apk')#package = 'com.example.android.myapplication'activity = 'com.example.android.myapplication.MainActivity'runComponent = package + '/' + activitydevice.startActivity(component=runComponent)# 最好休眠一下,不然点击经常没反应MonkeyRunner.sleep(3)device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)MonkeyRunner.sleep(3)device.touch(400,81,MonkeyDevice.DOWN_AND_UP)# 滑动条,对touch的downAndUp不感冒,但是可以用移动事件处理# 还能看到丝般不顺滑device.touch(875, 420, MonkeyDevice.DOWN)for i in range(1,10):    device.touch(875+5*i, 420, MonkeyDevice.MOVE)    MonkeyRunner.sleep(0.1)device.touch(925,420,MonkeyDevice.UP)# 截图,这是个MonekyImage对象result = device.takeSnapshot()result.writeToFile('myproject/shot1.png','png')

触摸参考:https://gist.github.com/sjp38/6202539
API:
MonkeyRunner;MonkeyDevice;MonkeyImage

monkeyRunner程序位于sdk/tools/bin下,

# 运行*.py文件方式# -plugin在每个插件包前都要添加,file为python文件,option是输入参数monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>

帮助文档

monkeyrunner help.py <format> <outfile> //format为text或html,outfile是路径

一试发现,文件没有。。(讲道理为什么要试了才发现。。)
搜了一下发现有两种解法:
一个是简化版:

#!/usr/bin/env python# Imports the monkeyrunner modules used by this programfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDevicetext = MonkeyRunner.help("html");f = open('help.html', 'w')f.write(text);f.close();

还有一个就是官方版了
但是两个跑起来试了一下,都会报错:

...com.google.clearsilver.jsilver.exceptions.JSilverTemplateNotFoundException: com.google.clearsilver.jsilver.exceptions.JSilverTemplateNotFoundException: No class loader resource 'html.cs' in 'com/android/monkeyrunner'...

如果format写成text,报错也是对应text.cs 。。

搜来搜去都没有结果,仔细一看,就一个类似的问题,还没人回答;评论里也有说会报错的。
会不会是monkeyRunner被抛弃了??
怎么可能,官网还有文档啊,想想有些没抛弃的官方文档都已经拿掉了。。

那该怎么破???

我们回过头来,为什么4.1.2的sdk里有这个文件,而文件可以编译?咳咳,说错了。为什么4.1.2之后的sdk里没有这个文件,而文件可以编译?
翻了一下,还真在tools/lib 目录下翻到了monkeyrunner.jar。
好了,真相就在眼前,我们只需要看一眼。怎么看呢,下个JD-GUI吧。打开之后,一下子就看到了我们的目标文件:
这里写图片描述

对比一下错误提示,好像包名不对。。那我是不是要给他重新移动一下?
这个简单,直接解压软件把下面三个解压出来,移到上面就好了。
重新运行一下,Ok!
效果预览图:
这里写图片描述

这里写图片描述

深入一些的示例

(想想上面的帮助文档,就对这个的文档没啥要求了,自己看代码吧—或者网上也很多)
其实就是By 和 EasyMonkeyDevice,还是刚刚那个连接—-最新的monkeyrunner代码
相信聪明的你看了一些文章就能明白,EasyMonkeyDevice其实就是对MonkeyDevice的部分方法进行转换,让我们可以直接使用id来定位。

#直接用README了from com.android.monkeyrunner import MonkeyRunner, MonkeyDevicefrom com.android.monkeyrunner.easy import EasyMonkeyDevicefrom com.android.monkeyrunner.easy import By# Connect to the current device.device = MonkeyRunner.waitForConnection()# Use the EasyMonkey API, all methods on device are available in easy_device.easy_device = EasyMonkeyDevice(device)if not easy_device.visible(By.id('id/all_apps_button')):    raise Error('Could not find the "all apps" button')print "Location of element:", easy_device.locate(By.id('id/all_apps_button'))easy_device.touch(By.id('id/all_apps_button'), 'DOWN_AND_UP')

自定义插件

自定义拓展插件请看原文最下方