getExcelData.py 4.25 KB
Newer Older
taoke committed
1 2
import xlrd,re,json

taoke committed
3
def get_excelData(workBook,sheetName,caseName):
taoke committed
4 5
    """
    :param sheetName: sheet表名
taoke committed
6
    :param caseName:  从excle中第一列关键字(字母)
7
    :return: # [{},{},{}]
taoke committed
8 9
    """
    workSheet = workBook.sheet_by_name(sheetName)
10
    list_title = workSheet.row_values(0)
taoke committed
11
    try:
taoke committed
12 13 14 15
        num_caseNum = list_title.index('caseNum')
        num_function = list_title.index('function')
        num_interface = list_title.index('interface')
        num_priority = list_title.index('priority')
16
        num_url = list_title.index('url')
taoke committed
17 18
        num_frontInterface = list_title.index('frontInterface')
        num_frontCondition = list_title.index('frontCondition')
19
        num_method = list_title.index('method')
taoke committed
20
        num_headers = list_title.index('headers')
21
        num_reqData = list_title.index('reqData')
taoke committed
22
        num_expectResult = list_title.index('expectResult')
23
        num_expectData = list_title.index('expectData')
taoke committed
24
        num_testPoint = list_title.index('testPoint')
25 26 27 28
        num_otherExpectData =list_title.index('otherExpectData')
        num_yapiAddress = list_title.index('yapiAddress')
        num_creator = list_title.index('creator')
        num_autoCreator = list_title.index('autoCreator')
taoke committed
29

30
        lis = []
taoke committed
31

32
        idx = 0
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        for one in workSheet.col_values(0):
            result = ''.join(re.findall(r'[A-Za-z]', one))  # 抽取字母字符串
            if caseName == result:
                dict0 = {"url": "", "headers": "", "method": "", "reqData": "", "expectData": ""}
                dict0['url'] = workSheet.cell_value(idx, num_url)
                dict0['headers'] = workSheet.cell_value(idx, num_headers)
                dict0['method'] = workSheet.cell_value(idx, num_method)
                dict0['reqData'] = workSheet.cell_value(idx, num_reqData)
                dict0['expectData'] = workSheet.cell_value(idx, num_expectData)
                dict0['testPoint']= workSheet.cell_value(idx, num_testPoint)
                dict0['caseNum']=workSheet.cell_value(idx, num_caseNum)
                dict0['otherExpectData'] =workSheet.cell_value(idx, num_otherExpectData)
                dict0['function'] =workSheet.cell_value(idx, num_function)
                dict0['interface'] = workSheet.cell_value(idx, num_interface)
                dict0['priority'] = workSheet.cell_value(idx, num_priority)
                dict0['yapiAddress'] = workSheet.cell_value(idx, num_yapiAddress)
                dict0['creator'] = workSheet.cell_value(idx, num_creator)
                dict0['autoCreator'] = workSheet.cell_value(idx, num_autoCreator)
                dict0['frontInterface'] = workSheet.cell_value(idx, num_frontInterface)
                dict0['frontCondition'] = workSheet.cell_value(idx, num_frontCondition)
                dict0['expectResult'] = workSheet.cell_value(idx, num_expectResult)
54

55 56 57 58 59
                # json字符串转换成字典
                try:
                    dict0['expectData'] = json.loads(dict0['expectData'])
                except:
                    dict0['expectData'] = None
taoke committed
60

61 62 63 64
                try:
                    dict0['otherExpectData'] = json.loads(dict0['otherExpectData'])
                except:
                    dict0['otherExpectData'] = None
65

66 67 68 69
                try:
                    dict0['reqData'] = json.loads(dict0['reqData'])
                except:
                    dict0['reqData'] = None
taoke committed
70

71 72 73 74 75 76 77
                try:
                    dict0['headers'] = json.loads(dict0['headers'])
                except:
                    dict0['headers'] = None
                lis.append(dict0)
            idx += 1
        return lis
taoke committed
78
    except:
79
        list0=[{'url': '', 'headers': None, 'method': 'post', 'reqData': None, 'expectData': None, 'testPoint': '', 'caseNum': '', 'otherExpectData': None, 'function': '', 'interface': '', 'priority': '', 'yapiAddress': '', 'creator': '', 'autoCreator': '', 'frontInterface': '', 'frontCondition': '', 'expectResult': ''}]
80
        print("检查excle中标题是否正确")
81 82
        return  list0

83

84

taoke committed
85
if __name__ == '__main__':
taoke committed
86
    workBook = xlrd.open_workbook('../../test_case_data/bmc/bmc_testcase_20210513.xlsx')
87
    li = get_excelData(workBook,"账号信息基本功能","login")
88
    for i in li:
89 90
        print(i)

taoke committed
91

92
# {"Authorization": "","Content-Type":"application/x-www-form-urlencoded"}