日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python selenium 框架说明_基于python+selenium的框架思路(二)

發(fā)布時間:2024/9/19 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python selenium 框架说明_基于python+selenium的框架思路(二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

#coding:utf-8

from util importObjectMap, ExcelUtilimportxlrd, xlwtimporttimefrom xlutils.copy importcopydefrun_test():from selenium importwebdriver

start_time=time.time()#讀取excel中的關鍵字的值,定位方式的值,定位表達式,和操作值等參數(shù)值。然后將參數(shù)值傳到對應關鍵字方法中

excelFile = xlrd.open_workbook(r"D:KeyWordsFrameWorkestScriptssearch.xlsx", formatting_info=True)

sheet0= excelFile.sheet_by_index(0) #獲取第一個sheet頁

#循環(huán)第一個sheet頁,其中每一行代表一條用例

for row0 in range(1, sheet0.nrows):#一定要在這個for循環(huán)里面再寫一次excelFile,要不然執(zhí)行多條用例時只會在最后一條用例寫入結(jié)果

excelFile = xlrd.open_workbook(r"D:KeyWordsFrameWorkestScriptssearch.xlsx", formatting_info=True)

driver=webdriver.chrome()

case_name= sheet0.row_values(row0)[3] #獲取用例對應的sheet頁

sheet = excelFile.sheet_by_name(case_name) #從第一個sheet中獲取對應的用例名稱找到對應的用例sheet

maxRows = sheet.nrows #獲取用例的最大行數(shù),也就是步驟數(shù)

successfulSteps = 0 #定義執(zhí)行成功的步驟數(shù)

retDict = {} #存放用例結(jié)果的字典

if sheet0.row_values(row0)[4] == 'y': #只執(zhí)行標記為y的用例

#print case_name

for row in range(1, maxRows): #循環(huán)用例步驟

keyword = sheet.row_values(row)[2] #讀取關鍵字列

#print keyword

locationType = sheet.row_values(row)[3] #獲取定位方式

if locationType not in ['id','xpath','name']:passlocatorExpression= sheet.row_values(row)[4] #獲取定位表達式的值

operationValue = sheet.row_values(row)[5] #獲取操作值

#dir(ObjectMap)獲取該模塊的所有方法和變量

#print dir(ObjectMap)

#遍歷ObjectMap中的方法和屬性,如果有名稱一致的則通過反射調(diào)用對應方法執(zhí)行關鍵字

for i indir(ObjectMap):if keyword ==i:#print i

#要用到反射機制,通過函數(shù)名字符串調(diào)用對應方法:http://www.liujiangblog.com/course/python/48

ifhasattr(ObjectMap, keyword):#print '有這個方法'

func =getattr(ObjectMap, keyword)try:

func(driver, locationType, locatorExpression, operationValue)print '%s:execute OK' %keyword#執(zhí)行每個關鍵字成功后,成功步數(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中寫入時,需要先copy,copy后sheet頁獲取sheet方法為get_sheet,與

#原來的獲取方法不一樣

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的框架思路(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。