环境准备
python3.7
新建python工程
chromedriver.exe
pip install xlrd
pip install xlutils
pip install selenium
项目结构
一、获取测试用例
Excel用例分为两部分
元素
获取页面元素信息:
def getPages(path):
book = xlrd.open_workbook(path) # 打开要读取的Excel
sheet = book.sheet_by_index(0)
rows = sheet.nrows # sheet页里面的行数
webPages = []##存放webPage类
webpage = webPageClass()
for i in range(1, rows):
cells = sheet.row_values(i)
# print(cells)
if i == 1:
webpage.pageName = cells[0]
elif cells[0] != '':
##如果当第一格有值,那么将上一个webpage对象加入到webpages集合中,并创建一个新的webpage类
webPages.append(webpage)
webpage = webPageClass()
webpage.pageName = cells[0]
webelement = pageElementClass(cells[1], cells[2], cells[3])
webpage.pageElements.append(webelement)
if i == rows - 1:
##当到达最后一行时,将webpage对象添加到webpages中
webPages.append(webpage)
return webPages
用例
获取用例信息
def getcase(path):
book = xlrd.open_workbook(path) # 打开要读取的Excel
sheet = book.sheet_by_index(1)
rows = sheet.nrows # sheet页里面的行
webpages = webpage.getPages(path)
testcaseList = []##存放case对象
testcase = TestCaseClass("")
for i in range(1, rows):
cells = sheet.row_values(i)
if i == 1:
testcase.casename = cells[0]
elif cells[0] != "":
##当第一个格子有值值时创建一个新的case对象,并将上一个case对象添加到数组中
testcaseList.append(testcase)
testcase = TestCaseClass(cells[0])
##检查用例元素是否存在再页面中
element = findpageelement(webpages, cells[3], cells[4])
##floatTostring()excel表中会出现纯数字内容,py会将其默认为float类型;
##所以需根据该值具体情况,转化为str类型
step = CaseStepClass(stepname=cells[2], element=element, action=cells[5], parm=floatTostring(cells[6]),
expect=cells[7], row=i)
testcase.steps.append(step)
testcaseList.append(testcase)
return testcaseList
def floatTostring(value):
if type(value) == float:
if value - int(value) == 0:
##如果value为整数则需转化为int在转化为str
return str(int(value))
else:
##如果value为小数则直接转化为str
return str(value)
return str(value)
def findpageelement(webElements, pagename, elementname):
##根据页面名称、元素名称寻找具体的元素属性,如果寻到了则返回pageelement对象
##如果没有找到则直接退出程序
print("寻找的页面" + pagename + "寻找的元素" + elementname)
for webPage in webElements:
print("进入页面" + webPage.pageName)
if webPage.pageName != pagename:
continue
elif webPage.pageName == pagename:
for pageElement in webPage.pageElements:
if pageElement.elemnetNmae != elementname:
continue
elif pageElement.elemnetNmae == elementname:
print("找到了元素")
print(pageElement.say())
return pageElement
else:
print("findpageelement:elementerr")
else:
print("findpageelement:pageerr")
print("!!!!!!!!寻找不到元素")
exit()
webdriver操作
元素定位
def find_element(self, type, value):
###只是添加了一些打印信息
print("方法" + type + "目标值:" + value + "查找元素")
try:
webElment = self.webdriver.find_element(type, value)
except BaseException:
print("寻找元素错误")
else:
print("寻找元素daole")
return webElment
元素操作
def elementAction(self, step):
##根据step.action的值,执行相应的webdriver方法
if step.action == "get":
print("浏览器打开了url:" + step.element.value)
self.webdriver.get(step.element.value)
return
element = self.find_element(step.element.by, step.element.value)
if step.action == "click":
print("点击了元素:" + step.element.value)
element.click()
elif step.action == "text":
print("预期值:" + step.expect)
print("实际值:" + element.text)
# assert step.expect == element.text
if step.expect == element.text:
# record(result="T", row=step.row)
return "T"
else:
return "F"
elif step.action == "send_keys":
print("send_keys元素:" + str(step.parm))
element.send_keys(step.parm)
elif step.action == "clear":
element.clear()
else:
print("action值错误")
用例执行
##执行用例
def run(self):
for testCase in self.testCases:
print("==============开始执行用例<<" + testCase.casename + ">>==============")
self.docase(testCase)
print("==============用例执行完毕<<" + testCase.casename + ">>==============")
self.close()
##关闭浏览器
def close(self):
self.webDriver.webdriver.close()
##执行用例步骤
def docase(self, case):
for step in case.steps:
try:
if step.action == "text":
##当步骤方法为text时,设想为校验步骤
##执行elementAction方法会返回校验结果
result = self.webDriver.elementAction(step)
##记录校验结果
self.record(result=result, row=step.row)
else:
##其它动作类型
self.webDriver.elementAction(step)
except BaseException:
print("err:步骤异常提前记录,并结束这一次用例")
self.record("F", case.steps[len(case.steps)-1].row)
break
def record(self, result, row):
###只支持xls
print("***写入用例结果****")
print("用例通过情况:" + result)
book = xlrd.open_workbook(self.casepath)
wtbook = xlutils.copy.copy(book)
wtsheet = wtbook.get_sheet(1)
wtsheet.write(row, 8, result)
wtbook.save(self.casepath)
程序启动
# 遍历文件夹
def walkFile(file):
filelist = []
for root, dirs, files in os.walk(file):
# root 表示当前正在访问的文件夹路径
# dirs 表示该文件夹下的子目录名list
# files 表示该文件夹下的文件list
# 遍历文件
for f in files:
print(os.path.join(root, f))
filelist.append(f)
# # 遍历所有的文件夹
# for d in dirs:
# print(os.path.join(root, d))
return filelist
def main():
##获取用例文件信息
filelist = walkFile("./excelcase/")
for casefile in filelist:
##根据用例文件创建多个测试任务
print("==============获取用例数据==============")
casefile = './excelcase/'+casefile
caselist = testcase.getcase(casefile)
print("==============用例数据获取完成==============")
testtask = test.testClass(caselist, casefile)
testtask.run()
if __name__ == '__main__':
main()