jmeter 中的 jmes 路径断言失败

jmes path assertion failing in jmeter

在我的 jmeter 测试中,测试收到以下 json 响应。

{"result":"success","additional-info":"{\"external-profile\":{\"email\":\"myemail@gmail.com\",\"firstname\":\"fn\",\"lastname\":\"ln\",\"portfolio\":{\"tags-of-interest\":[],\"question-created-tags\":[{\"tag\":\"un2-new tag-empty\",\"count\":1},{\"tag\":\"un2-new tag2-empty\",\"count\":1}],\"question-answered-tags\":[]}}}"}

我想检查邮件是否有路径 additional-info.exernal-profile.portfolio 以及是否有键 tags-of-interestquestion-created-tagquestion-answered-tag

我正在使用 json jmes path 但出现错误 Assertion failure message:Invalid argument type calling "keys": expected object but was null

我做错了什么?

问题是 additional-info 是一个 string 而不是一个对象。我使用 minimal-json-0.9.5beanshell post processor 脚本将字符串转换为 json 对象。

豆壳代码

import com.eclipsesource.json.*;

//prev.setSuccessful(false);
try {
    String jsonString = prev.getResponseDataAsString(); //response as string
    log.info("received json string: "+jsonString);
    
    JsonObject responseAsJsonObject = JsonObject.readFrom(jsonString); //convert response string as json
    log.info("converted to object: "+responseAsJsonObject);
    
    String additionalInfoString = responseAsJsonObject.get("additional-info").asString(); //get additional info string from json object
    log.info("additional info as string: "+additionalInfoString);
    
    JsonObject additionalInfoJsonObject = JsonObject.readFrom(additionalInfoString); //convert additional info string to json
    log.info("additional info as object: "+additionalInfoJsonObject);
    
    JsonObject externalProfileJsonObject = additionalInfoJsonObject.get("external-profile").asObject(); //get external profile object
    log.info("external profile as object: "+externalProfileJsonObject);
    
    JsonObject portfolioJsonObject = externalProfileJsonObject.get("portfolio").asObject(); //get portfolio object
    log.info("portfolio as object: "+portfolioJsonObject);
    
    JsonArray tagsOfInterest = portfolioJsonObject.get("tags-of-interest").asArray();
    log.info("tags of interest: "+tagsOfInterest);
    
    JsonArray tagsCreated = portfolioJsonObject.get("question-created-tags").asArray();
    log.info("tags created: "+tagsCreated);
    
    JsonArray tagsAnswered = portfolioJsonObject.get("question-answered-tags").asArray();
    log.info("tags tagsAnswered: "+tagsAnswered);
//  prev.setSuccessful(true);
} catch (Exception e){
    log.info("error in processing beanshell script: ", e);
    prev.setSuccessful(false);
}

只需要 2 个 JSON JMESPath Extractors:

  1. 从响应

    中将additional-info属性的内容提取到JMeter Variable

  2. external-profile属性中提取属性:

您可以使用 Debug Sampler and View Results Tree 侦听器组合查看提取的值:


请注意 since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language 用于编写脚本:

更多信息:Apache Groovy - Why and How You Should Use It