python webdriver测试框架--数据驱动txt驱动
数据驱动框架
数据驱动核心思想:程序不变,数据变
适合:程序本身容易实现,适合小功能测试里面
数据驱动txt文件驱动的方式,带报告
testdata.txt
gloryroad||光荣之路
富爸爸||爸爸
超人||动画
### testdata.txt 保存格式是utf-8
data_driver_by_txt.py
from selenium import webdriver
import time
#打开data.txt文件
with open("D:\全天课学习代码\第28次selenium(5)\数据驱动测试框架\data.txt","rb") as fp:
data = fp.readlines()
# for i in range(len(data)):
# print(data[i].decode())
# print(type(data[i].decode()))
# print(data[i].decode().split("||")[1])
# print(type(data[i].decode().split("||")[1]))
testresult = []
for i in range(len(data)):
try:
driver = webdriver.Chrome(executable_path="e:\\chromedriver")
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys(data[i].decode().split("||")[0].strip())
driver.find_element_by_id("su").click()
time.sleep(2)
assert data[i].decode().split("||")[1].strip() in driver.page_source
testresult.append(data[i].decode().strip()+u"||成功\n")
print(data[i].decode().split("||")[0] + "搜索测试用例成功")
except AssertionError as e:
print(data[i].decode().split("||")[1].strip()+"测试断言失败")
testresult.append(data[i].decode().strip() + "||断言失败\n")
except Exception as e:
print(data[i].decode().split("||")[1].strip() + "测试用例执行失败")
testresult.append(data[i].decode().strip() + "||异常失败\n")
with open(u"D:\全天课学习代码\第28次selenium(5)\数据驱动测试框架\data.txt","w") as fp:
fp.writelines(testresult)
driver.quit()
执行结果:
D:\python\python.exe D:/data_driver_by_txt.py
gloryroad搜索测试用例成功
富爸爸搜索测试用例成功
超人搜索测试用例成功
result.txt
gloryroad||光荣之路||成功
富爸爸||爸爸||成功
超人||动画||成功
修改testdata.txt使得2条用例断言失败
gloryroad||光荣之路
富爸爸||爸爸555
超人||动动
执行结果:
D:\python\python.exe D:/data_driver_by_txt.py
gloryroad搜索测试用例成功
爸爸555测试断言失败
动动漫测试断言失败
result.txt
gloryroad||光荣之路||成功
富爸爸||爸爸555||断言失败
超人||动动漫||断言失败