空手道框架中有 if 功能吗?



这是示例代码

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

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

请阅读文档的这一部分: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

最新更新