首页
JAVA笔记
数据库笔记
混乱记忆
小站记
您现在的位置
android monkeyrunner 超简单入手实例
简介: 节省人力成本是我们的目标,在软件开发世界中自动化测试方案的运用给我们带来惊喜。以前做web 开发接触到 Selenium,android app 的自动化测试我们从monkeyrunner 入手

monkeyrunner 历史和用途我在这里就不介绍了,只强调一点,他是基于 Jpython的实现。

monkeyrunner 看似是一个很NB,高端大气上档次的东西,实际上只是一个小朋友的玩具,入手很简单,实际上除了入手简单之外还有就是真正运用起来却非常复杂,先下个定义(烂语法烂工具)。说入手简单是因为他主要包含3 个类 MonkeyRunner,MonkeyDevice,MonkeyImage。大约不到20个API,常用API不大于10个,还有更简单的么?说它烂是因为竟然是 python 烂语法,而且常用api api 顶多也就支持 单击下谋个坐标,从谋坐标拖拽到谋坐标的简单功能,等。。。但是基于这些api 根本就不能满足我们执行单击或者拖拽之后的验证工作,虽然他可以使用MonkeyImage 来做 屏幕区域的图形对比,但是却不能读本地的图片,而且 sameAs 也是一个蹩脚的API。

虽然它可以扩展,因为是 python 的嘛,但是基本找不到文档。除非以前是一个 python 高手,否则很难玩转起来。

 

好了废话说了那么多讲点实际的,推荐先看官方文档

http://developer.android.com/tools/help/monkeyrunner_concepts.html
然后在看看我下面的这个实例就可以正常入手了。

(这个脚本是我专门为刷腾讯的全民飞机大战这个游戏中的金币而写的,呵呵,)

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
device=MonkeyRunner.waitForConnection()
#device.startActivity(component="com.example.android.apis/.ApiDemos")
#MonkeyRunner.sleep(3)
#result = device.takeSnapshot()
#result.writeToFile('d:\\aaabbbccc','png')


class MyImg:
    def __init__(self,device,rect):
        self.device = device
        self.rect = rect
        result = device.takeSnapshot()
        self.image = result.getSubImage(rect)
    def getImage(self):
        return self.image
    def compare(self,percent):
        result = device.takeSnapshot()
        result = result.getSubImage(self.rect)
        return self.image.sameAs(result,percent)
    def compareAtrect(self,des,percent):
        result = self.device.takeSnapshot()
        result = result.getSubImage(des)
        return self.image.sameAs(result,percent)   
    def writeToFile(self,des,type):
        self.image.writeToFile(des,type)

flag = True
step = 0
step1 =None
step2 =None
step3 =None
step4 =None
step5 =None
count =1

while True:
    if step ==0:
        device.shell("am force-stop com.tencent.feiji")
        MonkeyRunner.sleep(1)
        device.shell("am start -n com.tencent.feiji/.MainActivity")
        MonkeyRunner.sleep(20)
        step = 1
    if step ==1:
        print "at the step1"
        if step1 is None:
            step1 = MyImg(device,(160,690,72,72))
            step1.writeToFile('d:\\feiji\step_1.png','png')
        if step1.compare(0.6):
            device.touch(350,850,MonkeyDevice.DOWN_AND_UP)
            step =2
            MonkeyRunner.sleep(3)
        else:
            flag = False
               
    if step ==2:
        print "at the step2"
        if step2 is None:
            step2 = MyImg(device,(230,750,30,25))
            step2.writeToFile('d:\\feiji\step_2.png','png')
        if step2.compare(0.6):
            device.touch(350,850,MonkeyDevice.DOWN_AND_UP)
            step =3
            MonkeyRunner.sleep(3)
        else:
            flag = False
    if step ==3:
        print "at the step3"
        if step3 is None:
            step3 = MyImg(device,(132,809,40,40))
            step3.writeToFile('d:\\feiji\step_3.png','png')
        if step3.compare(0.6):
            device.touch(350,850,MonkeyDevice.DOWN_AND_UP)
            step =4
            MonkeyRunner.sleep(10)
        else:
            flag = False
    if step ==4:
        print "at the step4"
        MonkeyRunner.sleep(5)
        if step4 is None:
            step4 = MyImg(device,(500,90,5,10))
            step4.writeToFile('d:\\feiji\step_4.png','png')
        if step4.compare(0.6):
            count = 1
            device.drag((500,40),(40,920),0.1,83)
 
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)
            device.drag((40,40),(500,40),0.1,83)
            device.drag((500,40),(40,40),0.1,83)

            device.drag((40,920),(210,850),0.1,83)
            while step4.compare(0.6):
                MonkeyRunner.sleep(3)
            step = 5
            MonkeyRunner.sleep(15)
        else:
            flag = False
    if step ==5:
        print "at the step5"
        if step5 is None:
            step5 = MyImg(device,(30,808,40,40))
            step5.writeToFile('d:\\feiji\step_5.png','png')
        if step5.compare(0.6):
            device.touch(60,850,MonkeyDevice.DOWN_AND_UP)
            step = 1
            MonkeyRunner.sleep(3)
        else:
            flag = False
    if flag == False:
        step=0
        MonkeyRunner.sleep(60)
       

 

如何运行上面的脚本

    1. 在sdk_home\tools目录下创建一个text文本,将内容拷贝进去并命名为 feiji.py
    2. 手机连接PC并启动全民飞机大战
    3. 开启 cmd 终端,cd 进入到 sdk_home\tools目录,输入monkeyrunner feiji.py 后回车
    4. 观看循环自动飞机刷金币画面

 

最近更新