Skip to content

Commit 32d41d5

Browse files
author
gb112211
committed
部分python脚本支持多设备
1 parent 0958bc7 commit 32d41d5

3 files changed

Lines changed: 101 additions & 13 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Android测试中常用到的脚本
77

88
批量安装应用(支持以中文命名的 apk)、批量卸载、截屏、录制视频、获取当前应用的 apk 文件、包名、Activity 名等。<br>
99

10+
11+
###2016.7.19
12+
增加部分python脚本对多设备的支持:
13+
14+
![device_id](image/device_id.png)
1015
###2016.04.22
1116
增加 `fps.py`,获取测试界面的 `fps``jankniess`
1217

image/device_id.png

18.4 KB
Loading

python/scriptUtils/utils.py

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
import re
1212
import subprocess
1313
import time
14+
import Tkinter as tk
15+
import ttk
1416

1517
import exception
1618

19+
serialno_num = ""
20+
1721
#判断系统类型,windows使用findstr,linux使用grep
1822
system = platform.system()
1923
if system is "Windows":
2024
find_util = "findstr"
2125
else:
2226
find_util = "grep"
23-
27+
2428
#判断是否设置环境变量ANDROID_HOME
2529
if "ANDROID_HOME" in os.environ:
2630
if system == "Windows":
@@ -30,27 +34,94 @@
3034
else:
3135
raise EnvironmentError(
3236
"Adb not found in $ANDROID_HOME path: %s." %os.environ["ANDROID_HOME"])
33-
37+
38+
39+
def get_screen_size(window):
40+
return window.winfo_screenwidth(),window.winfo_screenheight()
41+
42+
def get_window_size(window):
43+
return window.winfo_reqwidth(),window.winfo_reqheight()
44+
45+
def center_window(root, width, height):
46+
screenwidth = root.winfo_screenwidth()
47+
screenheight = root.winfo_screenheight()
48+
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
49+
root.geometry(size)
50+
51+
class Window(object):
52+
device_id = ""
53+
device_id_list = []
54+
root = None
55+
box = None
56+
def __init__(self, device_id_list, root):
57+
self.device_id_list = device_id_list
58+
self.device_id = device_id_list[0]
59+
self.root = root
60+
self.box = None
61+
62+
def show_window(self):
63+
self.root.title(u'Serialno Number')
64+
center_window(self.root, 300, 240)
65+
self.root.maxsize(600, 400)
66+
self.root.minsize(300, 240)
67+
68+
options = self.device_id_list
69+
self.box = ttk.Combobox(values=options)
70+
self.box.current(0)
71+
self.box.pack(expand = tk.YES)
72+
self.box.bind("<<ComboboxSelected>>", self.select)
73+
ttk.Button(text=u"确定", command=self.ok).pack(expand = tk.YES)
74+
75+
self.root.mainloop()
76+
77+
78+
def select(self, event=None):
79+
self.device_id = self.box.selection_get()
80+
81+
def ok(self):
82+
global serialno_num
83+
serialno_num = self.device_id
84+
self.root.destroy()
85+
3486
#adb命令
3587
def adb(args):
36-
cmd = "%s %s" %(command, str(args))
88+
global serialno_num
89+
if serialno_num == "":
90+
devices = get_device_list()
91+
if len(devices) == 1:
92+
#global serialno_num
93+
serialno_num = devices[0]
94+
else:
95+
root = tk.Tk()
96+
window = Window(devices, root)
97+
window.show_window()
98+
cmd = "%s -s %s %s" %(command, serialno_num, str(args))
3799
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
38100

39101
#adb shell命令
40102
def shell(args):
41-
cmd = "%s shell %s" %(command, str(args))
103+
global serialno_num
104+
if serialno_num == "":
105+
devices = get_device_list()
106+
if len(devices) == 1:
107+
serialno_num = devices[0]
108+
else:
109+
root = tk.Tk()
110+
window = Window(devices, root)
111+
window.show_window()
112+
cmd = "%s -s %s shell %s" %(command, serialno_num, str(args))
42113
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
43114

44115
#获取设备状态
45116
def get_state():
46-
return os.popen("adb get-state").read().strip()
117+
return os.popen("adb -s %s get-state" %serialno_num).read().strip()
47118

48119
#获取对应包名的pid
49-
def get_app_pid(pkg_name):
120+
def get_app_pid(pkg_name):
50121
if system is "Windows":
51122
string = shell("ps | findstr %s$" %pkg_name).stdout.read()
52-
else:
53-
string = shell("ps | grep -w %s" %pkg_name).stdout.read()
123+
124+
string = shell("ps | grep -w %s" %pkg_name).stdout.read()
54125

55126
if string == '':
56127
return "the process doesn't exist."
@@ -64,13 +135,13 @@ def get_app_pid(pkg_name):
64135
#杀掉对应包名的进程
65136
def kill_process(pkg_name):
66137
pid = get_app_pid(pkg_name)
67-
138+
68139
result = shell("kill %s" %str(pid)).stdout.read().split(": ")[-1]
69-
140+
70141
if result != "":
71142
raise exception.SriptException("Operation not permitted or No such process")
72143

73-
#获取设备上当前应用的包名与activity
144+
#获取设备上当前应用的包名与activity
74145
def get_focused_package_and_activity():
75146
pattern = re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")
76147
out = shell("dumpsys window w | %s \/ | %s name=" %(find_util, find_util)).stdout.read()
@@ -89,6 +160,18 @@ def get_current_activity():
89160
def timestamp():
90161
return time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
91162

163+
def get_device_list():
164+
devices = []
165+
result = subprocess.Popen("adb devices", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
166+
result.reverse()
167+
for line in result[1:]:
168+
if "attached" not in line.strip():
169+
devices.append(line.split()[0])
170+
else:
171+
break
172+
return devices
173+
174+
92175
#连接设备
93176
# adb("kill-server").wait()
94177
# adb("start-server").wait()
@@ -97,10 +180,10 @@ def timestamp():
97180
if get_state() != "device":
98181
adb("kill-server").wait()
99182
adb("start-server").wait()
100-
183+
101184
if get_state() != "device":
102185
raise exception.SriptException("Device not run")
103-
186+
104187

105188
if __name__ == "__main__":
106189
pass

0 commit comments

Comments
 (0)