每个人优化自己的自动化框架都不一样,但一样的是,如何发现问题,进行思考!!!
我们写到现在突然发现很多时候,我们都需要在写接口自动化的时候,把请求地址都得复制粘贴一次,如下面的几个接口自动化的demo
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/01
# @Author : laopi
import requests
url = "http://192.168.0.134:8081/login"
headers={"Content-Type":"application/x-www-form-urlencoded"}
data={"username":"admin","password":"123456"}
response = requests.post(url=url,
headers=headers,
data=data)
print(response.json()['Admin-Token'])
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/01
# @Author : Laopi
import json
import requests
'''
调用登录获取token
'''
url = "http://192.168.0.134:8081/login"
headers={"Content-Type":"application/x-www-form-urlencoded"}
data={"username":"admin","password":"123456"}
response = requests.post(url=url,
headers=headers,
data=data)
cookie = response.json()['Admin-Token']
url = "http://192.168.0.134:8081/CrmLeads/addOrUpdate"
headers={"Content-Type":"application/json","Admin-Token":cookie}
data ={"entity": {
"leads_name": "收到就好2",
"telephone": "15991710522",
"mobile": "15991710522",
"address": "111",
"next_time": "2024-01-08 00:00:00",
"remark": "11111111"
},
"field": [
{
"fieldId": 2,
"formType": "select",
"isNull": 0,
"name": "线索来源",
"options": "促销,搜索引擎,广告,转介绍,线上注册,线上询价,预约上门,电话咨询,邮件咨询",
"isUnique": 0,
"inputTips": "null",
"type": 3,
"value": "搜索引擎",
"fieldType": 0,
"fieldName": "线索来源",
"setting": [
"促销",
"搜索引擎",
"广告",
"转介绍",
"线上注册",
"线上询价",
"预约上门",
"电话咨询",
"邮件咨询"
]
},
{
"fieldId": 5,
"formType": "select",
"isNull": 0,
"name": "客户行业",
"options": "IT,金融业,房地产,商业服务,运输/物流,生产,政府,文化传媒",
"isUnique": 0,
"inputTips": "null",
"type": 3,
"value": "房地产",
"fieldType": 0,
"fieldName": "客户行业",
"setting": [
"IT",
"金融业",
"房地产",
"商业服务",
"运输/物流",
"生产",
"政府",
"文化传媒"
]
},
{
"fieldId": 6,
"formType": "select",
"isNull": 0,
"name": "客户级别",
"options": "A(重点客户),B(普通客户),C(非优先客户)",
"isUnique": 0,
"inputTips": "null",
"type": 3,
"value": "A(重点客户)",
"fieldType": 0,
"fieldName": "客户级别",
"setting": [
"A(重点客户)",
"B(普通客户)",
"C(非优先客户)"
]
}
]
}
response1 = requests.post(url=url,headers=headers,data=json.dumps(data))
print(response1.json())
这时候,我们就想到一个问题,既然这些都是公共的,那为什么我不能把url几个地方都用到的,提取出来,作为公共的配置,哪怕下次环境换了IP地址,我也不用一个个去修改代码,直接去修改配置文件就可以了。