如何在空手道中使用条件 [英] How to use if condition in Karate

查看:106
本文介绍了如何在空手道中使用条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下Json回复

Suppose I have the following Json response

[
    {
        id: 1,
        name: "John",
        password: "JohnsPassword54",
    },
    {
        id: 2,
        name: "David",
        password: "DavidsPassword24",
    }
]

那我怎么能提取名为David的数组进行进一步验证?

Then how can I extract the array with name David to do further validation?

例如我想说如果名字==大卫然后保存id

e.g. I want to say if name == David then save the id

推荐答案

干得好:)掌握Json-Path是得到的关键空手道最多!

Well done :) Mastering Json-Path is key to get the most out of Karate !

仅仅为了演示,这是另一种选择,使用 get 关键字从返回的数组中获取第一个元素,因为Json-Path通配符搜索总是返回一个数组:

Just for the sake of demo, here is another option, using the get keyword to get the first element out of the array returned, as Json-Path wildcard searches always return an array:

* def response = 
"""
[
    {
        id: 1,
        name: "John",
        password: "JohnsPassword54"
    },
    {
        id: 2,
        name: "David",
        password: "DavidsPassword24"
    }
]
"""
* def userId = get[0] response $[?(@.name == 'David')].id
* match userId == 2

这篇关于如何在空手道中使用条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆