一键获取Android的appActvity和PackName

大家平常写Appium自动化时,可能写脚本半小时,得有5分钟用来去看Activity,大部分都是通过adb命令的方式来获取。为了提高效率,可以把这个命令放到python里去执行,然后根据规则去筛选出自己想要的信息,好啦。不说废话啦,来个秒找到  AppActivity的脚本吧,帮大家每天多节约5分钟哈

# -* - coding: UTF-8 -* -
import os
from common import const
from common.userConf import UserConf

def scan_devices():
"获取当前连接的设备"
result = os.popen("adb devices").read()
print (result)
devices = result.split("\n")[1:]
targets = []
for device in devices:
if device.strip():
tmp = device.split()
if len(tmp)==2 and tmp[1]=='device':
deviceInfo = {}
deviceID = tmp[0]
deviceInfo['id'] = deviceID
deviceInfo["os"] = os.popen("adb -s %s shell getprop ro.build.version.release" % (deviceID)).read().strip()
deviceInfo["name"] = os.popen("adb -s %s shell getprop ro.product.model" % (deviceID)).read().strip().replace(" ", "-")

targets.append(deviceInfo)
print (deviceInfo)
return targets

def getPackage():
"获取apk的包名和Activity的名字"
apkFile = "/Users/chengyanan/Downloads/AndroidAppPerformance-master/apk/kuai8.apk"
apkPath = os.path.join(const.workSpace, "apk")
commonPath = os.path.join(const.workSpace, "Common")
aapthPath = os.path.join(commonPath, "aapt")
for filename in os.listdir(apkPath):
if filename.endswith(".apk"):
apkFile = os.path.join(apkPath,filename)
break
commonStr = aapthPath+" dump badging "+apkFile
print (commonStr)
result = os.popen(commonStr).read()
lines = result.split("\n")

for line in lines:
if line.startswith("package: name="):
UserConf.packageName = line.split()[1].split("=")[1][1:-1]
print (UserConf.packageName)
break
for line in lines:
if line.startswith("launchable-activity: name="):
UserConf.activityName = line.split()[1].split("=")[1][1:-1]
print (UserConf.activityName)
break
if __name__ == "__main__":
getPackage()
scan_devices()

posted on 2019-09-18 10:13  barton123  阅读(568)  评论(0编辑  收藏  举报

导航