python selenium 框架说明_基于python+selenium的框架思路(二)
#coding:utf-8
from util importObjectMap, ExcelUtilimportxlrd, xlwtimporttimefrom xlutils.copy importcopydefrun_test():from selenium importwebdriver
start_time=time.time()#讀取excel中的關(guān)鍵字的值,定位方式的值,定位表達(dá)式,和操作值等參數(shù)值。然后將參數(shù)值傳到對(duì)應(yīng)關(guān)鍵字方法中
excelFile = xlrd.open_workbook(r"D:KeyWordsFrameWorkestScriptssearch.xlsx", formatting_info=True)
sheet0= excelFile.sheet_by_index(0) #獲取第一個(gè)sheet頁(yè)
#循環(huán)第一個(gè)sheet頁(yè),其中每一行代表一條用例
for row0 in range(1, sheet0.nrows):#一定要在這個(gè)for循環(huán)里面再寫一次excelFile,要不然執(zhí)行多條用例時(shí)只會(huì)在最后一條用例寫入結(jié)果
excelFile = xlrd.open_workbook(r"D:KeyWordsFrameWorkestScriptssearch.xlsx", formatting_info=True)
driver=webdriver.chrome()
case_name= sheet0.row_values(row0)[3] #獲取用例對(duì)應(yīng)的sheet頁(yè)
sheet = excelFile.sheet_by_name(case_name) #從第一個(gè)sheet中獲取對(duì)應(yīng)的用例名稱找到對(duì)應(yīng)的用例sheet
maxRows = sheet.nrows #獲取用例的最大行數(shù),也就是步驟數(shù)
successfulSteps = 0 #定義執(zhí)行成功的步驟數(shù)
retDict = {} #存放用例結(jié)果的字典
if sheet0.row_values(row0)[4] == 'y': #只執(zhí)行標(biāo)記為y的用例
#print case_name
for row in range(1, maxRows): #循環(huán)用例步驟
keyword = sheet.row_values(row)[2] #讀取關(guān)鍵字列
#print keyword
locationType = sheet.row_values(row)[3] #獲取定位方式
if locationType not in ['id','xpath','name']:passlocatorExpression= sheet.row_values(row)[4] #獲取定位表達(dá)式的值
operationValue = sheet.row_values(row)[5] #獲取操作值
#dir(ObjectMap)獲取該模塊的所有方法和變量
#print dir(ObjectMap)
#遍歷ObjectMap中的方法和屬性,如果有名稱一致的則通過(guò)反射調(diào)用對(duì)應(yīng)方法執(zhí)行關(guān)鍵字
for i indir(ObjectMap):if keyword ==i:#print i
#要用到反射機(jī)制,通過(guò)函數(shù)名字符串調(diào)用對(duì)應(yīng)方法:http://www.liujiangblog.com/course/python/48
ifhasattr(ObjectMap, keyword):#print '有這個(gè)方法'
func =getattr(ObjectMap, keyword)try:
func(driver, locationType, locatorExpression, operationValue)print '%s:execute OK' %keyword#執(zhí)行每個(gè)關(guān)鍵字成功后,成功步數(shù)+1
successfulSteps += 1
exceptException:print '%s:excute ERROR' %keywordfinally:#如果成功的步驟數(shù)等于用例的步驟數(shù),則整條用例執(zhí)行成功,寫入執(zhí)行結(jié)果
if successfulSteps == sheet.nrows - 1:
retDict['result'] = 'PASS'
print retDict['result']
end_time=time.time()
take_time= end_time -start_timeprinttake_time#向已經(jīng)存在的excel中寫入時(shí),需要先copy,copy后sheet頁(yè)獲取sheet方法為get_sheet,與
#原來(lái)的獲取方法不一樣
excleFileCopy =copy(excelFile)
case_sheet=excleFileCopy.get_sheet(0)
case_sheet.write(row0,5, take_time)
case_sheet.write(row0,6, retDict['result'])#寫入后需要save
excleFileCopy.save(r"D:KeyWordsFrameWorkestScriptssearch.xlsx")print 'pass write is ok'
else:
retDict['result'] = 'FAIL'
print retDict['result']
end_time=time.time()
take_time= end_time -start_timeprinttake_time
excleFileCopy=copy(excelFile)
case_sheet=excleFileCopy.get_sheet(0)
case_sheet.write(row0,5, take_time)
case_sheet.write(row0,6, retDict['result'])
excleFileCopy.save(r"D:KeyWordsFrameWorkestScriptssearch.xlsx")print 'fail write is ok'
else:raiseException
driver.quit()if __name__ == '__main__':
run_test()
總結(jié)
以上是生活随笔為你收集整理的python selenium 框架说明_基于python+selenium的框架思路(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 内存回收机制_【第1题】
- 下一篇: python最好的教程_喜大普奔~可能是