【问题标题】:How can i use logical OR for json array response我如何对 json 数组响应使用逻辑 OR
【发布时间】:2020-04-24 19:26:56
【问题描述】:

我有一个 API 的响应是 JSON 数组的场景。下面我给出了一个 JSON 响应示例,其中“测试”是整个数组中可用的关键。所以我必须写到 Feature 步骤,我应该在一个步骤中使用 null 和正则表达式验证值。如果我正在验证 reg exp 并且值为 null 或反之亦然。步骤失败。

例如:

* match each $..test== '#regex ^[ A-Za-z0-9-]*$'                                                                                                           
* match each $..test== null

我尝试了以下步骤,但失败了。

* match each $..test== '#regex ^[ A-Za-z0-9-]*$' | test == null


"response": [
{
   "test": "Anand07"

},{
   "test": null
},{
   "test": "Archu06"
}] 

感谢您的帮助。

【问题讨论】:

    标签: selenium automated-tests cucumber karate web-api-testing


    【解决方案1】:

    示例代码:

    Feature: Validation
    
        Scenario:
            * def isValid = function(x){ return x == null || karate.match(x,'#regex ^[ A-Za-z0-9-]*$').pass }
            * def response =
                """
                [
                    {
                        "test": "Anand07"
                    },
                    {
                        "test": null
                    },
                    {
                        "test": "Archu06"
                    }
                ]
                """
            * match each response[*].test == '#? isValid(_)'
    

    【讨论】:

      猜你喜欢
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 2013-10-15
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多