1、excel中数据(注意:数据是纯数字时,要将其设置成文本)
2、读取excel文件函数封装
文件名称:read_excel.py
import xlrd
class ReadExcel():
def __init__(self, excelPath, sheetName="Sheet1"):
self.data = xlrd.open_workbook(excelPath)
self.table = self.data.sheet_by_name(sheetName)
# 获取第一行作为key值
self.keys = self.table.row_values(0)
# 获取总行数
self.rowNum = self.table.nrows
# 获取总列数
self.colNum = self.table.ncols
def dict_data(self):
if self.rowNum <= 1:
print("总行数小于1")
else:
r = []
j=1
for i in range(self.rowNum-1):
s = {}
# 从第二行取对应values值
values = self.table.row_values(j)
for x in range(self.colNum):
s[self.keys[x]] = values[x]
r.append(s)
j+=1
return r
if __name__ == "__main__":
filepath = "D://gmy//seleniumTest//sel_test//cases//ddt_data.xlsx"
data = ReadExcel(filepath)
print(data.dict_data())
3、测试用例
文件名称:test_readexcel.py
import ddt
import unittest
from common.read_excel import ReadExcel
import os
from selenium import webdriver
from common.base import Base
curpath = os.path.dirname(os.path.realpath(__file__))
excelpath = os.path.join(curpath,"login.xlsx")
print(excelpath)
data = ReadExcel(excelpath)
testdata = data.dict_data()
@ddt.ddt
class Test(unittest.TestCase):
# 定位手机号
username = ("name", "loginName")
# 定位密码
psw = ("name", "loginPassWord")
# 定位登录按钮
loginbutton = ("id", "loginBtn")
# 定位提示信息
message = ("className", "toast-message")
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
cls.login = Base(cls.driver)
def setUp(self):
self.driver.get("url地址")
def tearDown(self):
#清空cookies
self.driver.delete_all_cookies()
@classmethod
def tearDownClass(cls):
cls.driver.quit()
@ddt.data(*testdata)
def test_login(self,data):
'''测试登录{0}'''
username = data['user']
password = data['psw']
exp = data['exp']
# 输入手机号
self.login.send(self.username, username)
# 输入密码
self.login.send(self.psw, password)
# 点击登录按钮
self.login.click(self.loginbutton)
# 获取结果
result = self.login.find(self.message).text
print("我是结果:", result)
self.assertEqual(result, exp)
if __name__ == '__main__':
unittest.main()
base.by 文件查看selenium+python自动化测试--登录
4、运行结果
最后,附带读取excel函数封装参考地址:https://www.cnblogs.com/yoyoketang/p/6701950.html
来源:oschina
链接:https://my.oschina.net/u/4375949/blog/3445367