1、应用场景:接口自动化测试中,存在参数依赖的情况,A接口的响应参数作为B接口的请求参数请求接口,这时需要从A的响应数据中将B需要用到的请求参数取出来,这时,就需要用到jsonpath_rw
2、前提:安装jsonpath_rw
pip install jsonpath_rw -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
3、实例:
from jsonpath_rw import jsonpath, parse
data={
"Code": 200,
"ErrorMessage": "",
"Data": {
"IsSuccess": true,
"Message": "数量更新成功",
"StackTrace": null,
"Data": {
"cartType": 0,
"CartId": "a47141fddd8848e1be5a281b25e613b8"
}
}
}
depend='Data.Data.CartId' #按响应数据结构编写需要查找的CartId
json_exe = parse(depend) # parse用于从一个字符串中解析出json对象
model = json_exe.find(response_data) # 返回的是list,但是不是我们想要的值
print([match.value for match in model][0])
#返回响应参数中CartId的a47141fddd8848e1be5a281b25e613b8
官方文档地址:GitHub - kennknowles/python-jsonpath-rw: A robust and significantly extended implementation of JSONPath for Python, with a clear AST for metaprogramming.
|