很久之前就写好的了,准备写个自动执行Monkey的脚本时才想到去找它,还是写在博客里找起来方便。
这次更新了批处理自动连接设备后执行Py脚本,结构如下图:
其中shotscreen为存放截图文件夹,sms.py为Py脚本,StartTest.bat为获得设备ID和调用Py脚本。
Start.bat中的代码如下:
1 @echo off 2 rem 获取当前运行设备 3 adb devices > d:\SMSTest\devices.txt 4 rem 运行monkeyrunner 脚本 5 monkeyrunner d:\SMSTest\sms.py
sms.py中的代码和之前相比只是增加了读取devices.txt中的devices id,感觉方法不是很可靠,求大家支招。
1 #导入我们需要用到的包和类并且起别名 2 import sys,time,datetime 3 import re 4 from com.android.monkeyrunner import MonkeyRunner as mr 5 from com.android.monkeyrunner import MonkeyDevice as md 6 from com.android.monkeyrunner import MonkeyImage as mi 7 8 #读取设备ID 9 i = 0 10 str = '' 11 for line in open('D:\SMSTest\devices.txt'): 12 if i==1: 13 str = line 14 break 15 i = i+1 16 p = re.compile(r'\s') 17 lis = p.split(str) 18 device_id=lis[0] 19 #连接设备 20 device = mr.waitForConnection(1.0,device_id) 21 if not device: 22 print >> sys.stderr,"fail" 23 sys.exit(1) 24 #定义要启动的Activity 25 componentName='com.android.mms/.ui.BootActivity' 26 #启动特定的Activity 27 device.startActivity(component = componentName) 28 mr.sleep(1.0) 29 #do someting 进行我们的操作 30 #新建短信 31 device.touch(57,747,'DOWN_AND_UP') 32 mr.sleep(1.0) 33 #输入 10086 34 device.type('10010') 35 #发送短信条数 36 for n in range(0,3): 37 #输入短信内容 38 device.touch(187,402,'DOWN_AND_UP') 39 device.type('hellomoto') 40 mr.sleep(1.0) 41 #发送短信 42 device.touch(432,380,'DOWN_AND_UP') 43 mr.sleep(1.0) 44 device.touch(51,752,'DOWN_AND_UP') 45 mr.sleep(3.0) 46 #创建时间字符串 47 t = time.strftime("%Y-%m-%d-%X",time.localtime()) 48 t = t.replace(":","-") 49 #takeSnapshot截图 50 snapshot = device.takeSnapshot() 51 snapshot.writeToFile('D: 52 \\SMSTest\\shotscreen\\'+t+'.png','png');