【问题标题】:Use conditional logic in karate API在空手道 API 中使用条件逻辑
【发布时间】:2019-11-20 20:38:06
【问题描述】:

在下面的测试用例中,如果条件满足,我们要打印 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

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"
    },

另一个条件是如果 IsDefault 为 1,则只打印相关的语言响应

【问题讨论】:

    标签: api cucumber karate


    【解决方案1】:

    示例代码:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      相关资源
      最近更新 更多