import unittest from selenium import webdriver from ddt import ddt,data,unpack import time from aaa import readxls test_data = readxls() @ddt class TestMath(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Firefox() cls.driver.get('http://www.baidu.com') @classmethod def tearDownClass(cls): cls.driver.quit() def setUp(self): self.driver.find_element_by_id('kw').clear() print('开始-------------') def tearDown(self): print('结束-------------') @data(*test_data) @unpack #在“脱外套”之后,针对你拿到的每一条数据根据逗号进行拆分 def test_print_data(self,a,b): #需要按拆出来的每条数据内的个数进行传参 self.driver.find_element_by_id('kw').send_keys(str(b)) self.driver.find_element_by_id('su').click() time.sleep(10) self.assertEqual(self.driver.title,b+'_百度搜索')
import xlrd def readxls(): xl=xlrd.open_workbook('C:\Users\del\Desktop\book.xls') #打开Excel表格 sheet1 = xl.sheet_by_name('Sheet1') #指定表1 listdata= [] #定义空列表,用来存放读取出来的每行数据 for i in range(1, 4): #循环1~4(不包含4),即循环3次 j = sheet1.row_values(i, 0, 2) #读取第i行的第0~2列(不含第2列) listdata.append(j) #读一行追加一行存入listdata中 # print(listdata) return listdata #返回列表 if __name__ == "__main__": print(readxls())
执行结果: