[英]Use conditional logic in karate API
In below test case we want to print get[0] response.Languages if condition fulfills在下面的测试用例中,如果条件满足,我们要打印 get[0] response.Languages
Scenario: languages
Given path 'admin/rest/core/languages/'
When method get
Then status 200
* def resp = response
* print resp
* def lang = get[0] response.Languages
* if ( response.Languages.IsDefault == '1' );
Then print lang
Response of API is as below API 的响应如下
Languages": [
{
"FullName": "English",
"ShortName": "en",
"ID": "1",
"ParentID": "0",
"IsFolder": "1",
"Icon": {
"URL": "/admin/images/flags/en.gif",
"Timestamp": "2019-08-19 13:19:07"
},
"DateFormat": "American24",
"NumberFormat": "####.##",
"IsDefault": "0",
"IsAvailable": "1",
"LCID": "2057",
"Self": "admin/rest/core.json/colors/1"
},
another condition is if IsDefault is 1 then only related language response should get printed另一个条件是,如果 IsDefault 为 1,则仅应打印相关的语言响应
Sample Code:示例代码:
Feature: Validation
Scenario:
* def resp =
"""
{
"Languages": [
{
"FullName": "English1",
"ShortName": "en",
"ID": "1",
"ParentID": "0",
"IsFolder": "1",
"Icon": {
"URL": "/admin/images/flags/en.gif",
"Timestamp": "2019-08-19 13:19:07"
},
"DateFormat": "American24",
"NumberFormat": "####.##",
"IsDefault": "0",
"IsAvailable": "1",
"LCID": "2057",
"Self": "admin/rest/core.json/colors/1"
},
{
"FullName": "English2",
"ShortName": "en",
"ID": "1",
"ParentID": "0",
"IsFolder": "1",
"Icon": {
"URL": "/admin/images/flags/en.gif",
"Timestamp": "2019-08-19 13:19:07"
},
"DateFormat": "American24",
"NumberFormat": "####.##",
"IsDefault": "1",
"IsAvailable": "1",
"LCID": "2057",
"Self": "admin/rest/core.json/colors/1"
}
]
}
"""
* def fun = function(x){ return x.IsDefault == 1 }
* def a = get resp.Languages[*]
* def res = karate.filter(a, fun)
* print res
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.