android MonkeyRunner的使用

monkeyRunner的环境搭建和基本概念我就不都介绍了,如果你对此不了解的请出门左拐自行谷歌。重点讲解如何使用。

脚本文件的创建

打开你sdk安装目录下的tools文件夹下,比如:
D:\android\sdk\tools
创建一个名为Touch.py的文件,如下图所示:
这里写图片描述

脚本文件的编写

我们就以360手机助手为例。首先我们要先获取到360手机助手的应用包名和启动Activity。如何获取这两个呢?反编译啊这么简单的还问对不对(有谁不知道的告诉我,我不会笑你的,哈哈哈,说好的不笑呢)好了,至于反编译应该不用我都说啥了,我想各个都是高手。

进到apktool.bat 所在的目录下,把下载好的360手机助手的apk包也放在这个路径下,打开命令行工具,进到当前在这个目录下,执行apktool.bat d 360.apk 如下图所示:
这里写图片描述

反编译完成后就可以看一个360的文件夹
这里写图片描述

在这个目录下会自动生成一个360的文件夹,打开这个文件夹就可以看到Mainifest.xml文件了找到应用包名和启动activity,拿到这两个之后就可以开干了。

# -*- coding: utf-8 -*-

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.monkeyrunner.easy import EasyMonkeyDevice, By 

device = MonkeyRunner.waitForConnection()
easyMonkey = EasyMonkeyDevice(device)  

package = 'com.qihoo.appstore'
activity = 'com.qihoo.appstore.home.LauncherActivity'

runComponent = package + '/' + activity

# Runs the component
device.startActivity(component=runComponent)

# 等待几秒钟
MonkeyRunner.sleep(3)

运行脚本

打开命令行,进入到你的tools目录下,执行当前这个脚本。
这里写图片描述
当你看到应用启动成功就说明这个脚本编写成功了。

基本语法

模拟点击

device = MonkeyRunner.waitForConnection()
device.touch(300,250,MonkeyDevice.DOWN_AND_UP) 

300:代表在屏幕上的x坐标

250:代表在屏幕上的y坐标

MonkeyDevice.DOWN_AND_UP : 代表点击事件 点击还有DOWN、UP这两个事件

device.press('KEYCODE_BACK')
press (string name, integer type)

name:这个代表模拟点击手机的物理按键,如back、menu、音量+、音量- 等等 具体的可以参考http://developer.android.com/intl/zh-cn/reference/android/view/KeyEvent.html(需要自备梯子)
type:MonkeyDevice的DOWN_AND_UP、DOWN、UP这三个事件

扫描二维码关注公众号,回复: 5710914 查看本文章

滑动

device.drag ( tuple start, tuple end, float duration, integer steps)
device.drag((250,850),(250,110),0.5,10)

start 起始坐标

end 结束坐标

duration 执行时间 默认为1秒

steps 步长 默认为10. 设置为1的话 可以实现滑屏(新手引导页面中就可以使用了)

输入文字

device.type('91')

91:代表你想输入的文字

获取手机屏幕的宽高

width = int(device.getProperty("display.width").encode('utf-8'))
height = int(device.getProperty("display.height").encode('utf-8'))

display.width 代表屏幕的宽,还有其他的参数可以参考http://developer.android.com/intl/zh-cn/tools/help/MonkeyDevice.html#table1(需要自备梯子)

通过ID获取控件

viewer = device.getHierarchyViewer()
searchView = viewer.findViewById('id/btn_search') 

注意:但很多手机上view server不工作,即使被rooted了,也就是活getHierarchyViewer()获取不到对象(所以推荐使用模拟器),后面的参考文献中有这方面的blog介绍,你们也可以去研究下。

ID可以通过下图的工具中获取,具体的可以自行百度查看下如何使用。
这里写图片描述

另一种方式通过EasyMonkeyDevice:

easyMonkey = EasyMonkeyDevice(device) 
download = By.id('id/download_tv')
easyMonkey.touch(download,MonkeyDevice.DOWN_AND_UP) 

获取listView的item

def getSelectListViewItem(viewer,device,index,viewId):
    contentView = viewer.findViewById(viewId)

    listView = contentView.parent
    childs = listView.children

    itemView = childs[index].children

    return itemView

这是以360应用首页为例子的python脚本

整体的运用

# -*- coding: utf-8 -*-

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.monkeyrunner.easy import EasyMonkeyDevice, By 

device = MonkeyRunner.waitForConnection()
easyMonkey = EasyMonkeyDevice(device)  

package = 'com.qihoo.appstore'
activity = 'com.qihoo.appstore.home.LauncherActivity'

runComponent = package + '/' + activity

# Runs the component
device.startActivity(component=runComponent)

# 等待几秒钟
MonkeyRunner.sleep(3)

# 滑动 
device.drag((250,850),(250,110),0.5,10)

#输入文字
device.touch(300,200,MonkeyDevice.DOWN_AND_UP) # 这个坐标要自己调整
MonkeyRunner.sleep(3) # 等待3秒
device.type('91')

# 按回车键
MonkeyRunner.sleep(1)
device.press('KEYCODE_ENTER')

# 回退到主界面
MonkeyRunner.sleep(1)
device.press('KEYCODE_BACK')

# 获取listView某一项的item
viewer = device.getHierarchyViewer()
contentView = viewer.findViewById('id/home_title')

listView = contentView.parent
childs = listView.children

itemView = childs[2].children

#获取当前item上的文字
relayout = itemView[0]
textView = viewer.findViewById('id/common_list_name',relayout)
text = viewer.getText(textView).encode('utf-8')
print text

参考文献

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

http://blog.csdn.net/jl_qiqi/article/details/8658515

https://www.dup2.org/comment/6246

http://www.2cto.com/kf/201411/356056.html

http://blog.csdn.net/vshuang/article/details/39783579

http://www.bubuko.com/infodetail-11124.html

http://www.51testing.com/html/47/47-810274.html

http://www.tuicool.com/articles/JfEVr2

http://www.51testing.com/html/36/n-848536.html

感谢他们的无私奉献!

猜你喜欢

转载自blog.csdn.net/androidWorkor/article/details/51149832