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(‘./TextData.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
# 数据驱动
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()
搜索词,响应
中国,中国_百度百科
美国,美国_百度百科
英国,英国_百度百科