前文中已对测试服务搭建进行了说明,接下来主要是对runCase()实现做些准备工作。
- 获取有效设备地址:(用于支持同时进行测试)
def findDevice():
"""
获取有效的设备地址
:return: 列表地址列表
"""
result = os.popen('adb devices')
res = str(result.read())
res = res.replace("List of devices attached", "")
res = res.replace("\n", "")
res = res.replace("\t", "")
devicess = []
d = []
if "device" in res:
devicess = str(res).split("device")
for dev in devicess:
d = d + str(dev).split("offline")
for k in d:
if k == "":
d.remove(k)
log.debug("设备列表:{}".format(str(d)))
return d
- 下载指定APP至指定目录:(方便后面安装)
def downApk(url1):
"""
下载指定apk
:param url1: 下载地址
"""
times = str(time.time())
frist_path = "./resTest/old/{}.apk".format(times)
try:
s = requests.Session()
r = s.get(url1, stream=True, verify=False, timeout=30)
f = open(frist_path, "wb")
for chunk in r.iter_content(chunk_size=512):
if chunk:
f.write(chunk)
except:
return None
return frist_path
- 获取应用包名,判断文件有效性:
def get_apk_page(App_dir):
"""
获取应用Package及启动的Activity
:param App_dir: 应用APK地址
:return: [Package,Activity]
"""
apk_page = []
cmd = 'aapt dump badging ' + App_dir
result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
res = result.stdout.read()
try:
tex = re.search("package: name='.+?'", str(res))
tex = tex.group()
tex = tex.replace("package: name='", "")
tex = tex.replace("'", "")
apk_page.append(tex)
te = re.search("launchable-activity: name='.+?'", str(res))
te = te.group()
te = te.replace("launchable-activity: name='", "")
te = te.replace("'", "")
apk_page.append(te)
except:
return None
log.debug("应用包名:{}".format(str(apk_page)))
return apk_page
- 清除设备上的多余APP:(测试环境统一)
def clear_device(device):
"""
指定设备清除多余APP,可自定义白名单
:param device: 设备
"""
cmd = "adb -s " + device + " shell pm list package -3"
result = os.popen(cmd)
res = result.read()
res.replace(r"\n\n", "")
packages = []
apk_ok = ['com.github.uiautomator', 'com.github.uiautomator.test', '']
if "package" in res:
packages = str(res).split("package")
for package in packages:
package = package.replace("\n\n", "").replace(":", "")
if package not in apk_ok:
cmd = 'adb -s ' + device + ' uninstall ' + package
result = os.popen(cmd)
result.read()
return