MonkeyRunner相关API使用

MonkeyRunner相关API使用

首先推荐几篇博文,罗列了很多API使用:

http://blog.csdn.net/zhubaitian/article/details/39933609?utm_source=tuicool&utm_medium=referral
http://blog.csdn.net/zhubaitian/article/details/39926209

本文用一个常用的app登陆流程:

  1. 安装APP
  2. 打开APP
  3. 判断是登陆界面还是导航界面
  4. 处理导航界面逻辑
  5. 处理登陆界面逻辑
  6. 判断是否登陆成功

直接上代码,使用方法请看注释

#!/usr/bin/python
# -*- coding:utf-8 -*-

#脚本准备工作,下面不再注释,可以看print的内容,此内容会打印在控制台
#此脚本适合三星note4 分辨率2560 * 1440
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
print "start MonkeyRunner and wait for devices"
device = MonkeyRunner.waitForConnection()
print "get device success"
print "start to install package"
#为了方便我把APK放在Android SDK的tools目录下
#device.installPackage('./HelloWorld.apk')
print "install package success"

#包名
package = 'com.test.hello'
#需要打开的Activity,前面要加上包名
lanuchActivity = 'com.test.hello.activity.GuideActivity'

#tempWidth = 1440
#tempHeight = 2560
#获取屏幕的高度宽度,密度比,如果执行失败,在执行一次
displayWidth = float(device.getProperty ('display.width'))
displayHeight = float(device.getProperty ('display.height'))
displayDensity = float(device.getProperty ('display.density'))
print "displayWidth:",displayWidth
print "displayHeight:",displayHeight
print "displayDensity:",displayDensity

#启动APK
def startLanuchActivity():
    runComponent = package + '/' + lanuchActivity
    print "run the lanuchActivity"
    device.startActivity(component=runComponent)
    #MonkeyRunner.alert("click OK and start the TEST","notice:","OK")
    print "lanuchActivity is lanuched and wait 4 s"
    MonkeyRunner.sleep(4)
    print "start the TEST for HelloWorld"
    return

#适配不适用所有机型,放弃
#def device.touch(x,y,touch):
    #device.touch(int(x*displayWidth/tempWidth),int(y*displayHeight/tempHeight),touch)
    #return 

#左侧滑功能
def myDrag():
    device.drag((int(displayWidth*3/4), int(displayHeight/2)),(int(displayWidth/2), int(displayHeight/2)),0.1,40)
    return

def handlerWelcomeActivity():
    print "start to handler welcome activity"
    #功能:拖拽      参数:开始,结束,时间,步骤  注意:时间少,步数多才有滑动的效果
    myDrag()
    MonkeyRunner.sleep(1)
    myDrag()
    MonkeyRunner.sleep(1)
    myDrag()

    MonkeyRunner.sleep(1)
    device.touch(780,2140,MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(4)
    print "handler welcome activity end"
    return

def handlerLoginActivity():
    print "start to handler login activity"
    device.touch(440,1640,MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(1)
    device.touch(688,935,MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(1)
    device.type('9901')
    MonkeyRunner.sleep(1)
    device.touch(688,1140,MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(1)
    device.type('ytt9901')
    MonkeyRunner.sleep(1)
    device.touch(1388,1488,MonkeyDevice.DOWN_AND_UP)

    MonkeyRunner.sleep(1)
    device.touch(700,1540,MonkeyDevice.DOWN_AND_UP)
    print "handler login activity end"
    return

#判断是欢迎界面还是登陆界面
#先截图,然后点底部第二个按钮,在截图,对比2张图
#如果一样就是欢迎界面,不一样是登陆界面
def judgeIsWelcomeActiviyt():
    print "take the welcomPic1.png for judge whether it is welcome activity"
    welcomPic1 = device.takeSnapshot()  
    welcomPic1 = welcomPic1.getSubImage((200,200,500,500))  
    #welcomPic1.writeToFile('./welcomPic1.png','png') 

    #点击欢迎界面底部第二个按钮
    device.touch(570,2425,MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(1)

    print "take the welcomPic2.png for judge whether it is welcome activity"
    welcomPic2 = device.takeSnapshot()  
    welcomPic2 = welcomPic2.getSubImage((200,200,500,500))  
    #welcomPic2.writeToFile('./welcomPic2.png','png')
    #返回对比的结果
    return welcomPic2.sameAs(welcomPic1,1.0)

def judgeIsLoginSuccess():
    print "take the loginPic1.png for judge whether it is login success"
    loginPic1 = device.takeSnapshot()  
    loginPic1 = loginPic1.getSubImage((200,200,500,500)) 

    MonkeyRunner.sleep(3)

    print "take the loginPic2.png for judge whether it is login success"
    loginPic2 = device.takeSnapshot()  
    loginPic2 = loginPic2.getSubImage((200,200,500,500))
    return loginPic1.sameAs(loginPic2,1.0)

def exitApp():
    #按多次返回键退出app,因为不知道在哪一步了,所以就多按几次
    print "exit app and end the TEST "
    device.press('KEYCODE_BACK','DOWN_AND_UP')
    MonkeyRunner.sleep(1)
    device.press('KEYCODE_BACK','DOWN_AND_UP')
    MonkeyRunner.sleep(1)
    device.press('KEYCODE_BACK','DOWN_AND_UP')
    MonkeyRunner.sleep(1)
    device.press('KEYCODE_BACK','DOWN_AND_UP')
    MonkeyRunner.sleep(1)
    device.press('KEYCODE_BACK','DOWN_AND_UP')
    MonkeyRunner.sleep(1)
    device.press('KEYCODE_BACK','DOWN_AND_UP')
    return

def exitFunction(start,end):
    MonkeyRunner.sleep(start)
    device.touch(108,208,MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(end)
    return 

def runMain():
    exitApp()
    startLanuchActivity()
    #如果相等,说明是登陆界面,如果不相等就是欢迎界面
    if judgeIsWelcomeActiviyt():
        print "welcomPic1 is equal welcomPic2"
    else:
        print "welcomPic1 is not equal welcomPic2"
        handlerWelcomeActivity()

    handlerLoginActivity()

    if judgeIsLoginSuccess():
        print "login failed"
    else:
        print "login success"


    #这里可以执行测试主界面的功能


    exitApp()
    return

x = 0
#执行次数
sumCount = 10
while x < sumCount:
    print "runMain:",x
    runMain()
    x = x + 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值