httprunner 2.x学习14-jsonpath提取(解决:ResponseObject does not have attribute: parsed_body)

前言

httprunner 2.x 版本是可以支持 jsonpath 提取器,但有个小bug一直未得到解决,会出现报错:ResponseObject does not have attribute: parsed_body

遇到问题

使用jsonpath提取器,提取返回结果,校验结果的时候,部分代码示例如下

    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [$.code, [0]]

运行会出现报错:

Traceback (most recent call last):
AttributeError: 'Response' object has no attribute 'parsed_body'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
httprunner.exceptions.ParamsError: ResponseObject does not have attribute: parsed_body

报错原因是ResponseObject 找不到 parsed_body 属性,这是框架本身的一个小BUG,但是这个框架作者一直没去维护更新,作者想主推3.x版本了,也就不再维护了。
github上已经有多个人提过issue了。https://github.com/httprunner/httprunner/issues/908

找到 response.py 下的这段代码

    def _extract_field_with_jsonpath(self, field):
        """
        JSONPath Docs: https://goessner.net/articles/JsonPath/
        For example, response body like below:
        {
            "code": 200,
            "data": {
                "items": [{
                        "id": 1,
                        "name": "Bob"
                    },
                    {
                        "id": 2,
                        "name": "James"
                    }
                ]
            },
            "message": "success"
        }

        :param field:  Jsonpath expression, e.g. 1)$.code   2) $..items.*.id
        :return:       A list that extracted from json repsonse example.    1) [200]   2) [1, 2]
        """
        result = jsonpath.jsonpath(self.parsed_body(), field)
        if result:
            return result
        else:
            raise exceptions.ExtractFailure("\tjsonpath {} get nothing\n".format(field))

修复bug

报错的原因是这句 result = jsonpath.jsonpath(parsed_body(), field) 有个parsed_body()方法写的莫名其妙的,在ResponseObject 里面并没有定义此方法。
jsonpath 第一个参数应该传一个json()解析后的对象,可以修改成 self.json就行了。
修改前

result = jsonpath.jsonpath(self.parsed_body(), field)

修改后

result = jsonpath.jsonpath(self.json, field)

由于jsonpath 提取的结果返回的是list, 如:1) [200] 2) [1, 2],我们平常大部分情况都是直接取值,不需要提取多个,于是return结果的时候,可以直接取值[0]
修改后

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

        result = jsonpath.jsonpath(self.json, field)
        if result:
            return result[0]
        else:
            raise exceptions.ExtractFailure("\tjsonpath {} get nothing\n".format(field))

jsonpath 提取和校验

jsonpath 提取返回结果,提取出匹配到的第一个值, 校验结果也一样

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

    extract:
        code: $.code
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [$.code, 0]
        - eq: ["$code", 0]

查看报告

jsonpath语法还不会的可以看这篇
https://www.cnblogs.com/yoyoketang/p/13216829.html
https://www.cnblogs.com/yoyoketang/p/14305895.html


httprunner 2.x实战教程点我 ->立即报名

posted @ 2021-06-15 10:53  上海-悠悠  阅读(990)  评论(0编辑  收藏  举报