python+excel接口自动化获取token并作为请求参数进行传参操作
作者:CesareCheung 时间:2022-05-19 04:10:18
1、登录接口登录后返回对应token封装:
import json
import requests
from util.operation_json import OperationJson
from base.runmethod import RunMethod
class OperationHeader:
def __init__(self, response):
self.response = json.loads(response)
def get_response_token(self):
'''
获取登录返回的token
'''
token = {"data":{"token":self.response['data']['token']}}
return token
def write_token(self):
op_json = OperationJson()
op_json.write_data(self.get_response_token())
if __name__ == '__main__':
url = "http://xxxx.com"
data = {
"username": "1111",
"password": "123456"
}
res=requests.post(url,data).json
op = OperationHeader(res)
op.write_token()
2、将登录接口返回的token存储在一个json文件中:
3、根据excel中对应的header字段(为了更容易看可以自行再封装及excel增加一列token字段)为yes时,读取保存token的文件数据,并通过将请求数据和token数据合并为一个请求数据后再作为一个请求数据发起请求:
request_data=dict(request_data,**token) # 把请求数据与登录token合并,并作为请求数据
res = self.run_method.run_main(method, url, request_data) # 再将合并的数据作为一个请求数据发起请求
补充知识:python读取Excel接口用例进行接口自动化并写入结果
前提准备:需要导入对应的库和准备Excel接口测试用例
续上:
源文件数据展示
执行后结果展示
来源:https://blog.csdn.net/weixin_42760923/article/details/100122235
标签:python,excel,token,传参
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
交互设计杂七杂八
2010-09-25 18:41:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/02-24s.jpg)
Django缓存系统实现过程解析
2021-07-22 23:18:09
python基于gevent实现并发下载器代码实例
2024-01-01 00:31:53
php中session_unset与session_destroy的区别分析
2023-07-17 21:34:56
js自动闭合html标签(自动补全html标记)
2023-08-25 07:06:35
Python实现快速排序的方法详解
2022-08-29 13:08:35
CSS实例教程:复合型CSS条状图表(下)
2010-01-23 12:52:00
javascript判断一个元素是否数组
2009-07-31 12:48:00
Javascirpt打造“互动指针”特效
2013-08-06 07:37:52
pycharm设置当前工作目录的操作(working directory)
2023-07-14 00:06:34
![](https://img.aspxhome.com/file/2023/6/93996_0s.jpg)
Python实现堆排序的方法详解
2023-12-02 07:43:20
![](https://img.aspxhome.com/file/2023/9/84119_0s.jpg)
详解Go 将在下个版本支持新型排序算法pdqsort
2023-10-07 23:49:40
![](https://img.aspxhome.com/file/2023/8/103028_0s.png)
python怎么自定义捕获错误
2022-05-25 02:57:35
如何编写一个只在Web服务关闭时执行的程序?
2009-11-08 19:03:00
Python破解网站登录密码脚本
2022-09-29 23:21:38
![](https://img.aspxhome.com/file/2023/8/93458_0s.png)
VS2008 和.NET 3.5 Beta2常见问题的解决方案
2007-09-23 12:33:00
Linux下为不同版本python安装第三方库
2023-11-12 05:04:35
10个超实用jQuery插件资源
2009-07-17 18:54:00
网页设计的十大戒律
2008-08-10 17:55:00
python yield迭代器详解
2023-12-06 09:36:50