启动多个device等的python 转帖

增强型MonkeyRunner 脚本计划

2011-11-25 10:02 by youxiachai, 7561 阅读, 6 评论, 收藏编辑

最近,做android项目测试,测试一个应用程序在多个设备中分辨率是否合适,麻烦得要命,不停的截图比较,前一段时间,研究了一下monkeyrunner ,决定通宵写一个实用性强的monkeyrunner来方便测试使用

前一段时间写的monkeyrunner资料

http://www.cnblogs.com/youxilua/archive/2011/11/12/2246576.html

功能说明:

  1. 可以自动安装apk到android模拟器或者android实机
  2. 可以截取设定好activity运行在android模拟器或者android实机上的效果图
  3. 截图根据设备名和截取时间保存
  4. 能够自动设别所有运行中的android模拟器或者android实机
  5. 能够自动安装在特定文件夹下所有的apk文件到android模拟器或者实机

功能实现:

环境配置:

  • 设置好android /tootls 和 android /platform-tools 目录的到环境变量中,确保adb命令能够在cmd下正常运行

脚本编写:

该功能需要创建以下几个文件

300x110_MarkMan

componentName.txt

使用约定

每一行为需要运行的activity的完整名字

例如:

kg.tom/.HandlerThreadActivity 
kg.monkey/.MonkeyActivity

 

 

takeSnap.bat
1
2
3
4
5
6
7
@echo off
rem 获取当前运行设备
adb devices > devices.txt
rem 获取APK文件
dir apk /B > apk.txt
rem 运行monkeyrunner 脚本
monkeyrunner work.py

xxxx

核心运行脚本:

1
#导入我们需要用到的包和类并且起别名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#导入我们需要用到的包和类并且起别名
import sys,time,datetime
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner import MonkeyDevice as md
from com.android.monkeyrunner import MonkeyImage as mi
deviceslist = []
devices = []
snapshot = []
templist = []
f = open("devices.txt")
while True:
    line = f.readline()
    if line:
        templist.append(line.strip())
    else:
        break;
f.close()
templist.pop()
for i in range(len(templist)):
    deviceslist.append(templist[i].split('\t'))
fc = open("componentName.txt")
complist = []
while True:
    comp = fc.readline()
    if comp:
        complist.append(comp.strip())
    else:
        break;
fc.close()
fp = open("apk.txt")
apklist = []
while True:
    apk = fp.readline()
    if apk:
        apklist.append(apk.strip())
    else:
        break;
print 'apk list :'
print apklist
print 'start componentName list :'
print complist
print 'devices list:'
print deviceslist
for i in range(1,len(deviceslist)):
    print 'current devices:'
    print deviceslist[i]
    devices.append(mr.waitForConnection(1.0,deviceslist[i][0]))
    #安装apk文件
    for j in range(len(apklist)):
        devices[i-1].installPackage('apk/'+apklist[j])
    #启动activity
    for k in range(len(complist)):
        print 'current start activity:'
        print complist[k]
        devices[i-1].startActivity(component=complist[k])
        #设置延时秒数
        mr.sleep(5.0)
        #----------------
        #这里可进行一定的UI操作
        #----------------
        #mr.sleep(3.0)
        #进行截图
        snapshot.append(devices[i-1].takeSnapshot())
        print 'end snapshot'
        #创建时间字符串
        t = time.strftime("%Y-%m-%d-%X",time.localtime())
        t = t.replace(":","-")
        #保存截图
        package = complist[k].replace('/.','.')
        snapshot[0].writeToFile('takeSnapshot/'+deviceslist[i][0]+'-'+t+'-'+package+'.png','png');
        snapshot.pop()
1
由于之前毫无编写python的经验,只能写成这样了,欢迎高手补充!!!!!!
posted on 2014-05-08 21:47  麦兜布熊  阅读(328)  评论(0)    收藏  举报