1. 页面
打开P_cases.html,找到id为right_div标签,从P_apis.html复制
2. 修改库表,models.py,DB_step
同步并生效:
3. 初始化js函数
4. 后端方法views.py
现在来测试一下:保存,展开等 功能全部正常
4. 发送执行
实现方案:
1. 在demo函数中直接调用views.py中的登陆态调用函数,只需要传入对应的项目id即可得到需要的登陆态字段
2. 要判断本次用例是否已调用且最多只调一次,并且要把登陆态字段变成可随时调用的公共变量
3. 仿照接口库普通接口插入登陆态变量得到方式 进行插入到url,header,请求体中。
设计完毕,开始实现:
登陆态的调用脚本,因为要受控制,所以变量要在setupclass中:
login_res默认为空字符串,当下面demo开始调用这个login_res时进行判断,若空字符串,则发起调用实际请求,否则证明已生成,则会直接使用。
所以在demo中的真实发送请求前加入这么一段:
# 登陆态
api_login = step.api_login
if api_login == 'yes':
if self.login_res == '':
project_id = DB_cases.objects.filter(id=DB_step.objects.filter(id=step.id)[0].Case_id)[0].project_id
from apitest.views import project_login_send_for_other
self.login_res = project_login_send_for_other(project_id)
print('进行了调用')
else:
pass
print(self.login_res)
else:
pass
print('【login_res】:', self.login_res)
# 发送请求
from apitest.views import api_send_basic
response = api_send_basic(api_method, api_host, api_url, api_headers, api_body_method, api_body, self.login_res)
res = response.text
print('【返回体】:', res)
2个步骤都进行了实际调用,浪费资源,甚至会导致被服务器当作恶意登陆封号。解决办法:全局函数和变量
首先删除掉没用的setupclass函数!
demo函数登陆态部分进行如下修改
# 登陆态
api_login = step.api_login
if api_login == 'yes':
try:
eval("login_res")
print('已调用过')
except:
print('未调用过')
project_id = DB_cases.objects.filter(id=DB_step.objects.filter(id=step.id)[0].Case_id)[0].project_id
from apitest.views import project_login_send_for_other
login_res = project_login_send_for_other(project_id)
else:
login_res = {}
print('【login_res】:', login_res)
# 发送请求
from apitest.views import api_send_basic
response = api_send_basic(api_method, api_host, api_url, api_headers, api_body_method, api_body, login_res)
res = response.text
print('【返回体】:', res)
5. 插入到url/header/body中:此处不用修改,参考用例库
6. 优化header判空
全部加上下图:api_send()、project_login_send()、project_login_send_for_other()、api_send_home()、demo()、error_request()
if ts_header == '': ts_header = '{}'