为空手道中的数组响应声明和使用条件

时间:2020-06-25 00:53:11

标签: api testing automated-tests karate

我有一个请求,该请求根据“状态”以两种可能的结构返回响应列表。

{
    "listSize": 2,
    "itemList": [
       {
            "id": ,
            "Name": "",
            "submittedOn": "",
            "Reference": null,
            "status": "Receipted",
            "response": null
        },
        {
            "id": 12345,
            "submittedOn": "",
            "Reference": null,
            "status": "Failed",
            "response": {
                "xml": "",
                "formErrors": [
                    {
                        "error_type": "",
                        "error_location":"", 
                        "error_message": "",
                    }
                ]
            }
        }, 
     ]
}

我需要检查结构的状态为“已接收”或“失败”。在Java中,我将使用for循环和其中的if语句根据“状态”字段使用不同的条件检查响应字段。 (下面的示例)

for (int i = 0; i < response.length; i++){
   if (response[i].status.equals("receipted")){
      //do something
   }
   else{ //failed
      //do something else
   }
}

我如何在空手道中获得类似的成就?我应该使用Java Helper吗?

1 个答案:

答案 0 :(得分:1)

首先,建议您在测试中编写静态预期结果。也就是说,有多种方法可以做到这一点,这是一种:

p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

这是另一个示例:https://stackoverflow.com/a/62567412/143475

还有其他方法可以在空手道中循环播放,但并非真正为匹配而设计:https://github.com/intuit/karate#loops

这是一个涉及JSON转换以使其更易于匹配的极端示例:https://stackoverflow.com/a/53120851/143475

另请参阅:https://github.com/intuit/karate#conditional-logic