import xdrlib,xlrd
class Mykey(object):
def _open_excel(self,path)
try:
work_book = xlrd.open_workbook(path,'rb')
except Exception,e:
print str(e)
raise Exception("Can not open work book! Pls check it at path: %s" % path)
return work_book
def _open_sheet_by_index(self,work_book,index):
try:
sheet = work_book.sheet_by_index(index)
except:
raise Exception("Can not open sheet by index %s" % index)
return sheet
def _open_sheet_by_name(self,work_book,sheet_name):
sheet=None
for st_name in work_book.sheet_names():
if str(st_name) == sheet_name:
sheet = work_book.sheet_by_name(sheet_name)
if sheet is None:
raise Exception("Can not open sheet by name %s" % sheet_name)
return sheet
def _get_data_from_sheet(self,sheet,col_index=2):
nrows = sheet.nrows? #行數
print nrows
ncols = sheet.ncols? #列數
if col_index > ncols:
raise Exception("Can not find %d cols from sheet %s" % (col_index, sheet.name))
#? ? ? ? colnames = sheet.row_value(0) #第一行的數據
list1=[]
list2=[]
for rownum in range(1,nrows):
row_col0 = sheet.row(rownum)[0].value
row_col1 = sheet.row(rownum)[1].value
list1.append(row_col0)
list2.append(row_col1)
#? ? ? ? ? ? print list2
dict_data = dict(zip(list1,list2))
print dict_data
return dict_data
def _get_testdata_from_sheet(self,sheet,col_index=3):
nrows = sheet.nrows? #行數
print nrows
ncols = sheet.ncols? #列數
if col_index > ncols:
raise Exception("Can not find %dth cols from sheet %s" % (col_index, sheet.name))
list1=[]
list2=[]
for rownum in range(1,nrows):
for colnum in range (0,ncols):
list1.append(sheet.row(rownum)[colnum].value)
list2.append(list1)
list1=[]
print list2
return list2
def get_testdata_from_excel_by_index(self,path,index):
'''
通過sheet index 從excel里讀取數據
path excel文件路徑,index 第幾個sheet
返回數據 數據為字典類型
Example:
|Get Testdata Form Excel By Index | path |index=0|
|Get Testdata Form Excel By Index | text.xls |
'''
work_book = self._open_excel(path)
sheet = self._open_sheet_by_index(work_book,index)
dict_data = self._get_data_from_sheet(sheet)
return dict_data
def get_testdata_from_excel_by_name(self,path,sheet_name=u'Sheet1'):
'''
通過sheetname 從excel里讀取數據
path excel文件路徑,sheet_name sheet 名字
返回數據 數據為字典類型 用于讀取用戶數據
Example:
|Get Testdata Form Excel By Index | path |sheet_name=u'Sheet1'|
|Get Testdata Form Excel By Index | text.xls |index=0|
'''
work_book = self._open_excel(path)
sheet = self._open_sheet_by_name(work_book,sheet_name)
dict_data = self._get_data_from_sheet(sheet)
return dict_data
def get_testdata_from_excel(self,path,sheet_name=u'Sheet1'):
'''
從excel里讀取數據
path excel文件路徑,sheet_name sheet 名字
返回數據 數據為list [[api1,params1,expect1,descript1],[api2,params2,expect2,descript2],[api3,params3,expect3],...]
[api1,params1,expect1,descript1] 相當于一條testcase
Example:
|Get Testdata Form Excel| path |sheet_name=u'Sheet1'|
|Get Testdata Form Excel| text.xls |
'''
work_book = self._open_excel(path)
sheet = self._open_sheet_by_name(work_book,sheet_name)
list_data = self._get_testdata_from_sheet(sheet)
return list_data
以上是從excel讀取數據的code,如何自定義關鍵字?詳見robot Framework 自定義關鍵字
excel表格格式:
UserMgmt_by_gcf.xls
keyworld:
Get Value Testdata From Sheet Name
使用:建.xls放在suit的同級目錄下
testcase