空手道API测试-排序验证方案 [英] Karate API Testing - Sorting Validation Scenario

查看:74
本文介绍了空手道API测试-排序验证方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产生以下响应的请求:

I have a request which produces below response:

{
"totalRows": 10,
"colDefs": [
    {
        "entityAttributeId": "somestring",
        "headerName": "somestring",
        "field": "someNumber",
        "entityPath": "",
        "entityId": "somestring"
    },
    {
        "entityAttributeId": "somestring",
        "headerName": "somestring",
        "field": "someNumber",
        "entityPath": "somestring",
        "entityId": "somestring"
    },
    {
        "entityAttributeId": "somestring",
        "headerName": "somestring",
        "field": "1",
        "entityPath": "",
        "entityId": "somestring"
    },
    {
        "entityAttributeId": "somestring",
        "headerName": "somestring",
        "field": "3",
        "entityPath": "somestring",
        "entityId": "somestring"
    }
],
"rowData": [
    {
        "1": "",
        "2": "s",
        "3": "1939EX",
        "4": "",
        "rowMeta": {
            "text/inthisform": {
                "someid": "1939EX"
            },
            "somestring": {
                "somestring": ""
            }
        }
    },
    {
        "1": "",
        "2": ",
        "3": "1939A2",
        "4": "",
        "rowMeta": {
            "text/inthisform": {
                "someid": "1939A2"
            },
            "somestring": {
                "somestring": "somevalue"
            }
        }
    },
    {
        "1": "",
        "2": "",
        "3": "1939A1",
        "4": "",
        "rowMeta": {
            "text/inthisform": {
                "someid": "1939A1"
            },
            "somestring": {
                "somevalue": "value"
            }
        }
    },

    {
        "1": "",
        "2": "",
        "3": "193901",
        "4": "",
        "rowMeta": {
            "sometext/inthisform": {
                "somevalue": "193901"
            },
            "somevalue": {
                "somevalue": "value"
            }
        }
    }
]
}

现在,我需要对该字段值应用排序功能."sometext/inthisform":{ "somevalue":"193901" }, 在Java中,我可以通过以下方式轻松实现此目的:将这个响应存储在一个数组中并对其进行排序,然后再通过我的排序功能响应对其进行比较.

Now I need to apply sorting feature over this field values."sometext/inthisform": { "somevalue": "193901" }, In Java, I can easily do this by storing this response an array and sort it and later compare it by my sort feature response.

有了空手道,我不知道如何前进.

With Karate, I have no clue, how to go forward.

我正在尝试以下选项: 和def temp = ListDataSet_Response.rowMeta.rowData [*].3

I am trying below option: And def temp = ListDataSet_Response.rowMeta.rowData[*].3

或 和def temp = ListDataSet_Response.rowMeta.sometext/inthisform.somevalue

or And def temp = ListDataSet_Response.rowMeta.sometext/inthisform.somevalue

存储arraylist值.由于空手道未提供任何编译或任何类型的错误信息,因此我不确定在这种情况下如何进行处理?

to store the arraylist values. As Karate does not give any compilation or any kind of error information, I am not sure how to proceed in this cases?

摘要:我想将一个字段的值存储到一个数组中,并想对它进行排序.应用排序后,我想将此新排序后的数组与我的功能排序后的数组响应进行比较.

Summary: I want to store the values of a field into an array and want to apply sort into it. After applying sort, I want to compare this new sorted array with my features sorted array response.

推荐答案

我通过这种方式找到了解决方案:

I found the solution by this way:

  1. 借助jsonpath存储JSONArray值:

  1. Store the JSONArray values with the help of jsonpath:

和def temp = $ ListDataSet_Response.rowData [*].3

And def temp = $ListDataSet_Response.rowData[*].3

直接在功能文件本身中调用排序功能.

Call the sort functions directly in the feature file itself.

这些行添加到了功能文件 背景中:

* def ArrayList = Java.type('java.util.ArrayList')
* def Collections = Java.type('java.util.Collections')

添加了以下行以升序进行排序:创建了列表,并将此临时变量(存储的响应)的值添加到了该列表中.以后,对该列表应用了排序.

Added these line for Sorting in Ascending order:Created list and added values to this temp variable(stored response) into that list.Later,Applied sorting over that list.

And def listAsAscending = new ArrayList()
* eval for(var i = 0; i < sometextid.length; i++) listAsAscending.add(sometextid[i])
* eval Collections.sort(listAsAscending, java.lang.String.CASE_INSENSITIVE_ORDER)
* print listAsAscending

类似地,降序:

And def listAsDescending = new ArrayList()
* eval for(var i = 0; i < sometextid.length; i++) listAsDescending.add(sometextid[i])
* eval Collections.sort(listAsDescending, Collections.reverseOrder())
* print listAsDescending

要比较列表,并在稍后与我的排序API响应进行比较:

To compare the list, with my sort API response later:

* match sortedTemp == listAsAscending
* match sortedTemp == listAsDescending

这篇关于空手道API测试-排序验证方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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