1、apifox-项目设置-公共脚本添加下面的脚本
envir、email是我先设好的环境变量,此脚本的结果是将获取得到的验证码设置成环境变量verifyCode
try {
var envir = pm.environment.get("envir");//获取当前环境
var type= request.data["optType"]; // 获取接口操作类型
var email = pm.environment.get("email");//获取测试邮箱号
var info = [envir,type,email];
var result = pm.execute("redisTool.py", info);
result = result.trim();
pm.environment.set("verifyCode", result);
console.log(result);
} catch (e) {
console.error(e.message);
}
2、设置-外部程序-打开目录将redisTool.py放到目录下
redisTool.py内容:
import redis
import json
def RedisUtil(envir,user_type,email):
ip = {"dev":"192.168.0.29","uat":"0.0.0.0"}
p = {"dev":"8888","uat":"8888"}
type = {"register":"code:register","resetPass":"code:resetpass"}
host = ip[envir]
port = p[envir]
name = type[user_type]
key = email
code = "null"
try:
pool = redis.ConnectionPool(host=host, port=port, socket_timeout=10)
r = redis.StrictRedis(connection_pool=pool)
bytes_code = r.hget(name=name,key=key)
if bytes_code:
str_code = bytes_code.decode('ascii')
json_code = json.loads(str_code)
lenth = len(json_code) - 1
code = json_code[lenth]['verifyCode']
except Exception as e:
print(e)
return code
if __name__ == '__main__':
envir = "dev"
user_type = "register"
email = "156@qq.com"
code = RedisUtil(envir,user_type,email)
print(code)
3、 接口-后置操作-引用脚本,调发送验证码接口后就可以获取得到验证码