getExcelData.py 2.45 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:
12 13 14 15 16
        num_url = list_title.index('url')
        num_headers = list_title.index('headers')
        num_method = list_title.index('method')
        num_reqData = list_title.index('reqData')
        num_expectData = list_title.index('expectData')
taoke committed
17
        num_testPoint = list_title.index('testPoint')
18
        lis = []
taoke committed
19

20 21 22 23 24
        idx = 0
        try:
            for one in workSheet.col_values(0):
                result = ''.join(re.findall(r'[A-Za-z]', one))  # 抽取字母字符串
                if caseName == result:
taoke committed
25
                    dict0 = {"url": "", "headers": "", "method": "", "reqData": "", "expectData": ""}
26 27 28
                    dict0['url'] = workSheet.cell_value(idx, num_url)
                    dict0['headers'] = workSheet.cell_value(idx, num_headers)
                    dict0['method'] = workSheet.cell_value(idx, num_method)
taoke committed
29 30
                    dict0['reqData'] = workSheet.cell_value(idx, num_reqData)
                    dict0['expectData'] = workSheet.cell_value(idx, num_expectData)
31
                    dict0['testPoint']= workSheet.cell_value(idx, num_testPoint)
32 33

                    # json字符串转换成字典
34 35 36 37
                    try:
                        dict0['expectData'] = json.loads(dict0['expectData'])
                    except:
                        dict0['expectData'] = None
taoke committed
38

39
                    try:
taoke committed
40
                        dict0['reqData'] = json.loads(dict0['reqData'])
41 42
                    except:
                        dict0['reqData'] = None
taoke committed
43

taoke committed
44 45 46
                    try:
                        dict0['headers'] = json.loads(dict0['headers'])
                    except:
taoke committed
47
                        dict0['headers'] = None
48
                    lis.append(dict0)
49 50 51 52
                idx += 1
            return lis
        except:
            print("excle中header值或参数或期望不是json字符串")
taoke committed
53
    except:
54 55
        print("检查excle中标题是否正确")

56

taoke committed
57
if __name__ == '__main__':
taoke committed
58
    workBook = xlrd.open_workbook('../../test_case_data/bmc/bmc_testcase01_20210513.xlsx')
1  
taoke committed
59
    li = get_excelData(workBook,"积分商城","focusSuccessIntegral")
taoke committed
60 61
    print(li)

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