【问题标题】:Karate: How to check attribute value in response is null [duplicate]空手道:如何在响应中检查属性值是否为空 [重复]
【发布时间】:2021-11-17 01:46:39
【问题描述】:

我有 2 个案例需要验证

案例1:验证属性lastName是否存在作为响应还需要检查其值是否为null

{
 "name" : 
   {
      "firstName" : "Peter",
      "lastName"  : null
   }
}

案例 2:验证属性在响应中不存在

{
 "name" : 
   {
      "firstName" : "Peter"
   }
}

在这里,我尝试使用karate.get('response.name.lastName'),但在这两种情况下都会返回 null

【问题讨论】:

    标签: api testing automation frameworks karate


    【解决方案1】:

    你真的应该阅读文档:https://github.com/intuit/karate#null-and-notpresent

    * def response = { "name": { "firstName": "Peter", "lastName": null } }
    * match response.name.lastName == '#null'
    
    * def response = { "name": { "firstName": "Peter" } }
    * match response.name.lastName == '#notpresent'
    

    【讨论】:

    • 我只需要写一个断言来验证。如果我使用'#null',那么如果它返回值"lastName" : "Thomas" 会失败。响应中不存在属性的情况也会失败。
    • @MandeepRehal 好的,我认为您想同时处理这两种情况,而您的原始问题根本不清楚。我已将此问题标记为重复,并且链接的 q & a 应该回答您所有的问题。如果需要,请提出新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 2013-05-11
    • 1970-01-01
    • 2020-12-03
    • 2018-11-13
    相关资源
    最近更新 更多