Jmeter JSON路径断言

时间:2014-02-25 06:40:49

标签: json performance jmeter jmeter-plugins

下面是我的JSON响应数据,我需要使用以下响应进行断言。我尝试过多种方式来编写JSON路径&期望值。始终断言失败。我想要的是请帮助写下面数据的路径和期望值

{
    "statusCode": 200,
    "statusMessage": "Success",
    "errorMessage": "",
    "responseData": {
        "id": 15,
        "userName": "partner@tarento.com",
        "firstName": "tarento",
        "lastName": "test1",
        "phoneNumber": "1234567812",
        "email": "partner@tarento.com",
        "password": "",
        "city": "",
        "agentList": [
            {
                "id": 37,
                "userName": "Rahul.antonyRaj@tarento.com",
                "firstName": "Sanjay",
                "lastName": "rahul",
                "phoneNumber": "7411269480",
                "email": "Rahul.antonyRaj@tarento.com",
                "password": "",
                "active": true
            },
            {
                "id": 68,
                "userName": "jinesh.sumedhan@tareto.com",
                "firstName": "jinesh",
                "lastName": "sumedhan",
                "phoneNumber": "9400993826",
                "email": "jinesh.sumedhan@tareto.com",
                "password": "",
                "active": true
            },
            {
                "id": 108,
                "userName": "a.sanjayrahul@gmail.com",
                "firstName": "Rahul",
                "lastName": "Antony",
                "phoneNumber": "9994590241",
                "email": "a.sanjayrahul@gmail.com",
                "password": "",
                "active": true
            },
            {
                "id": 304,
                "userName": "a.sanjayrajish@gmail.com",
                "firstName": "Agent",
                "lastName": "Agent",
                "phoneNumber": "9025699716",
                "email": "a.sanjayrajish@gmail.com",
                "password": "",
                "active": true
            }
        ],
        "roleName": "admin",
        "sessionKey": "435tnerLt9813942160478oDse46345635#1",
        "partner": {
            "id": 1,
            "name": "Tarento",
            "cityList": [
                "bangalore",
                "mumbai"
            ],
            "phone": "1234567812",
            "url": ""
        },
        "isActive": true,
        "isDeleted": false,
        "roleId": 1,
        "countryCode": "",
        "tags": [
            {
                "tagId": 1,
                "name": "all",
                "description": "this is default tag of all driver."
            },
            {
                "tagId": 2,
                "name": "airport",
                "description": ""
            },
            {
                "tagId": 3,
                "name": "street",
                "description": "any text message"
            },
            {
                "tagId": 255,
                "name": "night",
                "description": "night"
            }
        ]
    }
}

我收到了以下回复

3 个答案:

答案 0 :(得分:4)

我发现脚本语言javascript JSR223 Assertion 是最简单的。至少你是否了解java和javascript。而且无需添加任何插件。

我的详细工作代码:

var json = JSON.parse(SampleResult.getResponseDataAsString());
if (json.statusCode != 200) {
    AssertionResult.setFailureMessage("" 
        + json.statusCode 
        + " " + json.statusMessage
        + " " + json.errorMessage);
    AssertionResult.setFailure(true);
}

答案 1 :(得分:3)

我个人更喜欢使用BSF PostProcessor与Groovy语言结合使用。有关如何使用Groovy解析JSON的示例,您可以在此处找到how to parse json using groovy

答案 2 :(得分:1)

开始关注JSONPath Assertion将测试您的statusCode

$.statusCode

200置于JSONPath断言的预期值。

这个适用于userName

$.responseData.userName
很容易,不是吗?有关更多有用示例和操作方法,请参阅Parsing JSON指南。