robot Framework 从excel中读取数据

作者: _夏兮 | 来源:发表于2016-10-11 20:04 被阅读1571次

    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

    相关文章

      网友评论

      本文标题:robot Framework 从excel中读取数据

      本文链接:https://www.haomeiwen.com/subject/twpbyttx.html