从零学习前端开发 2018-03-01
引入xlrd模块:
ExcelDDT.py:
import unittest<br />from time import sleep<br /><br />from ddt import ddt, data<br />from selenium import webdriver<br />from xlrd import open_workbook<br /><br /><br />def getExcelTestData():<br /> openExcelFile = open_workbook("./ExcelTestData.xlsx")<br /> # 打开Excel文件<br /> getSheet = openExcelFile.sheet_by_name("Sheet1")<br /> # 获取工作表<br /> rowNumber = getSheet.row<br /> # 获取行数<br /> dataList = []<br /> # 数据List<br /> for i in range(1, rowNumber):<br /> # 从第二行开始遍历每一行<br /> dataList.append(getSheet.row_values(i))<br /> # 把每个单元格的数值存放到dataList中<br /> return dataList<br /><br /><br />@ddt<br /># ddt驱动<br />class MyTestCase(unittest.TestCase):<br /><br /> def setUp(self):<br /> self.driver = webdriver.Firefox()<br /> self.base_url = "https://www.baidu.com/"<br /> self.driver.get(self.base_url)<br /> self.driver.maximize_window()<br /> sleep(2)<br /><br /> @data(*getExcelTestData())<br /> # 传递数据<br /> def test_something(self, data):<br /> searchTerm, searchResult = tuple(data)<br /> driver = self.driver<br /> driver.find_element_by_xpath(".//*[@id='kw']").send_keys(searchTerm)<br /> driver.find_element_by_xpath(".//*[@id='su']").click()<br /> sleep(2)<br /> responseText = driver.find_element_by_xpath(".//*[@id='1']/h3/a").text<br /> self.assertEqual(responseText, searchResult)<br /><br /> def tearDown(self):<br /> self.driver.close()<br /> self.driver.quit()<br /><br />if __name__ == '__main__':<br /> unittest.main()<br /><br /><br />ExcelTestData.xlsx文件: