ddt数据驱动在ui自动化中的应用一【多测师】
import xlrd from selenium import webdriver import ddt import time import unittest class Excel(object): def __init__(self,excel_path,sheet_name): self.excel_file=xlrd.open_workbook(excel_path) self.sheet =self.excel_file.sheet_by_name(sheet_name) self.sheet_name =self.sheet.name self.rows = self.sheet.nrows self.cols = self.sheet.ncols """返回单元格,计数(0,0)表示第一行,第一列的单元格""" def get_sheet_data(self,row,col): test_data = self.sheet.cell(row ,col).value """读取excel,并处理数据返回""" def read_excel(self): list=[] for row in range(2,self.rows): lists=self.sheet.row_values(row)[:self.cols] list1=[] dict={} for j in range(self.cols): list1.append(lists[j].encode('utf-8')) dict['order_currency']=list1[0].decode('utf-8') dict['order_amount']=list1[1].decode('utf-8') dict['DFSXml']=list1[2].decode('utf-8') dict['card_number']=list1[3].decode('utf-8') dict['secureCode']=list1[4].decode('utf-8') list.append(dict) return list class Base(object): def __init__(self,driver): self.driver = driver def by_xpath(self,xpath): return self.driver.find_element_by_xpath(xpath) def by_id(self,id): return self.driver.find_element_by_id(id) def inputText(self,ele,text): ele.clear() ele.send_keys(text) def getTitle(self): return self.driver.current_url def run(browser,secureCode,order_currency,order_amount,card_number,DFSXml): browser.get("https://dfs.oceanpayment.com/pages/testPay.html") page = Base(browser) page.inputText(page.by_id("secureCode"), secureCode) page.inputText(page.by_id("order_currency"),order_currency) page.inputText(page.by_id("order_amount"), order_amount) js_bom = "document.documentElement.scrollTop=500" browser.execute_script(js_bom) page.inputText(page.by_id("order_number"),card_number) jsToTop ="document.documentElement.scrollTop=-500" browser.execute_script(jsToTop) page.inputText(page.by_id("DFSXml"),DFSXml) page.by_id("btnAdd").click() alert = browser.switch_to_alert() alert.accept() time.sleep(5) print(page.getTitle()) @ddt.ddt class TestRun(unittest.TestCase): path=r"E://Card.xlsx" #windows 保存excel文件的路径,需要更改根据存放位置 st_name='Diners' # excel sheet 名称不变不改 test_data= Excel(path,st_name).read_excel() def setUp(self): opt= webdriver.ChromeOptions() opt.add_argument('--start-maximized') driver=webdriver.Chrome(options=opt) driver.implicitly_wait(10) self.driver=driver def tearDown(self): self.driver.close() @ddt.data(*test_data) def test_pay(self,dict): run(self.driver,dict["secureCode"],dict["order_currency"],dict["order_amount"],dict["card_number"],dict['DFSXml']) if __name__ == '__main__': unittest.main()