簡體   English   中英

在jmeter中使用groovy響應斷言腳本的問題

[英]Issue with groovy response assertion script in jmeter

我想使用groovy腳本為Response斷言編寫代碼,對於Response數據,如下所示:

[
    {
        "fieldId":"947bb60f",
        "id":"e7b8ad2b",
        "name":"field",
    }
]

嘗試使用下面的groovy腳本,我收到錯誤(失敗消息)。

if (!jsonResponse.keySet().containsAll(["fieldId","id","name"] )) {
         failureMessage += "The json response body has wrong structure or error msg.\n\n";

}

相同的腳本與單樹結構一起正常工作如下。 通過groovy腳本欣賞你的幫助。

[

  "fieldId":"947bb60f",
  "id":"e7b8ad2b",
  "name":"field",

]

因此,您將獲得返回的項目列表(包含單個項目)

假設您從不期望有多個項目,您可以通過以下方式檢查它的大小:

if (jsonResponse.size() != 1) {
     failureMessage += "Expected one item, got ${jsonResponse.size()}.\n\n";
}

然后,你可以抓住第一個元素:

def jsonElement = jsonResponse[0]

並檢查字段名稱:

if (jsonElement.keySet() != ["fieldId","id","name"] as Set) {
     failureMessage += "Unexpected fields in response ${jsonElement.keySet()}.\n\n";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM