接口自动化测试首先需要安装RequestsLibrary库
pip install robotframework-requests
「Body Data格式」的实践中,使用了自定义的library,兼容了socket协议;关键字封装里面使用了python代码段,兼容了webservice协议;
这次就分享下library和封装的关键字,具体的case牵涉到业务层数据,就不展示了。
自定义library:除了有socket之外,还有其他3个关键字,也一起分享了
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import socket
import json
import base64
import os
import sys
import cx_Oracle
class YCKJ_Library():
def socket_api(self,ip,port,para):
'''
socket协议的接口测试
ip:ip地址,port:端口,para:入参
Examples:
| ${res} | Socket Api | ip | port | ${para} |
'''
para_len = para.encode('utf-8')
filesize_bytes = len(para_len)
a =str(filesize_bytes).zfill(8)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = int(port)
para = '%s%s' % (a,para)
print (para)
server_address = (ip,port)
print ('Connecting to %s:%s.'% server_address)
sock.connect(server_address)
message = para.encode("utf-8")
sock.sendall(message)
data = sock.recv(1024)
sock.close()
return (data.decode('utf-8'))
def getAllImages(self,folder):
imageList=[]#得到某个文件夹下jgp格式的文件
assert os.path.exists(folder)
assert os.path.isdir(folder)
for files in os.listdir(folder):
if files.endswith(".jpg"):
imageList.append(files)
return imageList
def base_64(self):
base_dir =os.path.dirname(__file__)#获取当前文件夹的绝对路径
base_dir = os.path.abspath(os.path.join(base_dir,os.path.pardir))
print(base_dir)
a=Socket_Library().getAllImages(base_dir+'/res/image')
print (a)
imageLt=[]
for ig in a:
file_path = os.path.join(base_dir+'/res/image',ig) #获取base_dir+'/image'文件夹内的文件
f=open(file_path,'rb') #二进制方式打开图文件
lsReadImage_f=base64.b64encode(f.read())#读取文件内容,转换为base64编码
f.close()#关闭文件
if len(imageLt)< 1:#限制转换的图片最多为1张;
imageLt.append(lsReadImage_f)
returnimageLt
def oracle_utf8(self):
#python3 目前不用也可以 import importlib importlib.reload(sys)
#python2 使用import imp imp.reload(sys)
#python2 使用 sys.setdefaultencoding('utf-8')
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
def dir(self):
base_dir =os.path.dirname(__file__)#获取当前文件夹的绝对路径
base_dir = os.path.abspath(os.path.join(base_dir,os.path.pardir))
print(base_dir)
a=base_dir+'/res'
return a
封装的关键字:
*** Setting ***
Library RequestsLibrary
Library ../ext/YCKJ_Library.py
*** Variable ***
${SERVER} http://127.0.0.1
${hostname} 127.0.0.1
${hostport} 8080
*** Keyword ***
http_post_all_json
[Documentation] 适用于http协议的post请求方式,且请求入参可以为xml和json格式;返回结果(all)是json格式
... Content-Type=application/json
[Arguments] ${path} ${request} ${expect}
${dict} Create Dictionary Content-Type=application/json
Create Session api ${SERVER} ${dict}
${total} Set Variable ${request}
${addr} Post Request api ${path} data=${total}
log ${addr.status_code}
log ${addr.content}
Should Be Equal As Strings ${addr.status_code} 200
${responsedata} To Json ${addr.content}
${wq} evaluate json.dumps(${responsedata},ensure_ascii=False) modules=json
Should Contain ${wq} ${expect}
log ${wq}
Delete All Sessions
http_post_req_url_res_json
[Documentation] 适用于http协议的post请求方式,且请求(req)入参:url传递参数 返回结果(res)是json格式
... Content-Type=application/x-www-form-urlencoded
[Arguments] ${path} ${request} ${expect}
${dict} Create Dictionary Content-Type=application/x-www-form-urlencoded
Create Session api ${SERVER} ${dict}
${total} Set Variable ${request}
${addr} Post Request api ${path} data=${total}
log ${addr.status_code}
log ${addr.content}
Should Be Equal As Strings ${addr.status_code} 200
${responsedata} To Json ${addr.content}
${wq} evaluate json.dumps(${responsedata},ensure_ascii=False) modules=json
Should Contain ${wq} ${expect}
log ${wq}
Delete All Sessions
webservice
[Documentation] 适用于webservice协议
[Arguments] ${path} ${request} ${expect}
${total} Set Variable ${request}
${wq} Evaluateurllib.request.urlopen(urllib.request.Request('${SERVER}${path}',data='${total}'.encode('utf-8'),headers={'Content-Type':'text/xml'})).read().decode('utf-8') modules=urllib
Should Contain ${wq} ${expect}
Delete All Sessions
socket
[Documentation] 适用于socket协议
[Arguments] ${request} ${expect}
${para} Set Variable ${request}
${wq} Socket Api ${hostname} ${hostport} ${para}
Should Contain ${wq} ${expect}
Delete All Sessions
base_6
${base_64} Base 64
Set Suite Variable ${base_64[0]}