我们在编写其他模块的时候,发现和登录的还是有一些区别的
第一个区别,登录用的请求头为:
{"Content-Type":"application/x-www-form-urlencoded"}
但是添加线索模块用的是:
{"Content-Type":"application/json"}
那我们这个时候就需要对不同的请求头参数做一判断和区别:
遇到{"Content-Type":"application/x-www-form-urlencoded"}时,可以如下:
data = json.loads(sheet.cell(i,3).value)
遇到请求体{"Content-Type":"application/json"}的呢?
如下:
data = json.dumps(sheet.cell(i,3).value)
所以我们基于这个可以写一个逻辑判断
if headers['Content-Type'] == 'application/json':
data = json.dumps(json.loads(sheet.cell(i, 3).value))
else:
data = json.loads(sheet.cell(i, 3).value)
第二个区别:登录不需要token,但是其他的需要token,这个时候,我们还得调用一次get_token
headers.update({'Admin-Token': cookie})
最后代码如下:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/01
# @Author : Laopi
import json
import os
import demjson as demjson
import openpyxl
import requests
from config.read_json_file import read_json_file
from config.setting import ConfigHandler
from demo import tokenCreate
from demo.tokenCreate import get_token
from tools.yamlControl import GetYamlData
baseUrl = GetYamlData(ConfigHandler.config_path).get_yaml_data()
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
case_path = os.path.join(root_path, 'testdata')
os.chdir(case_path) # 修改工作路径
workbook = openpyxl.load_workbook('接口测试用例.xlsx') # 返回一个workbook数据类型的值
workbook._active_sheet_index =1
sheet =workbook.active
cookie = tokenCreate.get_token()
url = baseUrl['host']+sheet.cell(row=2,column=2).value
print(url)
print(sheet.max_row) #获取有多少行
for i in range(6,sheet.max_row+1):
headers = json.loads(sheet.cell(i, 2).value)
headers.update({'Admin-Token': cookie})
if headers['Content-Type'] == 'application/json':
data = json.dumps(json.loads(sheet.cell(i, 3).value))
else:
data = json.loads(sheet.cell(i, 3).value)
print(data)
response1 = requests.post(url=url, headers=headers, data=data)
print(response1.json())
下来我们改造demo4:
如下:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/01
# @Author : Laopi
import json
import os
import openpyxl
import requests
from config.setting import ConfigHandler
from demo import tokenCreate
from tools.yamlControl import GetYamlData
'''
调用登录获取token
'''
baseUrl = GetYamlData(ConfigHandler.config_path).get_yaml_data()
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
case_path = os.path.join(root_path, 'testdata')
os.chdir(case_path) # 修改工作路径
workbook = openpyxl.load_workbook('接口测试用例.xlsx') # 返回一个workbook数据类型的值
workbook._active_sheet_index =2
sheet =workbook.active
cookie = tokenCreate.get_token()
url = baseUrl['host']+sheet.cell(row=2,column=2).value
print(sheet.max_row) #获取有多少行
for i in range(6,sheet.max_row+1):
headers = json.loads(sheet.cell(i, 2).value)
headers.update({'Admin-Token': cookie})
print(headers)
if headers['Content-Type'] == 'application/json':
data = json.dumps(json.loads(sheet.cell(i, 3).value))
else:
data = json.loads(sheet.cell(i, 3).value)
print(data)
response1 = requests.post(url=url, headers=headers, data=data)
print(response1.json())
# headers={"Content-Type":"application/x-www-form-urlencoded","Admin-Token":cookie}
# data = {"type":"1","isDefault": "0","name": "123","data": {"leads_name":{"condition":"contains","value":"收11","formType":"text","name":"leads_name"}}}
# response1 = requests.post(url=url,headers=headers,data=data)
# print(response1.json())