自动化测试过程中发现避免不了需要手动操作的部分,如手动下载最新的APK文件进行smoke测试,而往往新的版本是在夜间进行的,等到早上上班后再进行下载进行smoke测试的话时间就浪费了,因此需要有个脚本自动检测是否有新版本,并自动下载进行smoke测试。可使用requests方法实现下载:
# -*- coding:utf-8 -*-
import requests
import os
from requests.auth import HTTPBasicAuth
import time
from sys import argv
from time import sleep
url = "http://xxxxx.com" \
"version.txt"
base_download_url = "http://xxxxx.com/new.apk"
user_Agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 " \
"Safari/537.36 "
header1 = {
"Referer": url,
"User-Agent": user_Agent
}
header2 = {
"Referer": base_download_url,
"User-Agent": user_Agent
}
serial_no = argv[1]
user = argv[2]
password = argv[3]
# 获取当前版本号
version = os.popen("adb -s " + serial_no + " shell pm dump com.mediatek.wwtv.tvcenter | findstr versionName")
current_version_full = version.read().rsplit("\n")[0].rsplit("=")[1]
current_version = current_version_full.rsplit(".")[2]
print ("Current version: " + current_version_full)
local = os.getcwd()
status = True
# requests.adapters.DEFAULT_RETRIES = 5
# s = requests.session()
# s.keep_alive = False
while status:
response_version = requests.get(url, headers=header1, auth=HTTPBasicAuth(user, password))
response_apk = requests.get(base_download_url, headers=header2, auth=HTTPBasicAuth(user, password))
filename = os.path.basename(url)
apk = os.path.basename(base_download_url)
# 下载 Litv_apk_LiveTV.txt,以便检查最新版本
with open(filename, "wb") as txt:
txt.write(response_version.content)
version_file = open(local + "/version.txt", "r")
lines = version_file.readlines()
for line in lines:
# 检查是否有新版本
if line.find("current_version") != -1:
new_version_full = line.rsplit(" = ")[1].replace('"', '').replace('\n', '')
new_version = line.replace('"', '').rsplit(".")[2]
new_version = int(new_version)
print ("Checking New Version......")
if new_version > int(current_version):
apk_size = int(response_apk.headers["content-length"])
print ("New version found: %s,start downloading......" % new_version_full)
with open(apk, "wb") as apk:
apk.write(response_apk.content)
down_size = os.path.getsize(local + "/xxx.apk")
progress = str(down_size * 100 / apk_size)
print (progress + "% completed")
print ("100% completed,start install xxx.apk")
os.system("adb -s " + serial_no + " install xxx.apk")
status = False
else:
print ("The version of xxx is up to date, check later please.......\n")
sleep(60*60)
print ("====================xxx smoke test start=====================")
此代码是从jenkins中检测新版本并下载,其中apk的版本信息放置在version.txt中:current_version = “x.x.x”
apk 下载过程中未限制字节流,不能直观体现下载进度
且下载较大的文件会出现下载不完整的情况,此时需要进行字节流限制(每次下载的大小)
chunk_size = 1024000# 每次下载1M
with open(apk, "wb") as apk:
for chunk in response_apk.iter_content(chunk_size=chunk_size):
if chunk:
apk.write(chunk)
down_size = os.path.getsize(local + "/xxx.apk")
progress = str(down_size*100/apk_size)
print (progress + "% completed")
结果如下: