MonkeyRunner脚本编写

1.      MonkeyRunner使用python脚本语言编写

2.      举例描述一下脚本编写

# -*-coding:utf-8 -*-     (允许注释使用中文,便于添加注释,#号标记此行为注释行)

from com.android.monkeyrunner importMonkeyRunner, MonkeyDevice, MonkeyImage

(调用MonkeyRunner,MonkeyDevice, MonkeyImage函数)

import time (调用时间函数,)

device= MonkeyRunner.waitForConnection()      (连接设备)

if not device:

   print "Please connect a device to start!"            (判断是否有设备连接)

else:

   print "start test"

x=1         (计数)

for i in range(1,2):          (设置循环次数,每条用例写一个循环用来压力测试)

    device.startActivity(component='com.gooagoo/.activity.MainActivity')   (启动购阿购程序)

   MonkeyRunner.sleep(3)      (设置每步操作间隔时间)

   device.touch(636,1232,"DOWN_AND_UP")     (根据坐标屏幕进行点击操作)

    MonkeyRunner.sleep(3)

   device.press("KEYCODE_BACK","DOWN_AND_UP")       (进行返回键操作)

   MonkeyRunner.sleep(3)

    device.press("KEYCODE_HOME","DOWN_AND_UP")       (进行home键操作)

   MonkeyRunner.sleep(5)

result=device.takeSnapshot()   (用例完成之后,对界面进行截图,方便查看手机是否出现问题)

 

   MonkeyRunner.sleep(5)

   result.writeToFile('D:/test/%s.png'%x,'png')    (截图保存到d:/test路径下,以x命名)

    x+= 1       (计数加1)

   print i        (打印当前为第几次循环)

print "End test" + " "+ time.strftime('%Y-%m-%d  %H:%M:%S')      (打印测试结束时的时间)

坐标获取

(1)      android4.0以上系统手机自带坐标功能,在设置开发者选项中,显示指针位置

(2)      使用SDK中recorder工具查看

通过编写一个python脚本实现,邮件附件中提供

脚本内容如下:

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice, MonkeyImage

from com.android.monkeyrunner.recorder import MonkeyRecorder asrecorder

device = MonkeyRunner.waitForConnection()

recorder.start(device)

(3)      运行方法

在CMD中输入Monkeyrunner加上文件路径即可,入下图

运行之后直接在PC端即可操作手机,并继续坐标



1.      脚本运行

(1)      直接在CMD中输入monkeyrunner后面加上脚本文件路径和名称即可

(2)      编写一个bat的批处理文件,直接双击后自动执行,附件中提供

脚本如下:

@echo off

echo 请保持USB连接

md"D:\call"

echo 文件夹自动创建完成

:loop

set /anum+=1

echo 开始执行第%num%轮 开始时间%time% %date%

callmonkeyrunner  10010.py     (文件名)

echo 第%num%轮测试完毕 结束时间%time% %date%

if"%num%"=="50000000000" goto end

goto loop

:end

echo 所有程序安装完毕 结束时间%time%%date%

echo 测试完成 按任意键退出

echo. & pause


版本编写解释:


fromcom.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

importtime

device1 =MonkeyRunner.waitForConnection(1,"74DC000600000001")

device2 =MonkeyRunner.waitForConnection(1,"4df782e56b07bf11")

devicelist=(device1,device2)

for i inrange(1,2):

    for j in range(0,2):

       devicelist[j].startActivity(component='com.gooagoo/.activity.MainActivity')

 

print "End test" + " " +time.strftime('%Y-%m-%d  %H:%M:%S')

 

执行bat语句,查看日志,可新打开一个cmd

输入:adb logcat >d:/logcat.txt



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值