import csv
import unittest
from time import sleep from ddt import ddt, data, unpack
from selenium import webdriver def getCsvData():
# 读取CSV文件
value_rows = []
with open('./CsvTestData.csv', encoding='UTF-8') as f:
f_csv = csv.reader(f)
next(f_csv)
for r in f_csv:
value_rows.append(r)
return value_rows @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(*getCsvData())
# 测试数据 @unpack
# 解包
def test_something(self, searchTerm, searchResult):
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()
CsvTestData.csv文件:

Selenium(Python) ddt读取CSV文件数据驱动-LMLPHP

05-11 15:20