常规做APPUI自动化时,基本上都是采取的pom模式加上关键字驱动、数据驱动、实现测试数据分离。
需求是:
(1)实现基本业务流程的测试
(2)多设备同时运行
(3)自动拉取最新apk并自动安装
(4)持续集成
分析:
业务的基本流程覆盖,相信做过接口web端的自动化或者接口自动化的同学来说,实现app ui自动化其实不难,也是相同的思路pom模式、关键字驱动、数据驱动、数据分离等。
1.先来自动拉取最新apk 并且实现自动化安装。
import os
import re
import shutil
from urllib.request import urlopen # 用于获取网页
import requests
from bs4 import BeautifulSoup # 用于解析网页
path = os.path.normpath(os.path.join(os.path.join(os.path.dirname(os.path.dirname(__file__)))))
class automatic(object):
def __init__(self):
self.url = 'http://10.124.106.120:28759/service/rest/repository/browse/maven-releases/tcl/release/android/tclplus/'
@staticmethod
def _get_url(url):
"""
根据传入的url来获取超链接
"""
html = urlopen(url)
bsObj = BeautifulSoup(html, 'html.parser')
t1 = bsObj.find_all('a')
return t1
# 获取线上最新版本号与版本时间
def get_latest_version(self):
"""
获取最新版本号
"""
t1 = self._get_url(self.url)
time_version_dict = []
for t2 in t1:
t3 = t2.get('href')
if '2.0' not in t3:
# 将2.0以上的版本全部过滤掉,然后加入到列表里面
time_version_dict.append(str(t3).split("/")[0].strip("..").strip("http:"))
# 找出最大的版本号
latest_version = max(time_version_dict)
print(f"最大版本号为:{
latest_version}")
result_url = self.url + str(latest_version)
return result_url, latest_version
def download_apk(self):
"""
下载apk
"""
global apk_name
urls, number = self.get_latest_version()
t1 = self._get_url(urls)
time_version_dict = []
url_list = []
for t2 in t1:
t3 = t2.get('href')
url_list.append(t3)
# 利用正则表达式找到2021开头的版本号
pattern = re.compile(r'(?:2021)\d+\.?\d*')
time_version_dict.append(pattern.findall(t3))
Ak = max(time_version_dict)[0]
for url in url_list:
if str(Ak) in url:
file = requests.get(url, timeout=60)
apk_name = f"{
number}-{
max(time_version_dict)[0]}-jiagu-xtest-release.apk"
with open(path + '/APK/' + apk_name, 'wb') as zip_file:
zip_file.write(file.content)
return apk_name
主要就是发起请求,取到最新版本号,然后获取链接地址,拿到下载地址之后,来写file.content,保存apk 即可。
2.第二步,多设备链接并自动安装
Decorator.py
import time
from functools import wraps
from Common.BasePage import BasePage
from Common.ReportPath import ReportPath
from Common.Log import Log
flag = 'IMAGE:'
log = Log()
def _screenshot(name):
date_time = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
screenshot = name + '-' + date_time + '.PNG'
path = ReportPath().get_path() + '/' + screenshot
driver, sess = BasePage().get_driver()
driver.screenshot(path)
return screenshot
def teststep(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
log.i('\t--> %s', func.__qualname__)
ret = func(*args, **kwargs)
return ret
except AssertionError as e:
log.e('AssertionError, %s', e)
log.e('\t<-- %s, %s, %s', func.__qualname__, 'AssertionError', 'Error')
if flag in str(e):
raise AssertionError(e)
else:
raise AssertionError(flag + _screenshot(func.__qualname__))
except Exception as e:
log.e('Exception, %s', e)
log.e('\t<-- %s, %s, %s', func.__qualname__, 'Exception', 'Error')
if flag in str(e):
raise Exception(e)
else:
raise Exception(flag + _screenshot(func.__qualname__))
return wrapper
def teststeps(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
log.i(' --> %s', func.__qualname__)
ret = func(*args, **kwargs)
log.i(' <-- %s, %s', func.__qualname__, 'Success')
return ret
except AssertionError as e:
log.e('AssertionError, %s', e)
log.e(' <-- %s, %s, %s', func.__qualname__, 'AssertionError', 'Error')
if flag in str(e):
raise AssertionError(e)
else:
raise AssertionError(flag + _screenshot(func.__qualname__))
except Exception as e:
log.e('Exception, %s', e)
log.e(' <-- %s, %s, %s', func.__qualname__, 'Exception', 'Error')
if flag in str(e):
raise Exception(e)
else:
raise Exception(flag + _screenshot(func.__qualname__))
return wrapper
def _wrapper(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
log.i('--> %s', func.__qualname__)
ret = func(*args, **kwargs)
log.i('<-- %s, %s\n', func.__qualname__, 'Success')
return ret
except AssertionError as e:
log.e('AssertionError, %s', e)
log.e('<-- %s, %s, %s\n', func.__qualname__, 'AssertionError', 'Fail')
if flag in str(e):
raise AssertionError(e)
else:
raise AssertionError(flag + _screenshot(func.__qualname__))
except Exception as e:
log.e('Exception, %s', e)
log.e('<-- %s, %s, %s\n', func.__qualname__, 'Exception', 'Error')
if flag in str(e):
raise Exception(e)
else:
raise Exception(flag + _screenshot(func.__qualname__