Robot Framework接口自动化案例分享③——L3公共层
一、Public.py
Public.py中的函数方法是是通过python封装,用于读取TestData.xlsx文件中的数据。
# _*_ coding:utf-8 _*_ import random, string, xlrd, os from faker import Faker def update_value1(dict, key, str): "修改data中1个值" dict[key] = str return dict def update_value2(dict, key1, key2, str): "修改data中2个值" dict[key1][key2] = str return dict def randomint(): "生成随机整数" return str(random.randint(0, 1000000)) def randomstr(min, max): "生成随机字符串" return ''.join(random.sample(string.ascii_letters, random.randint(int(min), int(max)))) def randomch(): "生成随机中文" f = Faker(locale='zh_CN') return f.name() def randomsp(): "生成随机特殊字符" return ''.join(random.sample(string.punctuation, random.randint(5, 10))) def get_headers(sheet, key): "获取headers信息" excel = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Testdata.xlsx') # 获取用例文件路径 file = xlrd.open_workbook(excel) sheet = file.sheet_by_name(sheet) # 获得指定sheet数据 col_value1 = sheet.col_values(0) # 获取第1列值 col_value2 = sheet.col_values(1) nrows = sheet.nrows # 获取当前sheet行数 dict = {} for i in range(0, nrows): # dict[col_value1[i]] = col_value2[i] return dict[key] def get_xlsx(sheet): "获取指定Excel数据" excel = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Testdata.xlsx') # 获取用例文件路径 file = xlrd.open_workbook(excel) list = [] sheet = file.sheet_by_name(sheet) # 获得指定sheet数据 row_value1 = sheet.row_values(0) # 获取第1行的标题 nrows = sheet.nrows # 获取当前sheet行数 ncols = sheet.ncols # 获取当前sheet列数 for i in range(1, nrows): # 从第2行遍历当前sheet row = sheet.row_values(i) # 获取行数据 dict = {} # 创建空字典 for j in range(0, ncols): # 遍历sheet列,组成字典 if row_value1[j] == 'NO.' or row_value1[j] == 'code': dict[row_value1[j]] = int(row[j]) else: dict[row_value1[j]] = row[j] # 从第一列开始,将每一列的数据与第1行的数据组成一个键值对,形成字典 list.append(dict) # 将字典添加list中 return list def get_value(all_list, key1, value1, key2): """ 获取属性值 all_list:列表 key1:已知key value1:已知value值 key2:返回key的value值 """ for i in all_list: if i[key1] == value1: return i[key2]
二、L3公共层.robot
L3公共层.robot文件是封装的获取headers方法、GET方法、POST方法、PUT方法、DELETE方法、断言方法。
*** Settings *** Library RequestsLibrary Library Public.py Library json *** Variables *** ${air} ${EMPTY} *** Keywords *** Headers ${x-client-id} get_headers info x-client-id #获取x-client-id ${login_header} Create Dictionary x-client-id=${x-client-id} Content-Type=application/json #登录信息头 ${login_host} get_headers info login_host #获取登录host ${login_path} get_headers info login_path #获取登录path ${login_data} get_headers info login_data #获取登录账号、密码 ${host} get_headers info host #获取主地址 Set Global Variable ${host} Create Session api ${login_host} ${login_header} verify=True #用户登录 ${login_info} Post Request api ${login_path} data=${login_data} Set Global Variable ${login_info} ${x-key-hash} get_headers info x-key-hash_http ${headers} Create Dictionary x-api-key=${login_info.json()['x_api_key']} x-key-hash=${x-key-hash} Authorization=Bearer ${login_info.json()['access_token']} Content-Type=application/json;charset=utf-8 #头信息 Set Global Variable ${headers} Get_method [Arguments] ${path} ${params} Create Session api ${host} verify=True #创建一个session,连接某个服务器 ${info} Get Request api ${path} ${headers} params=${params} #发送GET请求 log ${info.json()} [Return] ${info} # 返回GET请求结果 Post_method [Arguments] ${path} ${data} # 头信息,路径,传入数据 Create Session api ${host} ${headers} verify=True #创建一个session,连接某个服务器 ${info} Post Request api ${path} data=${data} #发送POST请求 log ${info.json()} [Return] ${info} # 返回POST请求结果 Put_method [Arguments] ${path} ${data} Create Session api ${host} ${headers} verify=True ${info} Put Request api ${path} data=${data} log ${info.json()} [Return] ${info} Delete_method [Arguments] ${path} ${data} # 头信息,路径,传入数据 Create Session api ${host} ${headers} verify=True #创建一个session,连接某个服务器 ${info} Delete Request api ${path} data=${data} #发送POST请求 log ${info.json()} [Return] ${info} # 返回POST请求结果 Assert [Arguments] ${status_code} ${expected_code} Should Be Equal As Strings ${status_code} ${expected_code}
—————————————————————————————— 选择正确的事、再把事做正确
——————————————————————————————