forked from gb112211/AndroidTestScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
106 lines (80 loc) · 2.77 KB
/
utils.py
File metadata and controls
106 lines (80 loc) · 2.77 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2015年1月23日
@author: xuxu
'''
import os
import platform
import re
import subprocess
import time
import exception
#判断系统类型,windows使用findstr,linux使用grep
system = platform.system()
if system is "Windows":
find_util = "findstr"
else:
find_util = "grep"
#判断是否设置环境变量ANDROID_HOME
if "ANDROID_HOME" in os.environ:
if system == "Windows":
command = os.path.join(os.environ["ANDROID_HOME"], "platform-tools", "adb.exe")
else:
command = os.path.join(os.environ["ANDROID_HOME"], "platform-tools", "adb")
else:
raise EnvironmentError(
"Adb not found in $ANDROID_HOME path: %s." %os.environ["ANDROID_HOME"])
#adb命令
def adb(args):
cmd = "%s %s" %(command, str(args))
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#adb shell命令
def shell(args):
cmd = "%s shell %s" %(command, str(args))
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#获取设备状态
def get_state():
return os.popen("adb get-state").read().strip()
#获取对应包名的pid
def get_app_pid(pkg_name):
if system is "Windows":
string = shell("ps | findstr %s$" %pkg_name).stdout.read()
string = shell("ps | grep -w %s" %pkg_name).stdout.read()
if string == '':
return "the process doesn't exist."
pattern = re.compile(r"\d+")
result = string.split(" ")
result.remove(result[0])
return pattern.findall(" ".join(result))[0]
#杀掉对应包名的进程
def kill_process(pkg_name):
pid = get_app_pid(pkg_name)
result = shell("kill %s" %str(pid)).stdout.read().split(": ")[-1]
if result != "":
raise exception.SriptException("Operation not permitted or No such process")
#获取设备上当前应用的包名与activity
def get_focused_package_and_activity():
pattern = re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")
out = shell("dumpsys window w | %s \/ | %s name=" %(find_util, find_util)).stdout.read()
return pattern.findall(out)[0]
#获取当前应用的包名
def get_current_package_name():
return get_focused_package_and_activity().split("/")[0]
#获取当前设备的activity
def get_current_activity():
return get_focused_package_and_activity().split("/")[-1]
#时间戳
def timestamp():
return time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
#连接设备
# adb("kill-server").wait()
# adb("start-server").wait()
adb("wait-for-device")
if get_state() != "device":
adb("kill-server").wait()
adb("start-server").wait()
if get_state() != "device":
raise exception.SriptException("Device not run")
if __name__ == "__main__":
pass