Appium+Python+pytest⾃动化测试框架的实战
菜鸟⼀枚,写的不好勿喷,⼤家⼀起学习
先简单介绍⼀下⽬录,再贴⼀些代码,代码⾥有注释
Basic⽬录下写的是⼀些公共的⽅法,Data⽬录下写的是测试数据,image存的是测试失败截图,Log⽇志⽂件,Page测试的定位元素,report测试报告,Test测试⽤例,pytest.ini是pytest启动配置⽂件,需要安装的py模块,run.py运⾏⽂件
Basic/base.py
⾥⾯封装了⼀些⽅法,元素的点击,输⼊,查,还有⼀些⾃⼰需要的公共⽅法也封装在⾥⾯,如果你们有别的需要可以⾃⼰封装调⽤
# coding=utf-8
import random
import allure
import pymysql
import time
from selenium.webdrivermon.by import By
from selenium.webdriver.support.ui import WebDriverWait
from Basic import Log
import os
log = Log.MyLog()
class Base(object):
def __init__(self, driver):
self.driver = driver
# ⾃定义⼀个元素查⽅法
def find_element(self, feature,timeout=5, poll=1.0):
# feature = By.XPATH,"//*[@text='显⽰']"
"""
依据⽤户传⼊的元素信息特征,然后返回当前⽤户想要查元素
:param feature: 元组类型,包含⽤户希望的查⽅式,及该⽅式对应的值
:return: 返回当前⽤户查的元素
"""
by = feature[0]
value = feature[1]
wait = WebDriverWait(self.driver, timeout, poll)
if by == By.XPATH:
# print( "说明了⽤户想要使⽤ xpath 路径的⽅式来获取元素" )
value = self.make_xpath(value)
return wait.until(lambda x: x.find_element(by,value))
def find_elements(self, feature):
wait = WebDriverWait(self.driver, 5, 1)
return wait.until(lambda x: x.find_elements(feature[0], feature[1]))
def click_element(self, loc):
'''
封装点击操作函数
'''
self.find_element(loc).click()
def input_text(self, loc, text):
'''
封装输⼊操作函数
'''
self.fm = self.find_element(loc)
self.fm.clear()  # 需要先清空输⼊框,防⽌有默认内容
self.fm.send_keys(text)
# ⾃定义了⼀个可以⾃动帮我们拼接 xpath 路径的⼯具函数
def make_xpath(self, feature):
start_path = "//*["
end_path = "]"
python在线工具菜鸟工具res_path = ""
if isinstance(feature, str):
# 如果是字符串我们不能直接上来就拆我们可以判断⼀下它是否是默认正确的 xpath 写法
if feature.startswith("//*["):
return feature
# 如果⽤户输⼊的是字符串,那么我们就拆成列表再次进⾏判断
split_list = feature.split(",")
if len(split_list) == 2:
# //*[contains(@text,'设')]
res_path = "%scontains(@%s,'%s')%s" % (start_path, split_list[0], split_list[1], end_path)            elif len(split_list) == 3:
# //[@text='设置']
res_path = "%s@%s='%s'%s" % (start_path, split_list[0], split_list[1], end_path)
else:
print("请按规则使⽤")
elif isinstance(feature, tuple):
for item in feature:
# 默认⽤户在元组当中定义的数据都是字符串
split_list2 = item.split(',')
if len(split_list2) == 2:
res_path += "contains(@%s,'%s') and " % (split_list2[0], split_list2[1])
elif len(split_list2) == 3:
res_path += "@%s='%s' and " % (split_list2[0], split_list2[1])
else:
print("请按规则使⽤")
andIndex = res_path.rfind(" and")
res_path = res_path[0:andIndex]
res_path = start_path + res_path + end_path
else:
print("请按规则使⽤")
return res_path
def assert_ele_in(self, text, element):
'''
封装断⾔操作函数
'''
try:
assert text in self.find_element(element).text
assert 0
except Exception:
assert 1
def get_assert_text(self, element):
ele = self.find_element(element, timeout=5, poll=0.1)
# ⾃定义⼀个获取 toast内容的⽅法
def get_toast_content(self, message):
tmp_feature = By.XPATH, "//*[contains(@text,'%s')]" % message
ele = self.find_element(tmp_feature)
# ⾃定义⼀个⼯具函数,可以接收⽤户传递的部分 toast 信息,然后返回⼀个布尔值,来告诉    # ⽤户,⽬标 toast 到底是否存在
def is_toast_exist(self, mes):
# 拿着⽤户传过来的 message 去判断⼀下包含该内容的 toast 到底是否存在。
try:
<_toast_content(mes)
return True
except Exception:
# 如果⽬标 toast 不存在那么就说明我们的实际结果和预期结果不⼀样
# 因此我们想要的是断⾔失败
return False
def get_mysql(self,  table, value):
'''连接数据库'''
# 打开数据库连接
db = t(host='', port=, db=, user='', passwd='', charset='utf8')
# 使⽤ cursor() ⽅法创建⼀个游标对象 cursor
cursor = db.cursor()
try:
# 使⽤ execute()  ⽅法执⾏ SQL 查询
dbmit()
except Exception as e:
print(e)
# 使⽤ fetchone() ⽅法获取单条数据.
data = cursor.fetchone()
# 关闭数据库连接
db.close()
return data
def get_xpath(self, value):
'''封装获取xpath⽅法'''
text = By.XPATH, '//*[@text="%s"]' % value
return text
# ⾃定义⼀个获取当前设备尺⼨的功能
def get_device_size(self):
x = _window_size()["width"]
y = _window_size()["height"]
return x, y
# ⾃定义⼀个功能,可以实现向左滑屏操作。
def swipe_left(self):
start_x = _device_size()[0] * 0.9
start_y = _device_size()[1] * 0.5
end_x = _device_size()[0] * 0.4
end_y = _device_size()[1] * 0.5
self.driver.swipe(start_x, start_y, end_x, end_y)
# ⾃定义⼀个功能,可以实现向上滑屏操作。
def swipe_up(self):
start_x = _device_size()[0] * 1/2
start_y = _device_size()[1] * 1/2
end_x = _device_size()[0] * 1/2
end_y = _device_size()[1] * 1/7
self.driver.swipe(start_x, start_y, end_x, end_y, 500)
# 切换到
def switch_weixxin(self):
self.driver.start_activity("", ".ui.LauncherUI")
# 切换到医⽣端
def switch_doctor(self):
self.driver.start_activity("com.rjjk_doctor", ".MainActivity")
# 切换到销售端
def switch_sale(self):
self.driver.start_activity("com.rjjk_sales", ".MainActivity")
def switch_webview(self):
# 切换到webview
print(ts)
time.sleep(5)
self.driver.t("t.mm:tools")
print("切换成功")
time.sleep(3)
# ⾃定义根据坐标定位
def taptest(self, a, b):
# 设定系数,控件在当前⼿机的坐标位置除以当前⼿机的最⼤坐标就是相对的系数了
# 获取当前⼿机屏幕⼤⼩X,Y
X = _window_size()['width']
Y = _window_size()['height']
# 屏幕坐标乘以系数即为⽤户要点击位置的具体坐标
self.driver.tap([(a * X, b * Y)])
# ⾃定义截图函数
def take_screenShot(self):
'''
测试失败截图,并把截图展⽰到allure报告中
'''
tm = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))
_screenshot_as_file(
allure.attach.wd() + os.sep + "image/%s.png" %
tm, attachment_type=allure.attachment_type.PNG)
# ⾃定义随机⽣成11位⼿机号
def create_phone(self):
# 第⼆位数字
second = [3, 4, 5, 7, 8][random.randint(0, 4)]
# 第三位数字
third = {
3: random.randint(0, 9),
4: [5, 7, 9][random.randint(0, 2)],
5: [i for i in range(10) if i != 4][random.randint(0, 8)],
7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)],
8: random.randint(0, 9),
}[second]
# 最后⼋位数字
suffix = random.randint(9999999, 100000000)
# 拼接⼿机号
return "1{}{}{}".format(second, third, suffix)
Basic/deiver.py
APP启动的前置条件,⼀个是普通的app,⼀个是,配置⾃动化测试和⼀般的APP是有点区别的,需要切换webview才能定位到
from appium import webdriver
def init_driver():
desired_caps = {}
# ⼿机系统信息
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '9'
# 设备号
desired_caps['deviceName'] = 'emulator-5554'
# 包名
desired_caps['appPackage'] = ''
# 启动名
desired_caps['appActivity'] = ''
desired_caps['automationName'] = 'Uiautomator2'
# 允许输⼊中⽂
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
desired_caps['autoGrantPermissions'] = True
desired_caps['noReset'] = False
# ⼿机驱动对象
driver = webdriver.Remote("127.0.0.1:4723/wd/hub", desired_caps)    return driver
def driver_weixin():
desired_caps = {}
# ⼿机系统信息
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '9'
# 设备号
desired_caps['deviceName'] = ''
# 包名
desired_caps['appPackage'] = ''
# 启动名
desired_caps['appActivity'] = '.ui.LauncherUI'
# desired_caps['automationName'] = 'Uiautomator2'
# 允许输⼊中⽂
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
desired_caps['noReset'] = True
# desired_caps["newCommandTimeout"] = 30
# desired_caps['fullReset'] = 'false'
# desired_caps['newCommandTimeout'] = 10
# desired_caps['recreateChromeDriverSessions'] = True
desired_caps['chromeOptions'] = {'androidProcess': ':tools'}    # ⼿机驱动对象
driver = webdriver.Remote("127.0.0.1:4723/wd/hub", desired_caps)    return driver
Basic/get_data.py
这是获取测试数据的⽅法
import os
import yaml
def getData(funcname, file):
PATH = os.getcwd() + os.sep
with open(PATH + 'Data/' + file + '.yaml', 'r', encoding="utf8") as f:
data = yaml.load(f, Loader=yaml.FullLoader)
# 1 先将我们获取到的所有数据都存放在⼀个变量当中
tmpdata = data[funcname]
# 2 所以此时我们需要使⽤循环⾛进它的内⼼。
res_arr = list()
for value in tmpdata.values():
tmp_arr = list()
for j in value.values():
tmp_arr.append(j)
res_arr.append(tmp_arr)
return res_arr
Basic/Log.py
⽇志⽂件,不多介绍

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。