【问题标题】:Is there a if functionality in Karate framework?空手道框架中是否有 if 功能?
【发布时间】:2020-06-11 23:07:30
【问题描述】:

这里是示例代码

"xNotification": [
      {
        "code": "1234",
        "text": "Invalid Data"
        "yNotification": [
          {
            "code": "345",
            "text": "Invalid Data"
          }
        ]
      }
   ]
 }

yNotification 数组有时仅在响应中退出,当它存在时,我需要捕获代码值并将其放入变量中。我不想在它存在时断言我需要将它放在一个变量中。我是任何测试框架的新手。感谢您对这个问题的任何帮助。提前致谢。

【问题讨论】:

    标签: karate gherkin


    【解决方案1】:

    请阅读这部分文档:https://github.com/intuit/karate#conditional-logic

    例子:

    * def expected = zone == 'zone1' ? { foo: '#string' } : { bar: '#number' }
    * match response == expected
    

    编辑:这是您问题的完整解决方案:

    * def response =
    """
    { "xNotification": [
          {
            "code": "1234",
            "text": "Invalid Data",
            "yNotification": [
              {
                "code": "345",
                "text": "Invalid Data"
              }
            ]
          }
       ]
    }
    """
    * def code = karate.get('$.xNotification[0].yNotification[0].code')
    * print code
    

    【讨论】:

    • 嗨,彼得,感谢您的快速回复,但这不是我想要的。让我重新表述我的问题,有没有办法在上述响应中找到“yNotification”键是否存在或者响应中是否存在 yNotification 数组? JSON 以上是我的回应。谢谢
    • @GlenChaba 看到我的编辑。下次,请有礼貌地放入格式良好的 JSON。请阅读文档。如果路径不存在,karate.get() 将返回 null,您甚至可以以相同的语法/一行提供默认值
    • Peter,很抱歉 JSON 格式不正确,下次我会更加小心。感谢您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2023-03-15
    • 2022-11-18
    相关资源
    最近更新 更多