先简单介绍一下目录,再贴一些代码,代码里有注释
Basic目录下写的是一些公共的方法,Data目录下写的是测试数据,image存的是测试失败截图,Log日志文件,Page测试的定位元素,report测试报告,Test测试用例,pytest.ini是pytest启动配置文件,requirements.txt需要安装的py模块,run.py运行文件
Basic/base.py
里面封装了 一些方法,元素的点击,输入,查找,还有一些自己需要的公共方法也封装在里面,如果你们有别的需要可以自己封装调用
# coding=utf-8import random
import allure
import pymysql
import time
from selenium.webdriver.common.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 = "]"
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)
return ele.text
# 自定义一个获取 toast内容的方法def get_toast_content(self, messa