Skip to content

Commit 64104e0

Browse files
committed
2015.01.26
1 parent 8499d4f commit 64104e0

5 files changed

Lines changed: 132 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ AndroidTestScrpits
22
==================
33

44
Android测试中常用到的脚本
5+
6+
###2015年1月26日
7+
8+
1.改写python分类中的脚本结构,将大部分方法分装进了scriptUtils包中的utils模块中
9+
2.新增record.py(录制视频)
10+
3.使用时请直接在脚本目录下运行脚本

‎python/screenrecord.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
77
@author: xuxu
88
'''
9+
910
import os
11+
import string
12+
import sys
1013

1114
from scriptUtils import utils
1215

@@ -32,5 +35,10 @@ def record():
3235
utils.adb("pull /data/local/tmp/video.mp4 %s" %PATH("%s/%s.mp4" %(path, utils.timestamp()))).wait()
3336

3437
if __name__ == "__main__":
35-
record()
36-
print "Completed"
38+
sdk = string.atoi(utils.shell("getprop ro.build.version.sdk").stdout.read())
39+
if sdk < 19:
40+
print "sdk version is %s, less than 19!"
41+
sys.exit(0)
42+
else:
43+
record()
44+
print "Completed"

‎python/scriptUtils/__init__.py‎

Whitespace-only changes.

‎python/scriptUtils/exception.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
Created on 2015年1月23日
6+
7+
@author: xuxu
8+
'''
9+
class SriptException(Exception):
10+
def __init__(self, str):
11+
self.str = str
12+
13+
def _str_(self):
14+
return self.str

‎python/scriptUtils/utils.py‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
'''
4+
Created on 2015年1月23日
5+
6+
@author: xuxu
7+
'''
8+
9+
import os
10+
import platform
11+
import re
12+
import subprocess
13+
import time
14+
15+
import exception
16+
17+
#判断系统类型,windows使用findstr,linux使用grep
18+
system = platform.system()
19+
if system is "Windows":
20+
find_util = "findstr"
21+
else:
22+
find_util = "grep"
23+
24+
#判断是否设置环境变量ANDROID_HOME
25+
if "ANDROID_HOME" in os.environ:
26+
if system == "Windows":
27+
command = os.path.join(os.environ["ANDROID_HOME"], "platform-tools", "adb.exe")
28+
else:
29+
command = os.path.join(os.environ["ANDROID_HOME"], "platform-tools", "adb")
30+
else:
31+
raise EnvironmentError(
32+
"Adb not found in $ANDROID_HOME path: %s." %os.environ["ANDROID_HOME"])
33+
34+
#adb命令
35+
def adb(args):
36+
cmd = "%s %s" %(command, str(args))
37+
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
38+
39+
#adb shell命令
40+
def shell(args):
41+
cmd = "%s shell %s" %(command, str(args))
42+
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
43+
44+
#获取设备状态
45+
def get_state():
46+
return adb("get-state").stdout.read().split("\r\n")[-2]
47+
48+
#获取对应包名的pid
49+
def get_app_pid(pkg_name):
50+
if system is "Windows":
51+
string = shell("ps | findstr %s$" %pkg_name).stdout.read()
52+
53+
string = shell("ps | grep -w %s" %pkg_name).stdout.read()
54+
55+
if string == '':
56+
return "the process doesn't exist."
57+
58+
pattern = re.compile(r"\d+")
59+
result = string.split(" ")
60+
result.remove(result[0])
61+
62+
return pattern.findall(" ".join(result))[0]
63+
64+
#杀掉对应包名的进程
65+
def kill_process(pkg_name):
66+
pid = get_app_pid(pkg_name)
67+
68+
result = shell("kill %s" %str(pid)).stdout.read().split(": ")[-1]
69+
70+
if result != "":
71+
raise exception.SriptException("Operation not permitted or No such process")
72+
73+
#获取设备上当前应用的包名与activity
74+
def get_focused_package_and_activity():
75+
pattern = re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")
76+
out = shell("dumpsys window w | %s \/ | %s name=" %(find_util, find_util)).stdout.read()
77+
78+
return pattern.findall(out)[0]
79+
80+
#获取当前应用的包名
81+
def get_current_package_name():
82+
return get_focused_package_and_activity().split("/")[0]
83+
84+
#获取当前设备的activity
85+
def get_current_activity():
86+
return get_focused_package_and_activity().split("/")[-1]
87+
88+
#时间戳
89+
def timestamp():
90+
return time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
91+
92+
#连接设备
93+
# adb("kill-server").wait()
94+
# adb("start-server").wait()
95+
adb("wait-for-device")
96+
97+
if get_state() != "device":
98+
raise exception.SriptException("Device not run")
99+
100+
101+
if __name__ == "__main__":
102+
pass

0 commit comments

Comments
 (0)