[Jmeter]---JsonPath表达式提取响应&断言

时间:2022-07-28
本文章向大家介绍[Jmeter]---JsonPath表达式提取响应&断言,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

测试过程中会遇到一些复杂的json响应。比如多层list嵌套时的取值的问题。本篇主要讲述如何运用JsonPath表达式来解决多层嵌套取值的问题。

语法参考 https://goessner.net/articles/JsonPath/

Mock实例

https://easy-mock.com/mock/5d5128dd7d20f4574035dc71/example/etc_mock

{
    "response": [
        {
            "total": 4
        },
        {
            "location": "Beijing",
            "detail": {
                "area": [
                    {
                        "date": "2018-06",
                        "city": "北京"
                    }
                ],
                "province": "北京",
                "type": "domestic",
                "operator": "联通"
            }
        },
        {
            "location": "Shanghai",
            "detail": {
                "area": [
                    {
                        "date": "2018-11",
                        "city": "上海"
                    }
                ],
                "province": "上海",
                "type": "domestic",
                "operator": "联通"
            }
        },
        {
            "location": "Hangzhou",
            "detail": {
                "area": [
                    {
                        "date": "2019-04",
                        "city": "杭州"
                    }
                ],
                "province": "杭州",
                "type": "domestic",
                "operator": "联通"
            }
        },
        {
            "location": "Hangzhou",
            "detail": {
                "area": [
                    {
                        "date": "2019-04",
                        "city": "杭州"
                    }
                ],
                "province": "杭州",
                "type": "domestic",
                "operator": "联通"
            }
        }
    ],
    "responseHeader": {
        "time": 1561284795013,
        "version": "V1.1.0",
        "status": 200
    }
}

$..city:列出所有省份

..response[1].detail 提取response中第二个list中的detail

$..[?(@.location=="Shanghai")] 筛选出location为Shanghai的所有list数据

$..response[:3] 筛选前三条数据

注意:..response[:3] 是筛选出前三条数据 ;..response[3] 是排除前三条数据;

添加JSON提取器

添加JSON断言

实际运用结果