Selenium(Python) ddt读取Excel文件数据驱动

首先,

引入xlrd模块:

 

ExcelDDT.py:

import unittest
from time import sleep

from ddt import ddt, data
from selenium import webdriver
from xlrd import open_workbook


def getExcelTestData():
openExcelFile = open_workbook("./ExcelTestData.xlsx")
# 打开Excel文件
getSheet = openExcelFile.sheet_by_name("Sheet1")
# 获取工作表
rowNumber = getSheet.nrows
# 获取行数
dataList = []
# 数据List
for i in range(1, rowNumber):
# 从第二行开始遍历每一行
dataList.append(getSheet.row_values(i))
# 把每个单元格的数值存放到dataList中
return dataList


@ddt
# ddt驱动
class MyTestCase(unittest.TestCase):

def setUp(self):
self.driver = webdriver.Firefox()
self.base_url = "https://www.baidu.com/"
self.driver.get(self.base_url)
self.driver.maximize_window()
sleep(2)

@data(*getExcelTestData())
# 传递数据
def test_something(self, data):
searchTerm, searchResult = tuple(data)
driver = self.driver
driver.find_element_by_xpath(".//*[@id='kw']").send_keys(searchTerm)
driver.find_element_by_xpath(".//*[@id='su']").click()
sleep(2)
responseText = driver.find_element_by_xpath(".//*[@id='1']/h3/a").text
self.assertEqual(responseText, searchResult)

def tearDown(self):
self.driver.close()
self.driver.quit()

if __name__ == '__main__':
unittest.main()


ExcelTestData.xlsx文件:

 


posted @ 2018-03-01 16:28  此生不换Yang  阅读(1538)  评论(0编辑  收藏  举报